timeReactivates a Scheduler Job written in Groovy on code change
TypeXAR
CategoryOther
Developed by

Clemens Robbenhaar

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Description

Note: this is only meant for development. Do not use it in production!

When writing a Scheduler Job in Groovy one needs to deactivate and reactivate the job every time one saves the corresponding page. Otherwise the code from the last version of the page is used.

This can be done with two clicks in the Scheduler UI, but it is also possible to automatize this.

For this create a page somewhere, and add the following objects in the object editor:

One object of class XWiki.ComponentClass with the following fields.

Component Role Type
org.xwiki.observation.EventListener
Component Role Hint
schedulerJobReinitOnSave
Component Scope
Current Wiki    (or whatever you prefer)

Three objects of class  XWiki.ComponentMethodClass with the following fields:

Method name
getEvents
Method body code
 
{{groovy}}
import org.xwiki.bridge.event.*

xcontext.method.output.value = [new DocumentUpdatedEvent()]
{{/groovy}}
 
Method name
getName
Method body code
 
{{groovy}}
xcontext.method.output.value = "schedulerJobReinitOnSave"
{{/groovy}}
Method name
onEvent
Method body code
 
{{groovy}}
// avoid getting called twice:
if (xcontext.request.getAttribute('schedulertaskreinit running') == null) {
  xcontext.request.setAttribute('schedulertaskreinit running', true)

 def updatedDoc = xcontext.method.input.get(1)
 def jobInfo = updatedDoc.getObject('XWiki.SchedulerJobClass')


 if (jobInfo != null && xwiki.scheduler.getJobStatus(jobInfo).getValue() == 'Normal') {
    xwiki.scheduler.unscheduleJob(jobInfo)
    xwiki.scheduler.scheduleJob(jobInfo)
   if (xwiki.scheduler.getJobStatus(jobInfo).getValue() == 'Normal') {
      System.err.println('rescheduled ' + updatedDoc.getFullName())
   } else {
      System.err.println('ERROR when rescheduling ' + updatedDoc.getFullName() + ' status is now ' + xwiki.scheduler.getJobStatus(jobInfo).getValue())
   }
 }
}
{{/groovy}}

Afterwards every time you save your job page you should see a message like:

rescheduled MyApplication.Code.MyFancyJob

 

Prerequisites & Installation Instructions

Creation and editing of the page must be done with a user with programming rights.

You can also download the XAR and import it into your wiki.

     

Get Connected