Skip to content

Commit a659e14

Browse files
committed
scheduler: add db-scheduler module
- fix #3497
1 parent 9366f99 commit a659e14

File tree

19 files changed

+1762
-11
lines changed

19 files changed

+1762
-11
lines changed
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
== db-scheduler
2+
3+
Task scheduler module using https://github.com/kagkarlsson/db-scheduler[db-scheduler].
4+
5+
=== Usage
6+
7+
1) Add the dependencies (hikari):
8+
9+
[dependency, artifactId="jooby-hikari:DataSource via HikariCP, jooby-db-scheduler:Db Scheduler Module"]
10+
.
11+
12+
2) Add database driver (mySQL here):
13+
14+
[dependency, groupId="mysql", artifactId="mysql-connector-java", version="${mysql-connector-java.version}"]
15+
.
16+
17+
3) Install DbScheduler. Add SampleJob:
18+
19+
.Java
20+
[source, java, role="primary"]
21+
----
22+
import io.jooby.dbscheduler.DbSchedulerModule;
23+
24+
{
25+
install(new HikariModule());
26+
install(new DbSchedulerModule(Tasks.recurring(...)));
27+
}
28+
----
29+
30+
.Kotlin
31+
[source, kt, role="secondary"]
32+
----
33+
import io.jooby.dbscheduler.DbSchedulerModule
34+
35+
{
36+
install(DbSchedulerModule(Tasks.recurring(...)))
37+
}
38+
----
39+
40+
=== Tasks
41+
42+
Tasks are created as described in https://github.com/kagkarlsson/db-scheduler[db-scheduler documentation]. Optionally,
43+
you can annotate a method with the javadoc:dbscheduler.Scheduled[] annotation:
44+
45+
.Sample Job
46+
[source, java, role="primary"]
47+
----
48+
import io.jooby.dbscheduler.Scheduled;
49+
50+
public class SampleJob {
51+
52+
@Scheduled("1m")
53+
public void everyMinute() {
54+
...
55+
}
56+
}
57+
----
58+
59+
.Kotlin
60+
[source, kt, role="secondary"]
61+
----
62+
import io.jooby.dbscheduler.Scheduled
63+
64+
class SampleJob {
65+
66+
@Scheduled("1m")
67+
fun everyMinute() : Unit {
68+
...
69+
}
70+
}
71+
----
72+
73+
Once you annotate your method you must create task from them with:
74+
75+
.Bean Tasks
76+
[source, java]
77+
----
78+
import io.jooby.dbscheduler.BeanTasks;
79+
80+
{
81+
install(new HikariModule());
82+
install(new DbSchedulerModule(BeanTasks.recurring(this, SampleJob.class)));
83+
}
84+
----
85+
86+
A task method must follow these rules:
87+
88+
- Must be a public method
89+
- Possible arguments: none (zero), `TaskInstance`, `ExecutionContext`, `task data` or any other application service.
90+
- Return value: Task can return a value, which is persisted by DbScheduler. This is known as task data or task state.
91+
92+
=== Scheduled
93+
94+
The javadoc:dbscheduler.Scheduled[] annotation supports simple and cron triggers as well as property references:
95+
96+
.Same as .fixedDelay(Duration) with duration.
97+
----
98+
@Scheduled("1h")
99+
----
100+
101+
.Same as .fixedDelay(Duration) with duration set to N seconds.
102+
----
103+
@Scheduled("FIXED_DELAY|Ns")
104+
----
105+
106+
.Same as .daily(LocalTime) with optional time zone (e.g. Europe/Rome, UTC)
107+
----
108+
@Scheduled("DAILY|12:30,15:30...(|time_zone)")
109+
----
110+
111+
.Cron, every 5 minutes
112+
----
113+
@Scheduled("0 0/5 * * * ?")
114+
----
115+
116+
.Cron, fires every 5 minutes, at 10 seconds after the minute (i.e. 10:00:10 am, 10:05:10 am, etc.)
117+
----
118+
@Scheduled("10 0/5 * * * ?")
119+
----
120+
121+
.Property reference
122+
----
123+
@Scheduled("mytask.trigger")
124+
----
125+
126+
The `mytask.trigger` must be defined in your application property file. It could be a any of previous expressions.
127+
128+
=== Configuration
129+
130+
Configuration from properties files is fully supported, just need to add javadoc:dbscheduler.DbSchedulerProperties[] properties to your
131+
application configuration file:
132+
133+
.Options
134+
[source, properties]
135+
----
136+
# Turn on/off scheduler.
137+
db-scheduler.enabled = true
138+
# Set number of threads to use, default is to use the number of available processor
139+
db-scheduler.threads = 8
140+
db-scheduler.pollingInterval = 10s
141+
db-scheduler.alwaysPersistTimestampInUTC = true
142+
db-scheduler.enableImmediateExecution = false
143+
# No need to use registerShutdownHook, the scheduler is shutdown on application shutdown
144+
db-scheduler.registerShutdownHook = false
145+
db-scheduler.shutdownMaxWait = 1s
146+
----
147+
148+
Check more configuration options at https://github.com/kagkarlsson/db-scheduler?tab=readme-ov-file#configuration[configuration]
149+
150+
=== REST API
151+
152+
This modules comes with a simple REST API (sort of) to manage tasks:
153+
154+
.Scheduler API
155+
[source, java, role="primary"]
156+
----
157+
import io.jooby.dbscheduler.DbSchedulerApp;
158+
import io.jooby.dbscheduler.DbSchedulerModule;
159+
160+
{
161+
install(new DbScheduler(SampleJob.class));
162+
163+
mount("/scheduler", new DbSchedulerApp());
164+
}
165+
----
166+
167+
.Kotlin
168+
[source, kt, role="secondary"]
169+
----
170+
import io.jooby.dbscheduler.DbSchedulerApp
171+
import io.jooby.dbscheduler.DbSchedulerModule
172+
173+
{
174+
install(DbScheduler(SampleJob::class.java))
175+
176+
mount("/scheduler", DbSchedulerApp())
177+
}
178+
----
179+
180+
The API supports all these operations:
181+
182+
.List all tasks
183+
----
184+
GET /
185+
----
186+
187+
.Running tasks
188+
----
189+
GET /running
190+
----
191+
192+
.List tasks
193+
----
194+
GET /{taskName}
195+
----
196+
197+
.Reschedule a task
198+
----
199+
GET /{taskName}/reschedule
200+
----
201+
202+
.Pause schedule
203+
----
204+
GET /pause
205+
----
206+
207+
.Resume
208+
----
209+
GET /resume
210+
----
211+
212+
.State
213+
----
214+
GET /state
215+
----

