Wednesday, June 20, 2012

Force.com: The ugly state of job management from command line

I just couldn't do it in any reasonably good way - at least I haven't found one.  Unfortunately I often felt the need to do it.  For instance, after creating a new sandbox, one of the clean-up tasks would be to de-schedule those useless jobs so they won't generate unnecessary messages.  In any given production org there might be tens of, or even hundreds of jobs.  Do you want to click through them from the Web UI?  Naturally any admin would think about a script of some sort.  Here's the kick - I haven't been able to come up with one that meets the following criteria

  • Reasonably flexible
  • Reasonably powerful
  • Reasonably robust

This is a script I use currently to remove old jobs (mostly copied RupaliJ's post here). 

List<CronTrigger> cron = [SELECT id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger where nextfiretime=null];
for(CronTrigger CT: cron)
{
   try
   {
       System.abortjob(CT.id);
       system.debug(ct);
   } catch(Exception e1)
   {
       System.debug('--- Unable to delete scheduled job with Scheduled_Geocoder_Job_Id = ' + CT.id + ' because ' + e1.getMessage());
   }
}

Judging by the criteria, this falls short for the following reasons:
  • It cannot select job based on class used, job name, or type of job.  With those attributes not being supported by the CronTrigger object, the power of the script to meet different needs is next to none.
  • Since abortJob is considered a DML statement, this script will end after it has terminated 150 jobs.  Usually you bulkify your processing for most Apex DML operations, but since abortJob () doesn't support collections as argument, there's no way to actually do bulk operations.

The need for an efficient way has been made more urgent by this mysterious issue (also here), as you may have to delete/recreate some/all your scheduled jobs just to deploy a new class (itself not even a Schedulable).

If you have a better approach, I'm definitely all ears.

No comments:

Post a Comment