wmedvede commented on code in PR #1888:
URL:
https://github.com/apache/incubator-kie-kogito-apps/pull/1888#discussion_r1341445092
##########
jobs-service/jobs-service-common/src/main/java/org/kie/kogito/jobs/service/scheduler/JobSchedulerManager.java:
##########
@@ -73,26 +74,49 @@ public class JobSchedulerManager {
@Inject
Vertx vertx;
- private AtomicBoolean enabled = new AtomicBoolean(false);
+ final AtomicBoolean enabled = new AtomicBoolean(false);
+
+ final AtomicLong periodicTimerIdForLoadJobs = new AtomicLong(-1l);
void onStartup(@Observes @Priority(Interceptor.Priority.PLATFORM_AFTER)
StartupEvent startupEvent) {
- if (loadJobIntervalInMinutes > schedulerChunkInMinutes) {
- LOGGER.warn("The loadJobIntervalInMinutes ({}) cannot be greater
than schedulerChunkInMinutes ({}), " +
- "setting value {} for both",
- loadJobIntervalInMinutes,
- schedulerChunkInMinutes,
- schedulerChunkInMinutes);
- loadJobIntervalInMinutes = schedulerChunkInMinutes;
+ if (enabled.get()) {
Review Comment:
if the enabled value an be changed only by the MessagingChangeEvent arrival,
and we are already starting the load at this point when needed, I believe we
don't need this method any more.
##########
jobs-service/jobs-service-common/src/main/java/org/kie/kogito/jobs/service/scheduler/JobSchedulerManager.java:
##########
@@ -73,26 +74,49 @@ public class JobSchedulerManager {
@Inject
Vertx vertx;
- private AtomicBoolean enabled = new AtomicBoolean(false);
+ final AtomicBoolean enabled = new AtomicBoolean(false);
+
+ final AtomicLong periodicTimerIdForLoadJobs = new AtomicLong(-1l);
void onStartup(@Observes @Priority(Interceptor.Priority.PLATFORM_AFTER)
StartupEvent startupEvent) {
- if (loadJobIntervalInMinutes > schedulerChunkInMinutes) {
- LOGGER.warn("The loadJobIntervalInMinutes ({}) cannot be greater
than schedulerChunkInMinutes ({}), " +
- "setting value {} for both",
- loadJobIntervalInMinutes,
- schedulerChunkInMinutes,
- schedulerChunkInMinutes);
- loadJobIntervalInMinutes = schedulerChunkInMinutes;
+ if (enabled.get()) {
+ startJobsLoadingFromRepositoryTask();
}
+ }
- //first execution
- vertx.runOnContext(this::loadJobDetails);
- //periodic execution
- vertx.setPeriodic(TimeUnit.MINUTES.toMillis(loadJobIntervalInMinutes),
id -> loadJobDetails());
+ private void startJobsLoadingFromRepositoryTask() {
+ //guarantee it starts the task just in case it is not already active
+ if (periodicTimerIdForLoadJobs.get() < 0) {
+ if (loadJobIntervalInMinutes > schedulerChunkInMinutes) {
+ LOGGER.warn("The loadJobIntervalInMinutes ({}) cannot be
greater than schedulerChunkInMinutes ({}), " +
+ "setting value {} for both",
+ loadJobIntervalInMinutes,
+ schedulerChunkInMinutes,
+ schedulerChunkInMinutes);
+ loadJobIntervalInMinutes = schedulerChunkInMinutes;
+ }
+ //first execution
+ vertx.runOnContext(this::loadJobDetails);
+ //next executions to run periodically
+
periodicTimerIdForLoadJobs.set(vertx.setPeriodic(TimeUnit.MINUTES.toMillis(loadJobIntervalInMinutes),
id -> loadJobDetails()));
+ }
}
- protected void onMessagingStatusChange(@Observes MessagingChangeEvent
event) {
- this.enabled.set(event.isEnabled());
+ private void cancelJobsLoadingFromRepositoryTask() {
+ if (periodicTimerIdForLoadJobs.get() > 0) {
+ vertx.cancelTimer(periodicTimerIdForLoadJobs.get());
+ //negative id indicates this is not active anymore
+ periodicTimerIdForLoadJobs.set(-1);
+ }
+ }
+
+ protected synchronized void onMessagingStatusChange(@Observes
MessagingChangeEvent event) {
Review Comment:
maybe here we can do this:
if (enabled.get() && !wasEnabled) {
// good, avoid starting twice if we receive two consecutive
enabled = true
startJobsLoadingFromRepositoryTask();
} else if (!enabled.get()) {
// but only cancel if we receive enabled = false, otherwise with
two consecutive enable we are also cancelling.
cancelJobsLoadingFromRepositoryTask();
}
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]