Skip to content

Instantly share code, notes, and snippets.

@kiran-machhewar
Last active December 21, 2018 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiran-machhewar/4e4e5bbf573bc52adc94f9e46d39f38b to your computer and use it in GitHub Desktop.
Save kiran-machhewar/4e4e5bbf573bc52adc94f9e46d39f38b to your computer and use it in GitHub Desktop.
Schedulable Batch
/**
* @ApexClass : <APEX_CLASS_NAME>
* @Description : This a schedulable batch.
*/
global class <APEX_CLASS_NAME> implements schedulable, Database.Batchable<sObject>, Database.AllowsCallouts {
global String query = '';
global <APEX_CLASS_NAME>(){
query = '<QUERY_GOES_HERE>';
}
/**
*Check whether given apex class already in queue
*/
public static boolean isJobAlreadyRunning(String apexClassName) {
ApexClass batchApexClass = [Select Id From ApexClass Where Name = :apexClassName limit 1][0];
List<AsyncApexJob> apexJobs = [Select Id, CompletedDate From AsyncApexJob Where JobType = 'BatchApex' AND Status != 'Aborted' AND ApexClassID = :batchApexClass.Id AND CompletedDate = null];
if(apexJobs != null && apexJobs.size() > 0)
return true;
else
return false;
}
//Schedule execution
global void execute( SchedulableContext SC ) {
if(isJobAlreadyRunning('<APEX_CLASS_NAME>') ) {
System.debug('Do not shedule, either there is nothing to process or previous job is still in progress.');
} else {
Database.executeBatch(this, 1);
}
start();
}
//initate batch jobs
global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);
}
public static void start() {
Datetime sysTime = System.now().addMinutes(Integer.valueOf(<INTERVAL_IN_MINUTES>));
String chronExpression = '' + sysTime.second() + ' ' + sysTime.minute() + ' ' + sysTime.hour() + ' ' + sysTime.day() + ' ' + sysTime.month() + ' ? ' + sysTime.year();
if(!Test.isRunningTest()){
System.schedule('<JOB NAME TO BE APPEARED IN SCHEDULED JOBS>' + sysTime, chronExpression, new <APEX_CLASS_NAME>());
}
}
//batch apex execution
global void execute(Database.BatchableContext BC, List<sObject> scope){
//logic goes here
}
//finish batch jobs
global void finish(Database.BatchableContext BC){
System.debug('Batch is finished');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment