Skip to content

Commit e9e8d71

Browse files
committed
Merge branch '2.0.x'
Conflicts: src/main/java/com/coreoz/wisp/JobStatus.java src/main/java/com/coreoz/wisp/JobThread.java
2 parents e16caac + 404edb9 commit e9e8d71

22 files changed

Lines changed: 1492 additions & 559 deletions

README.md

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ Wisp Scheduler
66
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.coreoz/wisp/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.coreoz/wisp)
77

88
Wisp is a simple Java Scheduler with a minimal footprint.
9-
Wisp weighs only 30Kb and has zero dependency.
10-
It will only create threads that will be used: if one thread is enough to run all the jobs,
11-
then only one thread will be created.
12-
A second thread will only be created when 2 jobs have to run at the exact same time.
9+
Wisp weighs only 30Kb and has zero dependency except SLF4J.
10+
It will only create threads that will be used: if two threads are enough to run all the jobs,
11+
then only two threads will be created.
12+
A third thread will only be created when 2 jobs have to run at the same time.
1313

1414
The scheduler precision will depend on the system load.
1515
Though a job will never be executed early, it will generally run after 1ms of the scheduled time.
@@ -24,7 +24,7 @@ Include Wisp in your project:
2424
<dependency>
2525
<groupId>com.coreoz</groupId>
2626
<artifactId>wisp</artifactId>
27-
<version>1.0.0</version>
27+
<version>2.0.0</version>
2828
</dependency>
2929
```
3030

@@ -43,6 +43,14 @@ A project should generally contain only one instance of a `Scheduler`.
4343
So either a dependency injection framework handles this instance,
4444
or either a static instance of `Scheduler` should be created.
4545

46+
In production, it is generally a good practice to configure the
47+
[monitor for long running jobs detection](#long-running-jobs-detection).
48+
49+
Upgrade from version 1.x.x to version 2.x.x
50+
-------------------------------------------
51+
- If a cron schedule is used, then cron-utils must be upgraded to version 8.0.0
52+
- The [monitor for long running jobs detection](#long-running-jobs-detection) might be configured
53+
4654
Schedules
4755
---------
4856

@@ -75,7 +83,7 @@ So to use cron expression, cron-utils should be added in the project:
7583
<dependency>
7684
<groupId>com.cronutils</groupId>
7785
<artifactId>cron-utils</artifactId>
78-
<version>6.0.4</version>
86+
<version>8.0.0</version>
7987
</dependency>
8088
```
8189
Then to create a job which is executed every hour at the 30th minute,
@@ -94,6 +102,20 @@ the associated job will never be executed again.
94102
At the first execution, if a past time is referenced a warning will be logged
95103
but no exception will be raised.
96104

105+
### Long running jobs detection
106+
To detect jobs that are running for too long, an optional job monitor is provided.
107+
It can be setup with:
108+
```java
109+
scheduler.schedule(
110+
new LongRunningJobMonitor(scheduler),
111+
Schedules.fixedDelaySchedule(Duration.ofMinutes(1))
112+
);
113+
```
114+
This way, every minute, the monitor will check for jobs that are running for more than 5 minutes.
115+
A warning message with the job stack trace will be logged for any job running for more than 5 minutes.
116+
117+
The detection threshold can also be configured this way: `new LongRunningJobMonitor(scheduler, Duration.ofMinutes(15))`
118+
97119
Plume Framework integration
98120
---------------------------
99121

pom.xml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
24

35
<modelVersion>4.0.0</modelVersion>
46

57
<groupId>com.coreoz</groupId>
68
<artifactId>wisp</artifactId>
7-
<version>1.0.1-SNAPSHOT</version>
9+
<version>2.0.0-SNAPSHOT</version>
810
<packaging>jar</packaging>
911

1012
<name>Wisp Scheduler</name>
@@ -157,17 +159,23 @@
157159
</build>
158160

159161
<dependencies>
162+
<dependency>
163+
<groupId>org.slf4j</groupId>
164+
<artifactId>slf4j-api</artifactId>
165+
<version>1.7.25</version>
166+
</dependency>
167+
160168
<dependency>
161169
<groupId>org.projectlombok</groupId>
162170
<artifactId>lombok</artifactId>
163-
<version>1.16.18</version>
171+
<version>1.18.6</version>
164172
<scope>provided</scope>
165173
</dependency>
166174

