I have to create scheduled job, that will be executed every minute. Using http://advancedapex.com/2012/11/14/scheduledapex/ following code was written:
global class ScheduledOperatorStatusMonitor implements Schedulable{
public Interface IScheduleOperatorStatusMonitor{
void execute(SchedulableContext SC);
}
global void execute(SchedulableContext SC){
Type targettype = Type.forName('ScheduleOperatorStatusMonitorHandler');
if(targettype!=null) {
IScheduleOperatorStatusMonitor obj = (IScheduleOperatorStatusMonitor)targettype.NewInstance();
obj.execute(sc);
ScheduledOperatorStatusMonitor.start();
}
}
private static void start(){
Datetime sysTime=System.now().addSeconds(60);
String chronExpression = '' + sysTime.second() + ' ' + sysTime.minute() + ' ' + sysTime.hour() + ' ' + sysTime.day() + ' ' + sysTime.month() + ' ? ' + sysTime.year();
System.schedule( 'ScheduledOperatorStatusMonitor ' + sysTime, chronExpression, new ScheduledOperatorStatusMonitor() );
}
}
public class ScheduleOperatorStatusMonitorHandler implements ScheduledOperatorStatusMonitor.IScheduleOperatorStatusMonitor {
private void doWork(){
//some code here
}
public void execute( SchedulableContext SC )
{
doWork();
}
}
Everything is fine, only one issue - number of records for ScheduledOperatorStatusMonitor in Setup>Jobs>Scheduled Jobs is constantly increased. How code can be changed ( maybe add System.abortJob) to prevent this issue?.. It will be nice, if only one record for currently executed instance will be shown on UI.