docs/asciidoc/modules/modules.adoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,16 @@ Available modules are listed next.
5454
* link:/modules/thymeleaf[Thymeleaf]: Thymeleaf template engine.
5555

5656
=== Security
57-
* link:/modules/jasypt[Jasypt]: Encripted configuration files.
57+
* link:/modules/jasypt[Jasypt]: Encrypted configuration files.
5858
* link:/modules/pac4j[Pac4j]: Security engine module.
5959

6060
=== Session Store
6161
* link:/modules/caffeine[Caffeine]: In-memory session store using Caffeine cache.
6262
* link:/modules/jwt-session-store[JWT]: JSON Web Token session store.
6363
* link:/modules/redis#redis-http-session[Redis]: Save session data on redis.
6464

65-
=== Misc
66-
* link:/modules/node[Node]: Node integration module.
65+
=== Scheduler
66+
* link:/modules/db-scheduler[DbScheduler]: Db scheduler module.
6767
* link:/modules/quartz[Quartz]: Quartz scheduler module.
6868

6969
.

docs/asciidoc/templates.adoc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ Templates are available via javadoc:ModelAndView[] and requires a javadoc:Templa
66
[source, java, role = "primary"]
77
----
88
{
9-
install(new MyTemplateEngineModule()); <1>
9+
install(new MyTemplateEngineModule()); <1>
1010
1111
get("/", ctx -> {
12-
MyModel model = ...; <2>
12+
MyModel model = ...; <2>
1313
return new ModelAndView("index.html", model); <3>
1414
});
1515
}
@@ -22,8 +22,8 @@ Templates are available via javadoc:ModelAndView[] and requires a javadoc:Templa
2222
install(MyTemplateEngineModule()) <1>
2323
2424
get("/") { ctx ->
25-
val model = MyModel(...) <2>
26-
ModelAndView("index.html", model) <3>
25+
val model = MyModel(...) <2>
26+
ModelAndView("index.html", model) <3>
2727
}
2828
}
2929
----

