Skip to content

Commit ab9108b

Browse files
authored
Merge pull request #11 from sterlp/build-v1.6.1
Build v1.6.1
2 parents 433d888 + 471ea70 commit ab9108b

37 files changed

+533
-233
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# Changelog
22

3-
## v1.6
3+
## v1.6.1
4+
- simpler RetryStrategy - as function
5+
- showing last ping
6+
- showing execution time or still running triggers
7+
- saver way to keep track of running triggers
8+
9+
## v1.6.0 - (2025-03-11)
410

511
- Running triggers can be canceled now
612
- Running triggers can be failed now

core/src/main/java/org/sterl/spring/persistent_tasks/PersistentTaskService.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
import java.util.Optional;
99

1010
import org.springframework.context.event.EventListener;
11+
import org.springframework.data.domain.PageRequest;
1112
import org.springframework.data.domain.Pageable;
13+
import org.springframework.data.domain.Sort;
14+
import org.springframework.data.domain.Sort.Direction;
1215
import org.springframework.lang.NonNull;
1316
import org.springframework.stereotype.Service;
1417
import org.springframework.transaction.annotation.Transactional;
@@ -122,24 +125,46 @@ public <T extends Serializable> TriggerKey runOrQueue(
122125

123126
/**
124127
* Returns all triggers for a correlationId sorted by the creation time.
128+
* Data is limited to overall 300 elements.
129+
*
125130
* @param correlationId the id to search for
126131
* @return the found {@link TriggerData} sorted by create time ASC
127132
*/
128133
@Transactional(readOnly = true, timeout = 5)
129134
public List<TriggerData> findAllTriggerByCorrelationId(String correlationId) {
130135

131-
var running = triggerService.findTriggerByCorrelationId(correlationId)
136+
var running = triggerService.findTriggerByCorrelationId(correlationId, Pageable.ofSize(100))
132137
.stream().map(TriggerEntity::getData)
133138
.toList();
134139

135-
var done = historyService.findTriggerByCorrelationId(correlationId)
140+
var done = historyService.findTriggerByCorrelationId(correlationId, Pageable.ofSize(200))
136141
.stream().map(TriggerHistoryLastStateEntity::getData)
137142
.toList();
138143

139-
140144
var result = new ArrayList<TriggerData>(running.size() + done.size());
141145
result.addAll(done);
142146
result.addAll(running);
143147
return result;
144148
}
149+
150+
/**
151+
* Returns the first info to a trigger based on the correlationId.
152+
*
153+
* @param correlationId the id to search for
154+
* @return the found {@link TriggerData}
155+
*/
156+
@Transactional(readOnly = true, timeout = 5)
157+
public Optional<TriggerData> findLastTriggerByCorrelationId(String correlationId) {
158+
final var page = PageRequest.of(0, 1, Sort.by(Direction.DESC, "data.createdTime"));
159+
var result = triggerService.findTriggerByCorrelationId(correlationId, page)
160+
.stream().map(TriggerEntity::getData)
161+
.toList();
162+
163+
if (result.isEmpty()) {
164+
result = historyService.findTriggerByCorrelationId(correlationId, page)
165+
.stream().map(TriggerHistoryLastStateEntity::getData)
166+
.toList();
167+
}
168+
return result.isEmpty() ? Optional.empty() : Optional.of(result.getFirst());
169+
}
145170
}

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
@FunctionalInterface
1212
public interface RetryStrategy {
13-
RetryStrategy NO_RETRY = (c, e) -> false;
13+
RetryStrategy NO_RETRY = (c, e) -> null;
1414
/**
1515
* One initial execution and after that we will try it 3 more times. Overall 4 executions.
1616
*/
@@ -23,27 +23,26 @@ public interface RetryStrategy {
2323

2424
/**
2525
* Determines whether a retry should be attempted based on the current
26-
* execution count and the provided exception.
26+
* execution count and the provided exception. (optional)
2727
*
2828
* @param executionCount The number of attempts already made.
2929
* @param error The exception that triggered the retry.
3030
* @return {@code true} if the current execution count is less than
3131
* the maximum execution count; {@code false} otherwise.
3232
*/
33-
boolean shouldRetry(int executionCount, @Nullable Exception error);
33+
default boolean shouldRetry(int executionCount, @Nullable Exception error) {
34+
return true;
35+
}
3436

3537
/**
3638
* Calculates the time of the next retry attempt based on the current
3739
* execution count and the provided exception.
3840
*
3941
* @param executionCount The number of attempts already made.
4042
* @param exception The exception that triggered the retry.
41-
* @return The {@link OffsetDateTime} representing the time of the next retry attempt.
43+
* @return {@link OffsetDateTime} of the next execution, <code>null</code> for no retry.
4244
*/
43-
default OffsetDateTime retryAt(int executionCount, @Nullable Exception exception) {
44-
return OffsetDateTime.now().plusMinutes(executionCount);
45-
}
46-
45+
OffsetDateTime retryAt(int executionCount, @Nullable Exception exception);
4746

4847
// Default implementations
4948
/**
@@ -70,11 +69,11 @@ class LinearRetryStrategy implements RetryStrategy {
7069
private final int offset;
7170

7271
@Override
73-
public boolean shouldRetry(int executionCount, Exception error) {
72+
public boolean shouldRetry(int executionCount, @Nullable Exception error) {
7473
return maxExecutionCount > executionCount;
7574
}
7675
@Override
77-
public OffsetDateTime retryAt(int executionCount, Exception error) {
76+
public OffsetDateTime retryAt(int executionCount, @Nullable Exception error) {
7877
return OffsetDateTime.now().plus(offset + executionCount, unit);
7978
}
8079
}
@@ -103,12 +102,14 @@ class MultiplicativeRetryStrategy implements RetryStrategy {
103102
private final int scalingFactor;
104103

105104
@Override
106-
public boolean shouldRetry(int executionCount, Exception error) {
105+
public boolean shouldRetry(int executionCount, @Nullable Exception error) {
107106
return maxExecutionCount > executionCount;
108107
}
109108
@Override
110-
public OffsetDateTime retryAt(int executionCount, Exception error) {
111-
return OffsetDateTime.now().plus(scalingFactor * executionCount, unit);
109+
public OffsetDateTime retryAt(int executionCount, @Nullable Exception error) {
110+
var next = OffsetDateTime.now();
111+
if (scalingFactor > 0 && executionCount > 0) return OffsetDateTime.now().plus(scalingFactor * executionCount, unit);
112+
return next;
112113
}
113114
}
114115

@@ -133,11 +134,11 @@ class FixedIntervalRetryStrategy implements RetryStrategy {
133134
private final int interval;
134135

135136
@Override
136-
public boolean shouldRetry(int executionCount, Exception error) {
137+
public boolean shouldRetry(int executionCount, @Nullable Exception error) {
137138
return maxExecutionCount > executionCount;
138139
}
139140
@Override
140-
public OffsetDateTime retryAt(int executionCount, Exception error) {
141+
public OffsetDateTime retryAt(int executionCount, @Nullable Exception error) {
141142
return OffsetDateTime.now().plus(interval, unit);
142143
}
143144
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public class Trigger {
2323

2424
private OffsetDateTime runAt = OffsetDateTime.now();
2525

26+
private OffsetDateTime lastPing;
27+
2628
private OffsetDateTime start;
2729

2830
private OffsetDateTime end;

core/src/main/java/org/sterl/spring/persistent_tasks/api/task/RunningTriggerContextHolder.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.io.Serializable;
44
import java.util.Objects;
5-
import java.util.UUID;
65

76
/**
87
* The {@link RunningTrigger} state will be provided by this context holder to any thread.
@@ -38,7 +37,11 @@ public static String getCorrelationId() {
3837
public static String buildOrGetCorrelationId(String newCorrelationId) {
3938
var correlationId = getCorrelationId();
4039
if (correlationId == null) correlationId = newCorrelationId;
41-
if (correlationId == null) correlationId = UUID.randomUUID().toString();
40+
// take over any key from the trigger before ...
41+
if (correlationId == null) {
42+
var c = getContext();
43+
if (c != null) correlationId = c.getKey().getId();
44+
}
4245
return correlationId;
4346
}
4447
}

core/src/main/java/org/sterl/spring/persistent_tasks/history/HistoryService.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.List;
55
import java.util.Optional;
66

7+
import org.springframework.context.ApplicationEventPublisher;
78
import org.springframework.data.domain.Page;
89
import org.springframework.data.domain.PageRequest;
910
import org.springframework.data.domain.Pageable;
@@ -13,14 +14,13 @@
1314
import org.sterl.spring.persistent_tasks.api.TaskStatusHistoryOverview;
1415
import org.sterl.spring.persistent_tasks.api.TriggerKey;
1516
import org.sterl.spring.persistent_tasks.api.TriggerStatus;
17+
import org.sterl.spring.persistent_tasks.api.event.TriggerTaskCommand;
1618
import org.sterl.spring.persistent_tasks.history.model.TriggerHistoryDetailEntity;
1719
import org.sterl.spring.persistent_tasks.history.model.TriggerHistoryLastStateEntity;
1820
import org.sterl.spring.persistent_tasks.history.repository.TriggerHistoryDetailRepository;
1921
import org.sterl.spring.persistent_tasks.history.repository.TriggerHistoryLastStateRepository;
2022
import org.sterl.spring.persistent_tasks.shared.StringHelper;
2123
import org.sterl.spring.persistent_tasks.shared.stereotype.TransactionalService;
22-
import org.sterl.spring.persistent_tasks.trigger.TriggerService;
23-
import org.sterl.spring.persistent_tasks.trigger.model.TriggerEntity;
2424

2525
import lombok.RequiredArgsConstructor;
2626

@@ -29,7 +29,7 @@
2929
public class HistoryService {
3030
private final TriggerHistoryLastStateRepository triggerHistoryLastStateRepository;
3131
private final TriggerHistoryDetailRepository triggerHistoryDetailRepository;
32-
private final TriggerService triggerService;
32+
private final ApplicationEventPublisher applicationEventPublisher;
3333

3434
public Optional<TriggerHistoryLastStateEntity> findStatus(long triggerId) {
3535
return triggerHistoryLastStateRepository.findById(triggerId);
@@ -70,7 +70,7 @@ public Page<TriggerHistoryDetailEntity> findAllDetailsForKey(TriggerKey key, Pag
7070
return triggerHistoryDetailRepository.listKnownStatusFor(key, page);
7171
}
7272

73-
public Optional<TriggerEntity> reQueue(Long id, OffsetDateTime runAt) {
73+
public Optional<TriggerKey> reQueue(Long id, OffsetDateTime runAt) {
7474
final var lastState = triggerHistoryLastStateRepository.findById(id);
7575
if (lastState.isEmpty()) return Optional.empty();
7676

@@ -81,8 +81,9 @@ public Optional<TriggerEntity> reQueue(Long id, OffsetDateTime runAt) {
8181
.priority(data.getPriority())
8282
.id(data.getKey().getId())
8383
.build();
84-
85-
return Optional.of(triggerService.queue(trigger));
84+
85+
applicationEventPublisher.publishEvent(TriggerTaskCommand.of(trigger));
86+
return Optional.of(trigger.key());
8687
}
8788

8889
public long countTriggers(TriggerKey key) {
@@ -120,7 +121,7 @@ public List<TaskStatusHistoryOverview> taskStatusHistory() {
120121
return triggerHistoryLastStateRepository.listTriggerStatus();
121122
}
122123

123-
public List<TriggerHistoryLastStateEntity> findTriggerByCorrelationId(String correlationId) {
124-
return triggerHistoryLastStateRepository.findByCorrelationId(correlationId);
124+
public List<TriggerHistoryLastStateEntity> findTriggerByCorrelationId(String correlationId, Pageable page) {
125+
return triggerHistoryLastStateRepository.findByCorrelationId(correlationId, page);
125126
}
126127
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.sterl.spring.persistent_tasks.history.HistoryService;
2222
import org.sterl.spring.persistent_tasks.history.api.HistoryConverter.FromLastTriggerStateEntity;
2323
import org.sterl.spring.persistent_tasks.history.api.HistoryConverter.FromTriggerStateDetailEntity;
24-
import org.sterl.spring.persistent_tasks.trigger.api.TriggerConverter.FromTriggerEntity;
2524

2625
import lombok.RequiredArgsConstructor;
2726

@@ -55,8 +54,8 @@ public PagedModel<Trigger> list(
5554
}
5655

5756
@PostMapping("history/{id}/re-run")
58-
public ResponseEntity<Trigger> reRunTrigger(@PathVariable(name = "id", required = true) Long id) {
57+
public ResponseEntity<TriggerKey> reRunTrigger(@PathVariable(name = "id", required = true) Long id) {
5958
var newTrigger = historyService.reQueue(id, OffsetDateTime.now());
60-
return ResponseEntity.of(FromTriggerEntity.INSTANCE.convert(newTrigger));
59+
return ResponseEntity.of(newTrigger);
6160
}
6261
}

0 commit comments

Comments
 (0)