ottoka commented on code in PR #1004:
URL: https://github.com/apache/james-project/pull/1004#discussion_r875586230


##########
server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/service/ExpireInboxesService.java:
##########
@@ -0,0 +1,209 @@
+/****************************************************************
+ * 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.service;
+
+import java.time.Duration;
+import java.time.temporal.ChronoUnit;
+import java.util.Date;
+import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.atomic.AtomicLong;
+
+import javax.inject.Inject;
+
+import org.apache.james.core.Username;
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageManager;
+import org.apache.james.mailbox.MessageUid;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.exception.MailboxNotFoundException;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.SearchQuery;
+import org.apache.james.mailbox.model.SearchQuery.DateResolution;
+import org.apache.james.task.Task;
+import org.apache.james.task.Task.Result;
+import org.apache.james.user.api.UsersRepository;
+import org.apache.james.user.api.UsersRepositoryException;
+import org.apache.james.util.DurationParser;
+import org.apache.james.util.ReactorUtils;
+import org.apache.james.util.streams.Iterators;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.common.base.Preconditions;
+
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+
+public class ExpireInboxesService {
+
+    public static class RunningOptions {
+        public static final RunningOptions DEFAULT = new RunningOptions(1, 
Optional.empty());
+
+        private final int usersPerSecond;
+
+        private final Optional<String> maxAge;
+
+        @JsonIgnore
+        private final Optional<Duration> maxAgeDuration;
+
+        @JsonCreator
+        public RunningOptions(@JsonProperty("usersPerSecond") int 
usersPerSecond,
+                              @JsonProperty("maxAge") Optional<String> maxAge) 
{
+            Preconditions.checkArgument(usersPerSecond > 0, "'usersPerSecond' 
needs to be strictly positive");
+            this.usersPerSecond = usersPerSecond;
+    
+            this.maxAge = maxAge;
+            this.maxAgeDuration = maxAge.map(v -> {
+                Duration maxAgeDuration = DurationParser.parse(maxAge.get(), 
ChronoUnit.DAYS);
+                Preconditions.checkArgument(!maxAgeDuration.isNegative(), 
"'maxAge' must be positive");
+                return maxAgeDuration;
+            });
+        }
+
+        public int getUsersPerSecond() {
+            return usersPerSecond;
+        }
+
+        public Optional<String> getMaxAge() {
+            return maxAge;
+        }
+    }
+
+    public static class Context {
+        private final AtomicLong inboxesExpired;
+        private final AtomicLong inboxesFailed;
+        private final AtomicLong inboxesProcessed;
+
+        public Context() {
+            this.inboxesExpired = new AtomicLong(0L);
+            this.inboxesFailed = new AtomicLong(0L);
+            this.inboxesProcessed = new AtomicLong(0L);
+        }
+
+        public long getInboxesExpired() {
+            return inboxesExpired.get();
+        }
+
+        public long getInboxesFailed() {
+            return inboxesFailed.get();
+        }
+
+        public long getInboxesProcessed() {
+            return inboxesProcessed.get();
+        }
+
+        public void incrementExpiredCount() {
+            inboxesExpired.incrementAndGet();
+        }
+
+        public void incrementFailedCount() {
+            inboxesFailed.incrementAndGet();
+        }
+
+        public void incrementProcessedCount() {
+            inboxesProcessed.incrementAndGet();
+        }
+    }
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(ExpireInboxesService.class);
+
+    private final UsersRepository usersRepository;
+    private final MailboxManager mailboxManager;
+
+    @Inject
+    public ExpireInboxesService(UsersRepository usersRepository, 
MailboxManager mailboxManager) {
+        this.usersRepository = usersRepository;
+        this.mailboxManager = mailboxManager;
+    }
+
+    public Mono<Result> expireInboxes(Context context, RunningOptions 
runningOptions, Date now) {
+        try {
+            LOGGER.info("expire with maxAge {} = {}", runningOptions.maxAge, 
runningOptions.maxAgeDuration);
+            SearchQuery expiration = SearchQuery.of(
+                runningOptions.maxAgeDuration.map(maxAge -> {
+                        Date limit = Date.from(now.toInstant().minus(maxAge));
+                        return SearchQuery.internalDateBefore(limit, 
DateResolution.Second);
+                    })
+                    .orElse(
+                        SearchQuery.headerDateBefore("Expires", now, 
DateResolution.Second)
+                    )
+            );
+            return Iterators.toFlux(usersRepository.list())
+                .transform(ReactorUtils.<Username, Task.Result>throttle()
+                    .elements(runningOptions.getUsersPerSecond())
+                    .per(Duration.ofSeconds(1))
+                    .forOperation(username -> expireUserInbox(context, 
username, expiration)))

Review Comment:
   This implementation is based on an assumption:
   1. People only sort mails into folders if they consider them "important" and 
want to keep them,
   2. People let anything else pile up in the INBOX and can't be bothered with 
cleaning it up.
   Thus, I want to do the INBOX cleanup for them, either with a hard cutoff 
after a month or two (maxAge) or giving them more control (Expires).
   
   What you describe seems to be a different use case, which I do not quite 
understand yet. How would you distinguish between "important" mails to keep 
forever, and general post pile that should be cleaned regularly? Is there no 
more "important" in this scenario? Or would it be a site policy via a 
configuration parameter, telling which mailboxes (or name match patterns) to 
clean up? Please elaborate.



-- 
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]

Reply via email to