function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Raj CRaj C 

Implement Database.Batchable and Schedulable interfaces in a single class

We run several batch and scheduled jobs daily. Earlier I used to have two seperate classes - one that implements Database.Batchable and another class that implements the schedulable interface. Then I run the scheduled job from the Dev console.

I combined both the above classes into a single class that implements both the interfaces. The code compiled and has been running batch jobs daily.

But every once in a while the job gets aborted. However when I run the same job manually (i.e. go to the Dev console and run) it runs OK. Did any one else have the same issue.

I contacted Salesforce support and their feedback was that the SOQL query in the Start function is timing out. They opened up the SOQL timeout limits and the job continued working for some time (~90 days) and I now see the job aborting every once in a while.

Any one tried the same above and seen any issues.


Best Answer chosen by Raj C
Raj CRaj C
Thanks - glad to hear I am not the only one. will continue chugging along - hope this issue wont happen as frequent and fades away on its own... :)

All Answers

logontokartiklogontokartik
Well. It seems like there are 2 issues that you are talking about?
1. If there is any problem if we implement both batchable and schedulable interfaces. <kv> I dont see any problem, the only concerns with this approach is that they share the constructors. It should not have impacted the aborting of jobs
2. The timeouts in the Batch job, yes, I have seen this happen in the past when the timeout was 3 minutes for the SOQL queries. Now its 10 minutes. Unless your SOQL is too complex and not using any ids and using like statements and != statements I dont think you would hit the limit. Did you try running the SOQL outside of Batch job? Can you also share the soql if possible? There are basically various reasons why your jobs are aborted by Salesforce. The only way to identify is to contact support and ask them to track it down. 
Raj CRaj C
Thanks, yes I already contacted Salesforce support and optimized the query. 

The scheduled job runs well most of the time...I run the job at 1am and 2am daily when there are no other jobs running. However it aborts every once in a while.

What bothers me is that when I try to run the batch job manually it runs without issue. Infact the job never failed when run manually. 




kibitzerkibitzer
I don't think it has to do with the fact that they are implemented by the same class. I do think there is something internal going on with regards to batches started from scheduled apex. I've seen the same issue (with a batch class that is not the same as the scheduled class). But as you say, it only happens once in a while and does not seem to be reproducable.
Raj CRaj C
Thanks - glad to hear I am not the only one. will continue chugging along - hope this issue wont happen as frequent and fades away on its own... :)
This was selected as the best answer