How to change cron job status using Groovy script?

We have gone through situations to forcefully stop the running cron job or set the cron job status to Aborted, so one of the faster ways to do it is using the following groovy script.


import de.hybris.platform.jalo.flexiblesearch.FlexibleSearch;
import de.hybris.platform.cronjob.model.CronJobModel;
import de.hybris.platform.core.PK;
import de.hybris.platform.cronjob.enums.CronJobStatus;

// Get all Sync cronjobs that are in UNKNOWN status
List<String> pks = FlexibleSearch.getInstance().search(
        "select {c.PK} from {Cronjob as c}, {Job as j}, {EnumerationValue as e} where {e.code} = 'RUNNING' AND {c.status} = {e.PK} AND {j.pk} = {c.job}  AND {j.code}='jobCode'", Collections.EMPTY_MAP,Arrays.asList(String.class),true,true,0,-1).getResult();

// Set status to ABORTED for all Sync jobs in UNKNOWN status
for (String pk : pks) {
    CronJobModel cronJob = modelService.get(PK.fromLong(pk.toLong()));
    cronJob.setStatus(CronJobStatus.ABORTED);

    System.out.println("Setting status to ABORTED for  Sync cronjob with code - " + cronJob.getCode() + ",  job -  " + cronJob.getJob());
}
modelService.saveAll();

System.out.println("Total cronjobs reset = " + pks.size());

Leave a Comment

Your email address will not be published. Required fields are marked *