UpdateInstalledExtensionCountScheduler

Last modified by Thomas Mortagne on 2016/10/06 10:26

Job name:

Installed extension count update

Job description:

Update the counter of installed extensions

Job cron expression:

0 0 0 * * ?

Job script:

import groovy.json.JsonOutput;

def updateCount(extensionId, documentName) {
  query = [
     'query': [
       'query_string' : [
         'lowercase_expanded_terms' : false,
          'query' : "_timestamp:[now-1d TO *] AND extensions.id:\"$extensionId\""
       ]
     ],
     'aggs': [
       'instanceId_count': [
         'cardinality': [
           'field': 'instanceId'
         ]
       ]
     ]
   ]

  jsonQuery = JsonOutput.toJson(query)
  result = services.activeinstalls.searchInstalls('install2', jsonQuery, ['search_type' : 'count'])

 if (result) {
    count = result.getAsJsonObject('aggregations').getAsJsonObject('instanceId_count').getAsJsonPrimitive('value').getAsLong()

    document = xwiki.getDocument(documentName)

   if (document.getValue('installedCount') != count) {
      document.set('installedCount', count)

      document.save('Update install count')
   }
 } else if (services.activeinstalls.lastError) {
   throw services.activeinstalls.lastError
 }
}

def logger = org.slf4j.LoggerFactory.getLogger('ExtensionCode.UpdateInstalledExtensionCountScheduler')

logger.info('Starting updating installed extensions count')

def extensions = services.query.xwql("select doc.fullName, extension.id from Document doc, doc.object(ExtensionCode.ExtensionProxyClass) as proxy, doc.object(ExtensionCode.ExtensionClass) as extension").execute()
for (extension in extensions)
{
  logger.info('Updating installed extension [{}] count', extension[0])

 try {
    updateCount(extension[1], extension[0])
 } catch (Exception e) {
    logger.error('Failed to update installed count for extension [{}]', extension[0], e)
 }
}

logger.info('Installed extensions count update is finished')

Back to the job list

Tags:
Created by Thomas Mortagne on 2013/01/25 09:45
    
XWiki SAS Copyright © 2019