You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@causeway.apache.org by da...@apache.org on 2023/09/28 10:44:14 UTC

[causeway] branch CAUSEWAY-3616 created (now c536007fb9)

This is an automated email from the ASF dual-hosted git repository.

danhaywood pushed a change to branch CAUSEWAY-3616
in repository https://gitbox.apache.org/repos/asf/causeway.git


      at c536007fb9 CAUSEWAY-3616: moves background job control from the job and into a singleton service

This branch includes the following new commits:

     new c536007fb9 CAUSEWAY-3616: moves background job control from the job and into a singleton service

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[causeway] 01/01: CAUSEWAY-3616: moves background job control from the job and into a singleton service

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

danhaywood pushed a commit to branch CAUSEWAY-3616
in repository https://gitbox.apache.org/repos/asf/causeway.git

commit c536007fb92118771913419b7159209acb3ca31a
Author: danhaywood <da...@haywood-associates.co.uk>
AuthorDate: Thu Sep 28 11:44:08 2023 +0100

    CAUSEWAY-3616: moves background job control from the job and into a singleton service
---
 .../applib/CausewayModuleExtCommandLogApplib.java  |  4 +
 .../applib/job/BackgroundCommandsJobControl.java   | 95 ++++++++++++++++++++++
 .../applib/job/RunBackgroundCommandsJob.java       | 28 +++----
 3 files changed, 109 insertions(+), 18 deletions(-)

diff --git a/extensions/core/commandlog/applib/src/main/java/org/apache/causeway/extensions/commandlog/applib/CausewayModuleExtCommandLogApplib.java b/extensions/core/commandlog/applib/src/main/java/org/apache/causeway/extensions/commandlog/applib/CausewayModuleExtCommandLogApplib.java
index 8fc490cf90..e0b3b0d4f3 100644
--- a/extensions/core/commandlog/applib/src/main/java/org/apache/causeway/extensions/commandlog/applib/CausewayModuleExtCommandLogApplib.java
+++ b/extensions/core/commandlog/applib/src/main/java/org/apache/causeway/extensions/commandlog/applib/CausewayModuleExtCommandLogApplib.java
@@ -18,6 +18,8 @@
  */
 package org.apache.causeway.extensions.commandlog.applib;
 
+import org.apache.causeway.extensions.commandlog.applib.job.BackgroundCommandsJobControl;
+
 import org.springframework.context.annotation.Configuration;
 import org.springframework.context.annotation.Import;
 
@@ -55,6 +57,8 @@ import org.apache.causeway.extensions.commandlog.applib.subscriber.CommandSubscr
         CommandSubscriberForCommandLog.class,
         CommandLogEntry.TableColumnOrderDefault.class,
 
+        BackgroundCommandsJobControl.class,
+
         BackgroundService.class,
         BackgroundService.PersistCommandExecutorService.class,
 
