You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by GitBox <gi...@apache.org> on 2022/09/28 08:49:16 UTC

[GitHub] [james-project] quantranhong1999 commented on a diff in pull request #1208: JAMES-3825 Task to clean up tasks

quantranhong1999 commented on code in PR #1208:
URL: https://github.com/apache/james-project/pull/1208#discussion_r982111701


##########
server/task/task-distributed/src/main/scala/org/apache/james/task/eventsourcing/cassandra/CassandraTaskExecutionDetailsProjection.scala:
##########
@@ -45,4 +46,8 @@ class CassandraTaskExecutionDetailsProjection @Inject()(cassandraTaskExecutionDe
   override def listReactive(): Publisher[TaskExecutionDetails] = cassandraTaskExecutionDetailsProjectionDAO.listDetails()
 
   override def updateReactive(details: TaskExecutionDetails): Publisher[Void] = cassandraTaskExecutionDetailsProjectionDAO.saveDetails(details)
+
+  override def listDetailsByBeforeDate(beforeDate: Instant): Publisher[TaskExecutionDetails] = cassandraTaskExecutionDetailsProjectionDAO.listDetailsByBeforeDate(beforeDate)
+
+  override def remove(taskExecutionDetails: TaskExecutionDetails): Publisher[Void] = ???

Review Comment:
   ```suggestion
     override def remove(taskExecutionDetails: TaskExecutionDetails): Publisher[Void] = cassandraTaskExecutionDetailsProjectionDAO.remove(taskExecutionDetails)
   ```



##########
server/protocols/webadmin/webadmin-cassandra/src/main/java/org/apache/james/webadmin/services/TasksCleanupService.java:
##########
@@ -0,0 +1,113 @@
+/****************************************************************
+ * 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.james.webadmin.services;
+
+import java.time.Instant;
+import java.util.concurrent.atomic.AtomicLong;
+
+import javax.inject.Inject;
+
+import org.apache.james.eventsourcing.eventstore.EventStore;
+import org.apache.james.task.Task;
+import org.apache.james.task.eventsourcing.TaskAggregateId;
+import org.apache.james.task.eventsourcing.TaskExecutionDetailsProjection;
+import org.apache.james.util.ReactorUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+public class TasksCleanupService {
+    private static final Logger LOGGER = LoggerFactory.getLogger(TasksCleanupService.class);
+
+    public static class Context {
+
+        public static class Snapshot {
+            private final long removedTasksCount;
+            private final long processedTaskCount;
+
+            public Snapshot(long removedTasksCount, long processedTaskCount) {
+                this.removedTasksCount = removedTasksCount;
+                this.processedTaskCount = processedTaskCount;
+            }
+
+            public long getRemovedTasksCount() {
+                return removedTasksCount;
+            }
+
+            public long getProcessedTaskCount() {
+                return processedTaskCount;
+            }
+        }
+
+        private final AtomicLong removedTasksCount;
+        private final AtomicLong processedTaskCount;
+
+        public Context() {
+            removedTasksCount = new AtomicLong();
+            processedTaskCount = new AtomicLong();
+        }
+
+        void incrementRemovedTasksCount() {
+            removedTasksCount.incrementAndGet();
+        }
+
+        void incrementProcessedTaskCount() {
+            processedTaskCount.incrementAndGet();
+        }
+
+        void incrementRemovedTasksCount(int count) {
+            removedTasksCount.set(removedTasksCount.get() + count);
+        }
+
+        public Snapshot snapshot() {
+            return new Snapshot(removedTasksCount.get(), processedTaskCount.get());
+        }
+    }
+
+    private final EventStore eventStore;
+    private final TaskExecutionDetailsProjection taskExecutionDetailsProjection;
+
+    @Inject
+    public TasksCleanupService(EventStore eventStore, TaskExecutionDetailsProjection taskExecutionDetailsProjection) {
+        this.eventStore = eventStore;
+        this.taskExecutionDetailsProjection = taskExecutionDetailsProjection;
+    }
+
+    public Mono<Task.Result> removeBeforeDate(Instant beforeDate, Context context) {
+        return Flux.from(taskExecutionDetailsProjection.listDetailsByBeforeDate(beforeDate))
+            .doOnNext(oldTaskDetail -> context.incrementProcessedTaskCount())

Review Comment:
   What if that task is in progress?



##########
server/task/task-distributed/src/test/java/org/apache/james/task/eventsourcing/cassandra/CassandraTaskExecutionDetailsProjectionDAOTest.java:
##########
@@ -111,4 +120,65 @@ void listDetailsShouldReturnLastUpdatedRecords() {
         Stream<TaskExecutionDetails> taskExecutionDetails = testee.listDetails().toStream();
         assertThat(taskExecutionDetails).containsOnly(TASK_EXECUTION_DETAILS_UPDATED());
     }
+
+    @Test
+    void listBeforeDateShouldReturnCorrectEntry() {
+        TaskExecutionDetails taskExecutionDetails1 = new TaskExecutionDetails(TASK_ID(),
+            TaskType.of("type"),
+            TaskManager.Status.COMPLETED,
+            ZonedDateTime.ofInstant(Instant.parse("2000-01-01T00:00:00Z"), ZoneId.systemDefault()),
+            TaskExecutionDetailsFixture.SUBMITTED_NODE(),
+            Optional::empty,
+            Optional.empty(),
+            Optional.empty(),
+            Optional.empty(),
+            Optional.empty(),
+            Optional.empty(),
+            Optional.empty());
+
+        TaskExecutionDetails taskExecutionDetails2 = new TaskExecutionDetails(TASK_ID_2(),
+            TaskType.of("type"),
+            TaskManager.Status.COMPLETED,
+            ZonedDateTime.ofInstant(Instant.parse("2000-01-20T00:00:00Z"), ZoneId.systemDefault()),
+            TaskExecutionDetailsFixture.SUBMITTED_NODE(),
+            Optional::empty,
+            Optional.empty(),
+            Optional.empty(),
+            Optional.empty(),
+            Optional.empty(),
+            Optional.empty(),
+            Optional.empty());
+
+        testee.saveDetails(taskExecutionDetails1).block();
+        testee.saveDetails(taskExecutionDetails2).block();
+
+        assertThat(Flux.from(testee.listDetailsByBeforeDate(Instant.parse("2000-01-15T12:00:55Z"))).collectList().block())
+            .containsOnly(taskExecutionDetails1);
+    }
+
+    @Test
+    void removeShouldDeleteAssignEntry() {
+        TaskExecutionDetails taskExecutionDetails1 = new TaskExecutionDetails(TASK_ID(),
+            TaskType.of("type"),
+            TaskManager.Status.COMPLETED,

Review Comment:
   Q: What if we remove an in-progress task?



-- 
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: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org