Parameters for hourly execution (run in System Log):
String CRON_EXP = '0 0 * * * ?';
clsScheduledHourly sch = new clsScheduledHourly();
system.schedule('Hourly Sync', CRON_EXP, sch);
Class:global class clsScheduledHourly implements Schedulable{
global void execute(SchedulableContext ctx) {
syncBatch();
}
public static void syncBatch() {
List<AsyncApexJob> apexJobsList = new List<AsyncApexJob>(
[Select TotalJobItems, Status, ApexClass.Name
From AsyncApexJob
where Status!='Completed' and Status!='Aborted' and ApexClass.Name='syncReportBchbl']
);
if(apexJobsList==null || apexJobsList.size()<=0) {
syncReportBchbl rb = new syncReportBchbl();
try {
Database.executeBatch(rb, 5);
} catch(System.LimitException e) {
//do something
}
}
}
Test: static testMethod void testExecute() {
String CRON_EXP = '0 0 0 3 9 ? 2022';
Test.startTest();
// Schedule the test job
String jobId = System.schedule('testBasicScheduledApex', CRON_EXP, new clsScheduledHourly());
// Get the information from the CronTrigger API object
CronTrigger ct = [SELECT id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
// Verify the expressions are the same
System.assertEquals(CRON_EXP, ct.CronExpression);
// Verify the job has not run
System.assertEquals(0, ct.TimesTriggered);
// Verify the next time the job will run
System.assertEquals('2022-09-03 00:00:00', String.valueOf(ct.NextFireTime));
// Verifiy that it has not run
Test.stopTest();
}
}
No comments:
Post a Comment