diff --git a/extensions/core/commandlog/applib/src/main/java/org/apache/causeway/extensions/commandlog/applib/job/BackgroundCommandsJobControl.java b/extensions/core/commandlog/applib/src/main/java/org/apache/causeway/extensions/commandlog/applib/job/BackgroundCommandsJobControl.java
new file mode 100644
index 0000000000..b0584172b6
--- /dev/null
+++ b/extensions/core/commandlog/applib/src/main/java/org/apache/causeway/extensions/commandlog/applib/job/BackgroundCommandsJobControl.java
@@ -0,0 +1,95 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.causeway.extensions.commandlog.applib.job;
+
+import lombok.Getter;
+import lombok.extern.log4j.Log4j2;
+import lombok.val;
+
+import java.util.List;
+import java.util.Optional;
+import java.util.UUID;
+import java.util.stream.Collectors;
+
+import javax.inject.Inject;
+
+import org.apache.causeway.applib.annotation.Programmatic;
+import org.apache.causeway.applib.services.clock.ClockService;
+import org.apache.causeway.applib.services.command.CommandExecutorService;
+import org.apache.causeway.applib.services.iactnlayer.InteractionContext;
+import org.apache.causeway.applib.services.iactnlayer.InteractionService;
+import org.apache.causeway.applib.services.user.UserMemento;
+import org.apache.causeway.applib.services.xactn.TransactionService;
+import org.apache.causeway.applib.util.schema.CommandDtoUtils;
+import org.apache.causeway.extensions.commandlog.applib.dom.CommandLogEntry;
+import org.apache.causeway.extensions.commandlog.applib.dom.CommandLogEntryRepository;
+import org.apache.causeway.schema.cmd.v2.CommandDto;
+
+import org.quartz.DisallowConcurrentExecution;
+import org.quartz.Job;
+import org.quartz.JobExecutionContext;
+import org.quartz.PersistJobDataAfterExecution;
+
+import org.springframework.stereotype.Component;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Propagation;
+
+/**
+ * Is checked by {@link RunBackgroundCommandsJob} each time it is executed.
+ *
+ * <p>
+ *     A typical way to leverage this service would be to implement 'pause' and 'resume' actions on an admin-only
+ *     dashboard.
+ * </p>
+ *
+ * @see RunBackgroundCommandsJob
+ */
+@Service
+@Log4j2
+public class BackgroundCommandsJobControl {
+
+    public enum State {
+        RUNNING,
+        PAUSED
+    }
+
+    @Getter
+    private State state = State.RUNNING;
+
+    @Programmatic
+    public void pause() {
+        log.info("Paused");
+        state = State.PAUSED;
+    }
+    @Programmatic
+    public void resume() {
+        log.info("Running");
+        state = State.RUNNING;
+    }
+
+    @Programmatic
+    public boolean isPaused() {
+        return state == State.PAUSED;
+    }
+    @Programmatic
+    public boolean isRunning() {
+        return state == State.RUNNING;
+    }
+
+}
diff --git a/extensions/core/commandlog/applib/src/main/java/org/apache/causeway/extensions/commandlog/applib/job/RunBackgroundCommandsJob.java b/extensions/core/commandlog/applib/src/main/java/org/apache/causeway/extensions/commandlog/applib/job/RunBackgroundCommandsJob.java
index ed55916c44..5deef9000e 100644
--- a/extensions/core/commandlog/applib/src/main/java/org/apache/causeway/extensions/commandlog/applib/job/RunBackgroundCommandsJob.java
+++ b/extensions/core/commandlog/applib/src/main/java/org/apache/causeway/extensions/commandlog/applib/job/RunBackgroundCommandsJob.java
@@ -55,6 +55,13 @@ import lombok.extern.log4j.Log4j2;
  * the {@link org.apache.causeway.extensions.commandlog.applib.dom.BackgroundService} but not yet started; and then
  * executes them.
  *
+ * <p>
+ *     Note that although this is a component, a new instance is created for each run.  It is for this reason that
+ *     the control is managed through the injected {@link BackgroundCommandsJobControl}
+ * </p>
+ *
+ * @see BackgroundCommandsJobControl
+ *
  * @since 2.0 {@index}
  */
 @Component
@@ -68,29 +75,14 @@ public class RunBackgroundCommandsJob implements Job {
     @Inject ClockService clockService;
     @Inject CommandLogEntryRepository<? extends CommandLogEntry> commandLogEntryRepository;
     @Inject CommandExecutorService commandExecutorService;
+    @Inject BackgroundCommandsJobControl backgroundCommandsJobControl;
 
-    public enum State {
-        RUNNING,
-        PAUSED
-    }
-
-    @Getter
-    private State state = State.RUNNING;
-
-    @Programmatic
-    public void pause() {
-        state = State.PAUSED;
-    }
-    @Programmatic
-    public void resume() {
-        state = State.RUNNING;
-    }
 
     @Override
     public void execute(final JobExecutionContext quartzContext) {
 
-        if (state == State.PAUSED) {
-            log.debug("state is " + state);
+        if (backgroundCommandsJobControl.isPaused()) {
+            log.debug("currently paused");
             return;
         }