167175
<dependency>
168176
<groupId>com.cronutils</groupId>
169177
<artifactId>cron-utils</artifactId>
170-
<version>6.0.4</version>
178+
<version>8.0.0</version>
171179
<optional>true</optional>
172180
</dependency>
173181

src/main/java/com/coreoz/wisp/Job.java

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313
*/
1414
public class Job {
1515

16-
private volatile JobStatus status;
16+
private JobStatus status;
1717
private volatile long nextExecutionTimeInMillis;
1818
private volatile int executionsCount;
1919
private Long lastExecutionTimeInMillis;
20+
private Long timeInMillisSinceJobRunning;
21+
private Thread threadRunningJob;
2022
private final String name;
21-
private final Schedule schedule;
23+
private Schedule schedule;
2224
private final Runnable runnable;
25+
private Runnable runningJob;
2326

2427
// public API
2528

@@ -39,6 +42,19 @@ public Long lastExecutionTimeInMillis() {
3942
return lastExecutionTimeInMillis;
4043
}
4144

45+
/**
46+
* The time of the start of the job execution.
47+
*
48+
* Not null only when {@code #status() == JobStatus.RUNNING}.
49+
*/
50+
public Long timeInMillisSinceJobRunning() {
51+
return timeInMillisSinceJobRunning;
52+
}
53+
54+
public Thread threadRunningJob() {
55+
return threadRunningJob;
56+
}
57+
4258
public String name() {
4359
return name;
4460
}
@@ -64,24 +80,40 @@ public Runnable runnable() {
6480
this.runnable = runnable;
6581
}
6682

67-
Job status(JobStatus status) {
83+
void status(JobStatus status) {
6884
this.status = status;
69-
return this;
7085
}
7186

72-
Job nextExecutionTimeInMillis(long nextExecutionTimeInMillis) {
87+
void nextExecutionTimeInMillis(long nextExecutionTimeInMillis) {
7388
this.nextExecutionTimeInMillis = nextExecutionTimeInMillis;
74-
return this;
7589
}
7690

77-
Job executionsCount(int executionsCount) {
91+
void executionsCount(int executionsCount) {
7892
this.executionsCount = executionsCount;
79-
return this;
8093
}
8194

82-
Job lastExecutionTimeInMillis(Long lastExecutionTimeInMillis) {
95+
void lastExecutionTimeInMillis(Long lastExecutionTimeInMillis) {
8396
this.lastExecutionTimeInMillis = lastExecutionTimeInMillis;
84-
return this;
97+
}
98+
99+
void timeInMillisSinceJobRunning(Long timeInMillisSinceJobRunning) {
100+
this.timeInMillisSinceJobRunning = timeInMillisSinceJobRunning;
101+
}
102+
103+
void threadRunningJob(Thread threadRunningJob) {
104+
this.threadRunningJob = threadRunningJob;
105+
}
106+
107+
void schedule(Schedule schedule) {
108+
this.schedule = schedule;
109+
}
110+
111+
void runningJob(Runnable runningJob) {
112+
this.runningJob = runningJob;
113+
}
114+
115+
Runnable runningJob() {
116+
return runningJob;
85117
}
86118

87119
// toString

src/main/java/com/coreoz/wisp/JobStatus.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,23 @@
66
public enum JobStatus {
77

88
/**
9-
* will not be run ever again
9+
* The job will not be run ever again
1010
*/
1111
DONE,
1212
/**
13-
* will be executed again, but not right now
13+
* The job will be executed at his scheduled time
1414
*/
1515
SCHEDULED,
1616
/**
17-
* a scheduled job that is attached to a thread from the pool and ready to be executed when its time comes
17+
* This is an intermediate status before {@link #RUNNING},
18+
* it means that the job is placed on the thread pool executor
19+
* and is waiting to be executed as soon as a thread is available.
20+
* This status should not last more than a few µs/ms except if the
21+
* thread pool is full and running tasks are not terminating.
1822
*/
1923
READY,
2024
/**
21-
* a job currently running
25+
* The job is currently running
2226
*/
2327
RUNNING,
2428

src/main/java/com/coreoz/wisp/JobThread.java

Lines changed: 0 additions & 65 deletions
This file was deleted.

src/main/java/com/coreoz/wisp/JobThreadPool.java

Lines changed: 0 additions & 101 deletions
This file was deleted.

0 commit comments

Comments
 (0)