Manually kick a CronJob

#1

I looked into the definition of this type but could not find a start or run like Batch or MapReduce jobs!

How can I manually kick a CronJob?

0 Likes

#2

The whole point of a CronJob is that it is started by a timer… To perform the same action that a CronJob is performing, you can just call the function that the cron job calls.

1 Like

#3

Thanks @rileysiebel indeed I can just call the action executed by the cron job. I was just wondering if there is a way at least for testing to trigger the cron manually, but it seems it does not.

0 Likes

#4

CronJobs can be manually kicked off by invoking the schedule() member function on the CronJob Instance you wish to trigger. This function declares a schedule for the given CronJob to be valid in between the start and end times given. When testing, always put a small time window, because you often don’t want to overwrite any existing schedule. If you put the cronExpression input of the function to null or an empty string, the CronJob will be triggered one time immediately and be processed in the CronQueue as it would if it was triggered in an automated way.

Example:

CronJob.get('cron_job_id').schedule({startTime: "2018-04-24", endTime: "2018-04-24T23:00", cronExpression: "", skipOverdue: true})
6 Likes

#5

CronJob.get(‘cron_job_id’).schedule({startTime: “2018-04-24”, endTime: “2018-04-24T23:00”, cronExpression: “”, skipOverdue: true})

This seems to modify the cron expression in scheduleDef, which does not seem to be override by provisioning. So after the cron runs, the expression has to be changed back to the original version.

CronJob.get(‘cron_job_id’).schedule({cronExpression: "0 2 0 1 * ? *", skipOverdue: true})
1 Like