BrightSide Workbench Full Report + Source Code
Motor.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.scheduler.motor;
19 
20 import java.util.Date;
21 import java.util.Set;
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;
34 
39 public class Motor implements Runnable {
40 
41  static private Motor motor = null;
42 
43  private ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(5);
44  private ScheduledFuture<?> handle;
45 
46  private Set<AbstractTask> tasks = null;
47 
48  private int entityDay = 0;
49  private EntityTask entityTask = null;
50 
51  private Motor(IConstructor constructor) {
52  this(constructor, 1, TimeUnit.MINUTES);
53  }
54 
55  private Motor(IConstructor constructor, long period, TimeUnit unit) {
56  TaskMigration.migrate(constructor);
57  tasks = TaskSet.load(constructor);
58  handle = scheduler.scheduleAtFixedRate(this, 0, period, unit);
59  WebLoggers.info(this).message("Scheduler started.").log();
60  }
61 
62  static public Motor getInstance() {
63  return motor;
64  }
65 
66  static public Motor getInstance(IConstructor constructor, long period, TimeUnit unit) {
67  return getInstance(constructor, period, unit, false);
68  }
69 
70  static public synchronized Motor getInstance(IConstructor constructor, long period, TimeUnit unit, boolean forceNew) {
71  if(forceNew && motor != null) motor.stop();
72  if(motor == null || forceNew) motor = new Motor(constructor, period, unit);
73  return motor;
74  }
75 
76  public Set<AbstractTask> getTasks() {
77  return tasks;
78  }
79 
80  @Override
81  public void run() {
82  Thread.currentThread().setName("ElephantTaskMotorPool");
83  Date now = new Date();
84 
85  for(AbstractTask task : tasks) {
86  if(handle.isCancelled()) break;
87  if(task.isDone() &&
88  task.getSettings().isActive() &&
89  task.shouldRun(now)) {
90  WebLoggers.info(this).message(I_.format("%s:%s started.",
91  task.getName(), task.getSettings().getDescription())).log();
92  task.doRun(this);
93  }
94  }
95 
96  CheckDate cd = new CheckDate(now);
97  int dayOfYear = cd.getDayOfYear();
98  if(dayOfYear != entityDay) {
99  entityDay = dayOfYear;
100  entityTask = new EntityTask();
101  WebLoggers.info(this).message(I_.format("%s:%s started.",
102  entityTask.getName(), entityTask.getDescription())).log();
103  entityTask.doRun(this);
104  }
105 
106  //LastActivities.generateUserContent(now);
107  }
108 
109  public void stop() {
110  for(AbstractTask task : tasks) {
111  task.stop();
112  WebLoggers.info(this).message(I_.format("%s:%s stopped.",
113  task.getName(), task.getSettings().getDescription())).log();
114  }
115  if(entityTask != null) {
116  entityTask.stop();
117  WebLoggers.info(this).message(I_.format("%s:%s stopped.",
118  entityTask.getName(), entityTask.getDescription())).log();
119  }
120  handle.cancel(true);
121  scheduler.shutdown();
122  WebLoggers.info(this).message("Scheduler shutdown.").log();
123  }
124 
125  public ScheduledFuture<?> runTask(AbstractTask task) {
126  return scheduler.schedule(task, 5, TimeUnit.SECONDS);
127  }
128 
129 }
static String format(String msg, Object... arguments)
Definition: I_.java:49
WebLoggers message(String text, Object... parameters)
Definition: WebLoggers.java:34
static WebLoggers info(Object entity)
Definition: WebLoggers.java:43
static Motor getInstance(IConstructor constructor, long period, TimeUnit unit)
Definition: Motor.java:66
static Motor getInstance()
Definition: Motor.java:62
ScheduledFuture<?> runTask(AbstractTask task)
Definition: Motor.java:125
Set< AbstractTask > getTasks()
Definition: Motor.java:76
static synchronized Motor getInstance(IConstructor constructor, long period, TimeUnit unit, boolean forceNew)
Definition: Motor.java:70
static Set< AbstractTask > load(IConstructor constructor)
Definition: TaskSet.java:41
static TaskSettingsSet migrate(IConstructor constructor)