18 package org.turro.scheduler.motor;
20 import java.util.Date;
22 import java.util.concurrent.Executors;
23 import java.util.concurrent.ScheduledExecutorService;
24 import java.util.concurrent.ScheduledFuture;
25 import java.util.concurrent.TimeUnit;
26 import org.amic.util.date.CheckDate;
27 import org.turro.elephant.context.IConstructor;
28 import org.turro.i18n.I_;
29 import org.turro.log.WebLoggers;
30 import org.turro.scheduler.entities.EntityTask;
31 import org.turro.scheduler.task.AbstractTask;
32 import org.turro.scheduler.task.TaskSet;
33 import org.turro.scheduler.task.migration.TaskMigration;
39 public class Motor implements Runnable {
41 static private Motor motor =
null;
43 private ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(5);
44 private ScheduledFuture<?> handle;
46 private Set<AbstractTask> tasks =
null;
48 private int entityDay = 0;
52 this(constructor, 1, TimeUnit.MINUTES);
58 handle = scheduler.scheduleAtFixedRate(
this, 0, period, unit);
67 return getInstance(constructor, period, unit,
false);
71 if(forceNew && motor !=
null) motor.
stop();
72 if(motor ==
null || forceNew) motor =
new Motor(constructor, period, unit);
82 Thread.currentThread().setName(
"ElephantTaskMotorPool");
83 Date now =
new Date();
86 if(handle.isCancelled())
break;
88 task.getSettings().isActive() &&
89 task.shouldRun(now)) {
91 task.getName(), task.getSettings().getDescription())).
log();
96 CheckDate cd =
new CheckDate(now);
97 int dayOfYear = cd.getDayOfYear();
98 if(dayOfYear != entityDay) {
99 entityDay = dayOfYear;
103 entityTask.
doRun(
this);
113 task.getName(), task.getSettings().getDescription())).
log();
115 if(entityTask !=
null) {
121 scheduler.shutdown();
126 return scheduler.schedule(task, 5, TimeUnit.SECONDS);
static String format(String msg, Object... arguments)
WebLoggers message(String text, Object... parameters)
static WebLoggers info(Object entity)
static Motor getInstance(IConstructor constructor, long period, TimeUnit unit)
static Motor getInstance()
ScheduledFuture<?> runTask(AbstractTask task)
Set< AbstractTask > getTasks()
static synchronized Motor getInstance(IConstructor constructor, long period, TimeUnit unit, boolean forceNew)
static Set< AbstractTask > load(IConstructor constructor)
static TaskSettingsSet migrate(IConstructor constructor)