docs/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<dependency>
3434
<groupId>me.tongfei</groupId>
3535
<artifactId>progressbar</artifactId>
36-
<version>0.10.0</version>
36+
<version>0.10.1</version>
3737
</dependency>
3838

3939
<dependency>
@@ -100,7 +100,7 @@
100100
<plugin>
101101
<groupId>org.codehaus.mojo</groupId>
102102
<artifactId>exec-maven-plugin</artifactId>
103-
<version>3.1.0</version>
103+
<version>3.4.1</version>
104104
<executions>
105105
<execution>
106106
<id>asciidoctor</id>

docs/src/main/java/io/jooby/adoc/DocGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static void generate(Path basedir, boolean publish, boolean v1, boolean d
6262
int adocCount =
6363
Stream.of(treeDirs)
6464
.map(throwingFunction(dir -> countAdoc(asciidoc.resolve(dir))))
65-
.reduce(1, (a, b) -> a + b);
65+
.reduce(1, Integer::sum);
6666
int steps = 7 + (doAscii ? adocCount : 0);
6767

6868
ProgressBarBuilder pbb =
@@ -71,7 +71,7 @@ public static void generate(Path basedir, boolean publish, boolean v1, boolean d
7171
.setInitialMax(steps)
7272
.setTaskName("Building Site");
7373

74-
try (ProgressBar pb = pbb.build()) {
74+
try (var pb = pbb.build()) {
7575

7676
Path outdir = asciidoc.resolve("site");
7777
if (!Files.exists(outdir)) {

modules/jooby-bom/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@
6969
<artifactId>jooby-conscrypt</artifactId>
7070
<version>${project.version}</version>
7171
</dependency>
72+
<dependency>
73+
<groupId>io.jooby</groupId>
74+
<artifactId>jooby-db-scheduler</artifactId>
75+
<version>${project.version}</version>
76+
</dependency>
7277
<dependency>
7378
<groupId>io.jooby</groupId>
7479
<artifactId>jooby-distribution</artifactId>

modules/jooby-db-scheduler/pom.xml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
5+
6+
<parent>
7+
<groupId>io.jooby</groupId>
8+
<artifactId>modules</artifactId>
9+
<version>3.2.10-SNAPSHOT</version>
10+
</parent>
11+
12+
<modelVersion>4.0.0</modelVersion>
13+
<artifactId>jooby-db-scheduler</artifactId>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>io.jooby</groupId>
18+
<artifactId>jooby</artifactId>
19+
<version>${jooby.version}</version>
20+
</dependency>
21+
22+
<!-- https://mvnrepository.com/artifact/com.github.kagkarlsson/db-scheduler -->
23+
<dependency>
24+
<groupId>com.github.kagkarlsson</groupId>
25+
<artifactId>db-scheduler</artifactId>
26+
<version>14.0.3</version>
27+
</dependency>
28+
29+
<!-- Test dependencies -->
30+
<dependency>
31+
<groupId>org.junit.jupiter</groupId>
32+
<artifactId>junit-jupiter-engine</artifactId>
33+
<scope>test</scope>
34+
</dependency>
35+
36+
<dependency>
37+
<groupId>org.jacoco</groupId>
38+
<artifactId>org.jacoco.agent</artifactId>
39+
<classifier>runtime</classifier>
40+
<scope>test</scope>
41+
</dependency>
42+
</dependencies>
43+
</project>

0 commit comments

Comments
 (0)