Skip to content

Commit efa23e1

Browse files
authored
Run-Now in the UI as button (#2)
- Run now button in the UI - Offline Schedulers are deleted from the registry - testing mssql server automatically - DB change to support MS for durations
1 parent 0b38705 commit efa23e1

File tree

30 files changed

+3032
-657
lines changed

30 files changed

+3032
-657
lines changed

.github/workflows/build.yml

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,34 @@ jobs:
2828
- name: PMD with Maven
2929
run: mvn pmd:pmd --file pom.xml
3030

31+
test-mssql:
32+
runs-on: ubuntu-latest
33+
needs: build
34+
services:
35+
sql-edge:
36+
image: mcr.microsoft.com/azure-sql-edge
37+
options: --cap-add SYS_PTRACE --name azuresqledge
38+
env:
39+
ACCEPT_EULA: Y
40+
MSSQL_SA_PASSWORD: "veryStrong123"
41+
ports:
42+
- 1433:1433
43+
44+
steps:
45+
- uses: actions/checkout@v4
46+
- name: Set up JDK 21
47+
uses: actions/setup-java@v4
48+
with:
49+
java-version: '21'
50+
distribution: 'temurin'
51+
cache: maven
52+
- name: Run Tests with MSSQL
53+
run: mvn test -pl core -am -Dspring.profiles.active=mssql
54+
55+
3156
java-doc:
3257
runs-on: ubuntu-latest
58+
needs: [build]
3359
if: ${{ github.ref == 'refs/heads/main' }}
3460
permissions:
3561
contents: write # if you have a protection rule on your repository, you'll need to give write permission to the workflow.
@@ -56,9 +82,9 @@ jobs:
5682
target-folder: javadoc-core # url will be https://<username>.github.io/<repo>/javadoc-core
5783
project: maven # or gradle
5884

59-
deploy:
85+
maven-deploy:
6086
runs-on: ubuntu-latest
61-
needs: [build]
87+
needs: [build, test-mssql]
6288
if: ${{ github.ref == 'refs/heads/main' }}
6389
steps:
6490
- uses: actions/checkout@v4

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## v1.1.1
4+
5+
- Run now button in the UI
6+
- Offline Schedulers are deleted from the registry
7+
38
## v1.1.0 - (2024-12-30)
49

510
- Showing trigger history entries

README.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,21 @@ Focus is the usage with spring boot and JPA.
88

99
Secondary goal is to support [Poor mans Workflow](https://github.com/sterlp/pmw)
1010

11+
# DBs for storage
12+
13+
## Tested in the pipeline
14+
15+
- H2
16+
- azure-sql-edge (MSSQL)
17+
18+
## Supported in theory
19+
20+
- MSSQL
21+
- mySQL
22+
- PostgreSQL
23+
- mySQL
24+
- MariaDB
25+
1126
# Setup and Run a Task
1227

1328
- [JavaDoc](https://sterlp.github.io/spring-persistent-tasks/javadoc-core/org/sterl/spring/persistent_tasks/PersistentTaskService.html)
@@ -198,20 +213,21 @@ Now the `PersistentTaskService` has a method to trigger or to trigger and to wai
198213
}
199214
```
200215

201-
During the setup and cleanup it is possible to cancel any pending stuff:
216+
During the setup and cleanup it is possible to cancel any pending triggers:
202217

203218
```java
204219
@BeforeEach
205220
public void beforeEach() throws Exception {
206221
triggerService.deleteAll();
207222
historyService.deleteAll();
208-
schedulerA.setMaxThreads(10);
223+
schedulerService.setMaxThreads(10);
209224
schedulerService.start();
210225
}
211226

212227
@AfterEach
213228
public void afterEach() throws Exception {
214-
schedulerService.stop();
229+
// will cancel any pending tasks
230+
schedulerService.shutdownNow(); // use .stop() if you want to wait
215231
}
216232
```
217233

core/src/main/java/org/sterl/spring/persistent_tasks/api/AddTriggerRequest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public record AddTriggerRequest<T extends Serializable>(
1414
OffsetDateTime runtAt,
1515
int priority) {
1616

17+
@SuppressWarnings("unchecked")
1718
public TaskId<T> taskId() {
1819
return (TaskId<T>)key.toTaskId();
1920
}

core/src/main/java/org/sterl/spring/persistent_tasks/history/api/TriggerHistoryResource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public List<Trigger> listInstances(@PathVariable("instanceId") long instanceId)
3232

3333
@GetMapping("history")
3434
public PagedModel<Trigger> list(
35-
@PageableDefault(size = 100, direction = Direction.ASC, sort = "data.runAt") Pageable pageable) {
35+
@PageableDefault(size = 100, direction = Direction.DESC, sort = "id") Pageable pageable) {
3636

3737
return FromLastTriggerStateEntity.INSTANCE.toPage( //
3838
historyService.findTriggerState(null, pageable));

core/src/main/java/org/sterl/spring/persistent_tasks/scheduler/SchedulerService.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,15 @@ public void setMaxThreads(int value) {
5858
@PreDestroy
5959
public void stop() {
6060
taskExecutor.close();
61-
var s = editSchedulerStatus.checkinToRegistry(name);
62-
log.info("Stopped {}", s);
61+
editSchedulerStatus.offline(name);
62+
log.info("Stopped {}", name);
6363
}
6464

6565
public void shutdownNow() {
66+
var running = taskExecutor.getRunningTasks();
6667
taskExecutor.shutdownNow();
67-
var s = editSchedulerStatus.checkinToRegistry(name);
68-
log.info("Force stop {}", s);
68+
log.info("Force stop {} with {} running tasks", name, running);
69+
editSchedulerStatus.offline(name);
6970
}
7071

7172
public SchedulerEntity pingRegistry() {

core/src/main/java/org/sterl/spring/persistent_tasks/scheduler/component/EditSchedulerStatusComponent.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public SchedulerEntity checkinToRegistry(String name) {
3232
result.setLastPing(OffsetDateTime.now());
3333
return schedulerRepository.save(result);
3434
}
35+
36+
public void offline(String name) {
37+
schedulerRepository.deleteById(name);
38+
}
3539

3640
public SchedulerEntity get(String name) {
3741
return schedulerRepository.findById(name)

core/src/main/java/org/sterl/spring/persistent_tasks/scheduler/component/TaskExecutorComponent.java

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
import java.util.concurrent.atomic.AtomicInteger;
1616

1717
import org.springframework.lang.NonNull;
18+
import org.springframework.lang.Nullable;
1819
import org.sterl.spring.persistent_tasks.api.TriggerKey;
1920
import org.sterl.spring.persistent_tasks.trigger.TriggerService;
2021
import org.sterl.spring.persistent_tasks.trigger.model.TriggerEntity;
2122

22-
import jakarta.annotation.Nullable;
2323
import jakarta.annotation.PostConstruct;
2424
import jakarta.annotation.PreDestroy;
2525
import lombok.Getter;
@@ -34,6 +34,7 @@ public class TaskExecutorComponent implements Closeable {
3434
@Getter
3535
@Setter
3636
private Duration maxShutdownWaitTime = Duration.ofSeconds(10);
37+
@Nullable
3738
private ExecutorService executor;
3839
private final ConcurrentHashMap<TriggerEntity, Future<TriggerKey>> runningTasks = new ConcurrentHashMap<>();
3940
private final AtomicBoolean stopped = new AtomicBoolean(true);
@@ -58,6 +59,8 @@ public Future<TriggerKey> submit(@Nullable TriggerEntity trigger) {
5859
if (trigger == null) {
5960
return CompletableFuture.completedFuture(null);
6061
}
62+
if (stopped.get()) throw new IllegalStateException("Executor is already stopped");
63+
6164
final var result = executor.submit(() -> runTrigger(trigger));
6265
runningTasks.put(trigger, result);
6366
return result;
@@ -72,6 +75,7 @@ private TriggerKey runTrigger(TriggerEntity trigger) {
7275
}
7376
}
7477

78+
@SuppressWarnings("resource")
7579
@PostConstruct
7680
public void start() {
7781
if (stopped.compareAndExchange(true, false)) {
@@ -87,33 +91,37 @@ public void start() {
8791
public void close() {
8892
if (stopped.compareAndExchange(false, true)) {
8993
synchronized (stopped) {
90-
if (executor != null) {
91-
executor.shutdown();
92-
waitForRunningTasks();
93-
executor = null;
94-
}
94+
doShutdown();
9595
}
9696
}
9797
}
9898

99-
private void waitForRunningTasks() {
100-
if (runningTasks.size() > 0) {
101-
log.info("Shutdown executor with {} running tasks, waiting for {}.",
102-
runningTasks.size(), maxShutdownWaitTime);
103-
104-
try {
105-
executor.awaitTermination(maxShutdownWaitTime.getSeconds(), TimeUnit.SECONDS);
106-
} catch (InterruptedException e) {
107-
log.warn("Failed to complete runnings tasks.", e.getCause());
108-
} finally {
109-
executor.shutdownNow();
99+
private void doShutdown() {
100+
if (executor != null) {
101+
executor.shutdown();
102+
if (runningTasks.size() > 0) {
103+
log.info("Shutdown executor with {} running tasks, waiting for {}.",
104+
runningTasks.size(), maxShutdownWaitTime);
105+
106+
try {
107+
executor.awaitTermination(maxShutdownWaitTime.getSeconds(), TimeUnit.SECONDS);
108+
} catch (InterruptedException e) {
109+
log.warn("Failed to complete runnings tasks.", e.getCause());
110+
shutdownNow();
111+
} finally {
112+
executor = null;
113+
runningTasks.clear();
114+
}
115+
} else {
116+
executor = null;
110117
}
111118
}
112119
}
113120

114121
public void shutdownNow() {
115122
stopped.set(true);
116-
executor.shutdownNow();
123+
if (executor != null) executor.shutdownNow();
124+
executor = null;
117125
}
118126

119127
public int getFreeThreads() {

core/src/main/java/org/sterl/spring/persistent_tasks/trigger/api/TriggerResource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public long count() {
3838
@GetMapping("triggers")
3939
public PagedModel<Trigger> list(
4040
@RequestParam(name = "taskId", required = false) String taskId,
41-
@PageableDefault(size = 100, direction = Direction.ASC, sort = "data.runAt")
41+
@PageableDefault(size = 100, direction = Direction.DESC, sort = "id")
4242
Pageable pageable) {
4343
return FromTriggerEntity.INSTANCE.toPage(
4444
triggerService.findAllTriggers(TaskId.of(taskId), pageable));

core/src/test/java/org/sterl/spring/persistent_tasks/AbstractSpringTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ public void beforeEach() throws Exception {
157157

158158
@AfterEach
159159
public void afterEach() throws Exception {
160-
schedulerA.stop();
161-
schedulerB.stop();
160+
schedulerA.shutdownNow();
161+
schedulerB.shutdownNow();
162162
triggerService.deleteAll();
163163
historyService.deleteAll();
164164
}

0 commit comments

Comments
 (0)