BrightSide Workbench Full Report + Source Code
elephant-scheduler/src/main/java/org/turro/scheduler/task/AbstractTask.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 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 
19 package org.turro.scheduler.task;
20 
21 import org.turro.scheduler.task.settings.TaskSettings;
22 import java.time.Duration;
23 import java.util.Date;
24 import java.util.concurrent.ScheduledFuture;
25 import org.turro.elephant.context.IConstructor;
26 import org.turro.html.Colors;
27 import org.turro.log.WebLoggers;
28 import org.turro.scheduler.motor.Motor;
29 import org.turro.string.Phrases;
30 import org.turro.util.Comparison;
31 
36 public abstract class AbstractTask implements Runnable, Comparable<AbstractTask> {
37 
38  private ScheduledFuture<?> future;
39 
40  private TaskSettings settings;
41 
42  private IConstructor constructor;
43  private Date lastExecuted;
44 
45  public void setConstructor(IConstructor constructor) {
46  this.constructor = constructor;
47  }
48 
49  public void setSettings(TaskSettings settings) {
50  this.settings = settings;
51  }
52 
54  return constructor;
55  }
56 
58  if(settings == null) settings = TaskSettings.from(this);
59  return settings;
60  }
61 
62  public void setLastExecuted(Date lastExecuted) {
63  this.lastExecuted = lastExecuted;
64  }
65 
66  public boolean isDone() {
67  return future == null ? true : future.isDone();
68  }
69 
70  public boolean shouldRun(Date now) {
71  if(enoughTimeLapse(now) && settings.shouldRun(now)) {
72  return settings.getConstraints().needsRefresh();
73  }
74  return false;
75  }
76 
77  public void doRun(Motor motor) {
78  lastExecuted = new Date();
79  future = motor.runTask(this);
80  }
81 
82  public void stop() {
83  if(future != null && !future.isDone()) {
84  future.cancel(true);
85  }
86  }
87 
88  @Override
89  public void run() {
90  try {
91  execute();
92  } catch(Exception ex) {
93  WebLoggers.warning(this).exception(ex).log();
94  }
95  }
96 
97  public abstract boolean isSystem();
98  public abstract String getName();
99  public abstract String getDataLabel();
100  public abstract void execute();
101 
102  private boolean enoughTimeLapse(Date now) {
103  return lastExecuted == null || (Duration.between(lastExecuted.toInstant(), now.toInstant()).getSeconds() > (60 * 5));
104  }
105 
106  /* Utils */
107 
108  public String getColor() {
109  return Colors.cssByName(isSystem() ? "DarkRed" : "DarkBlue");
110  }
111 
112  public String getIcon() {
113  return isSystem() ? "gears" : "user";
114  }
115 
116  public String getDescription() {
117  return Phrases.start()
118  .add(getName(), "[]")
119  .add(getSettings().getDescription())
120  .toString();
121  }
122 
123  /* Comparable */
124 
125  @Override
126  public int compareTo(AbstractTask o) {
127  return Comparison.ascendant()
128  .compare(isSystem(), o.isSystem())
129  .compareTo(getSettings().compareTo(o.getSettings()))
130  .get();
131  }
132 
133 }
static WebLoggers warning(Object entity)
Definition: WebLoggers.java:47
WebLoggers exception(Throwable throwable)
Definition: WebLoggers.java:29
ScheduledFuture<?> runTask(AbstractTask task)
Definition: Motor.java:125
static TaskSettings from(AbstractTask task)