Job.apex is a library that aims to easily create and manage Apex scheduled jobs.
Job.apex is created to offer a readable and fluent API to create Apex scheduled jobs as an alternative to the normal cron expression way. Besides, Job.apex supports a repeating job to work around the limitation in Apex that scheduled jobs cannot run in an interval less than 1 hour. Also Job.apex has API to manage all the scheduled jobs.
Job.apex has a dependency on R.apex. Please include R.apex before getting started with Job.apex.
public class CustomJob extends Func {
public CustomJob() {
super(1);
}
public override Object exec(Object context) {
System.debug('Custom job executed');
return null;
}
}
Job j = new Job('test', new CustomJob());
new Job('test', new CustomJob())
.everyDay()
.atHour(8)
.schedule();
// Schedule a job that runs at 8:00 everyday
new Job('test', new CustomJob())
.betweenDaysOfWeek('Mon', 'Fri')
.atHour(8)
.schedule();
// Schedule a job that runs at 8:00 every week day
new Job('test', new CustomJob())
.fromDay(1)
.everyDays(2)
.atHour(8)
.schedule();
// Schedule a job that runs at 8:00 every other day from day 1
new Job('test', new CustomJob())
.inMonth('May')
.on2nd('Sun')
.atHour(8)
.schedule();
// Schedule a job that runs at 8:00 on the second Sunday of May
new Job('test', new CustomJob())
.onLastWeekdayOfMonth()
.atHour(8)
.schedule();
// Schedule a job that runs at 8:00 on the last week day of every month
new Job('test', new CustomJob())
.inMonth('Sept')
.onNearestWeekday(20)
.atHour(8)
.schedule();
// Schedule a job that runs at 8:00 on the nearest week day of 9/20
new Job('test', new CustomJob())
.startNow()
.everyMinutes(30)
.repeatForever()
.schedule();
// Schedule a job that first runs 30 minutes after now, and then repeats itself forever