-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskSchedulingServiceImpl.java
More file actions
82 lines (71 loc) · 2.98 KB
/
TaskSchedulingServiceImpl.java
File metadata and controls
82 lines (71 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.app.service;
import com.app.model.ScheduleTask;
import com.app.repository.ScheduleTaskRepository;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import net.javacrumbs.shedlock.core.LockAssert;
import net.javacrumbs.shedlock.core.LockConfiguration;
import net.javacrumbs.shedlock.core.LockProvider;
import net.javacrumbs.shedlock.core.SimpleLock;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Service;
import java.time.Duration;
import java.time.Instant;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;
@Service
@RequiredArgsConstructor
@Log4j2
public class TaskSchedulingServiceImpl implements TaskSchedulingService {
private final TaskScheduler taskScheduler;
private final LockProvider lockProvider;
private final ScheduleTaskRepository scheduleTaskRepository;
private final Map<Long, ScheduledFuture<?>> tasks = new ConcurrentHashMap<>();
@PostConstruct
public void initializeScheduledTasks() {
scheduleTaskRepository.findAllByIsActiveTrue().forEach(this::scheduleTask);
}
@Override
public void scheduleTask(ScheduleTask scheduleTask) {
Runnable taskWrapper = () -> {
String lockName = scheduleTask.getName()+" - "+scheduleTask.getId();
Instant createdAt = Instant.now();
LockConfiguration config = new LockConfiguration(createdAt, lockName, Duration.ofMinutes(1), Duration.ofSeconds(10));
Optional<SimpleLock> lock = lockProvider.lock(config);
LockAssert.TestHelper.makeAllAssertsPass(true);
try {
lock.ifPresent(simpleLock -> {
try {
LockAssert.assertLocked();
// Execute the actual task logic here
log.info("Executing Task {}", lockName);
} catch (Exception e) {
log.error("Error executing task: {}", scheduleTask.getId(), e);
}
});
} finally {
lock.ifPresent(SimpleLock::unlock);
log.info("Release Task {}", lockName);
}
};
ScheduledFuture<?> future = taskScheduler.schedule(taskWrapper, new CronTrigger(scheduleTask.getCustomScheduleDetails()));
tasks.put(scheduleTask.getId(), future);
}
@Override
public void cancelScheduledTask(Long taskId) {
ScheduledFuture<?> future = tasks.get(taskId);
if (future != null) {
future.cancel(false);
tasks.remove(taskId);
}
}
@Override
public void addOrUpdateTask(ScheduleTask scheduleTask) {
cancelScheduledTask(scheduleTask.getId()); // Cancel the current task if it's already scheduled
scheduleTask(scheduleTask); // Reschedule it
}
}