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;  
}
  • 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? – Daniel Ballinger Apr 6 '15 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. – user2451 Apr 7 '15 at 12:05
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.

  • 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? – user2451 Apr 15 '15 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. – Daniel Ballinger Apr 15 '15 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. – user2451 Apr 25 '15 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.