1

Any thoughts on why the removeExistingCompletedJobs Method suddenly stopped working around March 2015? We now get an Apex error "Job does not exist or is already aborted" I'm not sure if it coincided with Spring '15 release or not as I wasn't watching closely. Code done by another developer bit over my head with the Cron stuff but I understand it a little.

I know there are other ways to remove scheduled jobs but this had been working fine for past 18 months. Classes are using API 31.0.

Submitted a case but Support hasn't been too helpful since this is in Production so I had to comment out code as a workaround (uncommented here).

    global void execute(SchedulableContext SC){
    queryBalance();

  removeExistingCompletedJobs(sc);
  scheduleNewJobs(sc);

  //Schedule a new batch to retrieve Existing Assignments
  startBatchToRetrieveAssignments();

  //Schedule a new batch to mark assigments as approved or rejected
  startBatchApproveAssignments();

  startBatchToDeleteHit();
}

//=========================================================================//
//Find new jobs and schedule them in the Queue
//=========================================================================//
   for(Schedule__c schedule :lstSchedule){
     schedule.Schedule_Job_Name__c = getJobName(schedule);
     schedule.Schedule_Job_Created__c = true;

     MturkScheduleJobPoster  mturkJobPoster = new MturkScheduleJobPoster(schedule.ID);
     **Id jobID = System.schedule(schedule.Schedule_Job_Name__c, getCronExpression(schedule),
                   mturkJobPoster );  
     schedule.Schedule_Job_ID__c  = jobID;**           
   } 

public void removeExistingCompletedJobs(SchedulableContext SC) {
   List<Schedule__c> lstSchedule = [SELECT Name,  Schedule_Job_ID__c
                                    FROM Schedule__c
                                    WHERE Schedule_Job_Completed__c = true
                                    AND  Schedule_Job_ID__c != null
                                    AND  StartDateTime__c < :System.now()
                                    Limit 100];

   for(Schedule__c schedule :lstSchedule){
       System.abortJob(schedule.Schedule_Job_ID__c);
       schedule.Schedule_Job_ID__c = null;
       schedule.Schedule_Job_Completed__c = false;
       schedule.Schedule_Job_Created__c = false;
       schedule.Schedule_Job_Name__c = null;
   }
   update lstSchedule;  
}
2
  • In what way is it no longer working? Are you getting an error? Does the SOQL query return any records? Is the abortJob call failing? Is the update failing? Apr 6, 2015 at 21:41
  • Apex error "Job does not exist or is already aborted". Happens with each scheduled run even after removing all jobs through Anonymous script. I also edited question to make that clearer.
    – ddeve
    Apr 7, 2015 at 12:05

1 Answer 1

0

I'm going to assume that the error

"Job does not exist or is already aborted"

is coming from the call to System.abortJob(schedule.Schedule_Job_ID__c);. The System.abortJob method takes a Job_Id parameter. If the Schedule__c.Schedule_Job_ID__c field has an ID that is no longer valid then you will get an error when attempting to abort it.

You could verify the ID first against CronTrigger. If it doesn't exist or has an ERROR or DELETED state you can't abort it.

3
  • So we tried manually removing all jobs returned by the CronTrigger in all states, then turning job back on again and still no dice. I've seen references in past to "ghost jobs" that we cannot see and have wondered if that is somehow the issue?
    – ddeve
    Apr 15, 2015 at 14:55
  • Maybe. Have you examined the problem Id that is being passed to System.abortJob? Ideally you should be able to find the corresponding job in CronTrigger. If it isn't there then it probably doesn't need to be aborted. I.e. Schedule__c.Schedule_Job_ID__c may no longer reference a valid job when it comes time to call abortJob. Apr 15, 2015 at 23:51
  • I'm still not sure what happened but now that I understand logic better it didn't make sense to use the Custom Object records to find jobs to kill anyway. I changed logic to use the CronTrigger and abort from there and seems to be working.
    – ddeve
    Apr 25, 2015 at 16:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .