- Count how many current jobs are being executed.
- This information is stored in the AsyncApexJob table.
- Before calling the Database.executebatch() method within a scheduled class,
you should try something like:
//check if there are 5 active batch jobs
if ([SELECT count() FROM AsyncApexJob WHERE JobType='BatchApex' AND (Status = 'Processing' OR Status = 'Preparing')] < 5])
{
Database.executeBatch(batchClassInstance);
}
else
{
//schedule this same schedulable class again in 30 mins
nameOfYourSchedulableClass sc = new nameOfYourSchedulableClass();
Datetime dt = Datetime.now() + (0.024305); // i.e. 30 mins
String timeForScheduler = dt.format('s m H d M \'?\' yyyy');
Id schedId = System.Schedule('MatrixRetry'+timeForScheduler,timeForScheduler,sc);
}
- In the same 'else' clause you can send an e-mail to yourselves so you're notified that the job will run a bit later than normal.
Note: - You can have more than 5 scheduled classes that will call Database.executeBatch() in a Queued status
- This information is stored in the AsyncApexJob table.
- Before calling the Database.executebatch() method within a scheduled class,
you should try something like:
//check if there are 5 active batch jobs
if ([SELECT count() FROM AsyncApexJob WHERE JobType='BatchApex' AND (Status = 'Processing' OR Status = 'Preparing')] < 5])
{
Database.executeBatch(batchClassInstance);
}
else
{
//schedule this same schedulable class again in 30 mins
nameOfYourSchedulableClass sc = new nameOfYourSchedulableClass();
Datetime dt = Datetime.now() + (0.024305); // i.e. 30 mins
String timeForScheduler = dt.format('s m H d M \'?\' yyyy');
Id schedId = System.Schedule('MatrixRetry'+timeForScheduler,timeForScheduler,sc);
}
- In the same 'else' clause you can send an e-mail to yourselves so you're notified that the job will run a bit later than normal.
Note: - You can have more than 5 scheduled classes that will call Database.executeBatch() in a Queued status
No comments :
Post a Comment