Skip to content

The Unified and Autonomous Development Platform

Fix issue #403 - Post comment via email stopped working after a while if mail timeout is specified

Robin Shenc3cb31176a12
diff --git a/server-core/src/main/java/io/onedev/server/buildspec/job/DefaultJobManager.java b/server-core/src/main/java/io/onedev/server/buildspec/job/DefaultJobManager.java
index e1fb17da8b..6055986989 100644
--- a/server-core/src/main/java/io/onedev/server/buildspec/job/DefaultJobManager.java
+++ b/server-core/src/main/java/io/onedev/server/buildspec/job/DefaultJobManager.java
@@ -1046,9 +1046,9 @@ public class DefaultJobManager implements JobManager, Runnable, CodePullAuthoriz
@Listen
public void on(SystemStopping event) {
- if (thread != null) {
- Thread copy = thread;
- thread = null;
+ Thread copy = thread;
+ thread = null;
+ if (copy != null) {
try {
copy.join();
} catch (InterruptedException e) {
diff --git a/server-core/src/main/java/io/onedev/server/migration/DataMigrator.java b/server-core/src/main/java/io/onedev/server/migration/DataMigrator.java
index f5d8488443..ca307e8c3e 100644
--- a/server-core/src/main/java/io/onedev/server/migration/DataMigrator.java
+++ b/server-core/src/main/java/io/onedev/server/migration/DataMigrator.java
@@ -2977,4 +2977,28 @@ public class DataMigrator {
}
}
+ private void migrate64(File dataDir, Stack<Integer> versions) {
+ for (File file: dataDir.listFiles()) {
+ if (file.getName().startsWith("Settings.xml")) {
+ VersionedXmlDoc dom = VersionedXmlDoc.fromFile(file);
+ for (Element element: dom.getRootElement().elements()) {
+ String key = element.elementTextTrim("key");
+ if (element.elementTextTrim("key").equals("MAIL")) {
+ Element valueElement = element.element("value");
+ if (valueElement != null) {
+ Element timeoutElement = valueElement.element("timeout");
+ int timeout = Integer.valueOf(timeoutElement.getTextTrim());
+ if (timeout == 0)
+ timeout = 60;
+ else if (timeout < 10)
+ timeout = 10;
+ timeoutElement.setText(String.valueOf(timeout));
+ }
+ }
+ }
+ dom.writeToFile(file, false);
+ }
+ }
+ }
+
}
diff --git a/server-core/src/main/java/io/onedev/server/model/support/administration/MailSetting.java b/server-core/src/main/java/io/onedev/server/model/support/administration/MailSetting.java
index 949e82a755..7245937497 100644
--- a/server-core/src/main/java/io/onedev/server/model/support/administration/MailSetting.java
+++ b/server-core/src/main/java/io/onedev/server/model/support/administration/MailSetting.java
@@ -2,6 +2,8 @@ package io.onedev.server.model.support.administration;
import java.io.Serializable;
+import javax.validation.constraints.Min;
+
import org.hibernate.validator.constraints.NotEmpty;
import io.onedev.server.web.editable.annotation.Editable;
@@ -103,8 +105,8 @@ public class MailSetting implements Serializable {
this.enableStartTLS = enableStartTLS;
}
- @Editable(order=600, description="Specify timeout in seconds when communicating with mail server. " +
- "Use 0 to set an infinite timeout.")
+ @Editable(order=600, description="Specify timeout in seconds when communicating with mail server")
+ @Min(value=10, message="This value should not be less than 10")
public int getTimeout() {
return timeout;
}
diff --git a/server-core/src/main/java/io/onedev/server/notification/DefaultMailManager.java b/server-core/src/main/java/io/onedev/server/notification/DefaultMailManager.java
index a3319d0da6..559a8ddcb6 100644
--- a/server-core/src/main/java/io/onedev/server/notification/DefaultMailManager.java
+++ b/server-core/src/main/java/io/onedev/server/notification/DefaultMailManager.java
@@ -22,7 +22,6 @@ import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Singleton;
import javax.mail.Folder;
-import javax.mail.FolderClosedException;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
@@ -55,7 +54,9 @@ import org.slf4j.LoggerFactory;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
+import com.sun.mail.iap.ProtocolException;
import com.sun.mail.imap.IMAPFolder;
+import com.sun.mail.imap.protocol.IMAPProtocol;
import edu.emory.mathcs.backport.java.util.Arrays;
import io.onedev.commons.bootstrap.Bootstrap;
@@ -135,8 +136,6 @@ public class DefaultMailManager implements MailManager {
private final UrlManager urlManager;
- private volatile boolean stopping;
-
private volatile Thread thread;
@Inject
@@ -222,9 +221,7 @@ public class DefaultMailManager implements MailManager {
}
email.setSocketConnectionTimeout(Bootstrap.SOCKET_CONNECT_TIMEOUT);
-
- if (mailSetting.getTimeout() != 0)
- email.setSocketTimeout(mailSetting.getTimeout()*1000);
+ email.setSocketTimeout(mailSetting.getTimeout()*1000);
email.setStartTLSEnabled(mailSetting.isEnableStartTLS());
email.setSSLOnConnect(false);
@@ -283,9 +280,9 @@ public class DefaultMailManager implements MailManager {
@Override
public void run() {
- Thread thread = DefaultMailManager.this.thread;
- if (thread != null)
- thread.interrupt();
+ Thread copy = thread;
+ if (copy != null)
+ copy.interrupt();
}
});
@@ -700,28 +697,44 @@ public class DefaultMailManager implements MailManager {
@Override
public void run() {
- while (!stopping) {
+ while (thread != null) {
try {
MailSetting mailSetting = settingManager.getMailSetting();
- monitorInbox(mailSetting, new MessageListener() {
-
- @Override
- public void onReceived(Message message) {
- onMessage(mailSetting, message);
+ if (mailSetting != null && mailSetting.getReceiveMailSetting() != null) {
+ Future<?> future = monitorInbox(mailSetting.getReceiveMailSetting(), mailSetting.isEnableStartTLS(),
+ mailSetting.getTimeout(), new MessageListener() {
+
+ @Override
+ public void onReceived(Message message) {
+ onMessage(mailSetting, message);
+ }
+
+ });
+ try {
+ future.get();
+ } catch (InterruptedException e) {
+ future.cancel(true);
+ } catch (ExecutionException e) {
+ logger.error("Error monitoring inbox", e);
+ try {
+ Thread.sleep(5000);
+ } catch (InterruptedException e2) {
+ }
}
-
- }).waitForFinish();
+ } else {
+ try {
+ Thread.sleep(60000);
+ } catch (InterruptedException e) {
+ }
+ }
} catch (Exception e) {
- if (ExceptionUtils.find(e, InterruptedException.class) == null)
- logger.error("Error monitoring inbox", e);
- } finally {
+ logger.error("Error checking mail setting", e);
try {
Thread.sleep(5000);
- } catch (InterruptedException e) {
+ } catch (InterruptedException e2) {
}
}
}
- thread = null;
}
});
thread.start();
@@ -729,175 +742,107 @@ public class DefaultMailManager implements MailManager {
@Listen
public void on(SystemStopping event) {
- stopping = true;
- while (true) {
- Thread thread = this.thread;
- if (thread != null) {
- thread.interrupt();
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- }
- } else {
- break;
- }
- }
- }
-
- private void close(AtomicReference<Store> storeRef, AtomicReference<IMAPFolder> inboxRef) {
- if (inboxRef.get() != null) {
- if (inboxRef.get().isOpen()) {
- try {
- inboxRef.get().close(false);
- } catch (Exception e) {
- }
- }
- inboxRef.set(null);
- }
- if (storeRef.get() != null) {
+ Thread copy = thread;
+ thread = null;
+ if (copy != null) {
+ copy.interrupt();
try {
- storeRef.get().close();
- } catch (Exception e) {
+ copy.join();
+ } catch (InterruptedException e) {
}
- storeRef.set(null);
}
}
@Override
- public InboxMonitor monitorInbox(MailSetting mailSetting, MessageListener listener) {
- if (mailSetting != null && mailSetting.getReceiveMailSetting() != null) {
- ReceiveMailSetting receiveMailSetting = mailSetting.getReceiveMailSetting();
-
- Properties properties = new Properties();
-
- properties.setProperty("mail.imap.host", receiveMailSetting.getImapHost());
- properties.setProperty("mail.imap.port", String.valueOf(receiveMailSetting.getImapPort()));
- properties.setProperty("mail.imap.starttls.enable", String.valueOf(mailSetting.isEnableStartTLS()));
-
- properties.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
- properties.setProperty("mail.imap.socketFactory.fallback", "false");
- properties.setProperty("mail.imap.socketFactory.port", String.valueOf(receiveMailSetting.getImapPort()));
-
- properties.setProperty("mail.imap.connectiontimeout", String.valueOf(Bootstrap.SOCKET_CONNECT_TIMEOUT));
- if (mailSetting.getTimeout() != 0)
- properties.setProperty("mail.imap.timeout", String.valueOf(mailSetting.getTimeout()*1000));
-
- AtomicReference<IMAPFolder> inboxRef = new AtomicReference<>(null);
- AtomicReference<Store> store = new AtomicReference<>(null);
- try {
- Session session = Session.getInstance(properties);
- store.set(session.getStore("imap"));
-
- store.get().connect(receiveMailSetting.getImapUser(), receiveMailSetting.getImapPassword());
+ public Future<?> monitorInbox(ReceiveMailSetting receiveMailSetting, boolean enableStartTLS,
+ int timeout, MessageListener listener) {
+ return executorService.submit(new Runnable() {
- inboxRef.set((IMAPFolder) store.get().getFolder("INBOX"));
- inboxRef.get().open(Folder.READ_ONLY);
- inboxRef.get().addMessageCountListener(new MessageCountListener() {
-
- @Override
- public void messagesAdded(MessageCountEvent event) {
- for (Message message: event.getMessages())
- listener.onReceived(message);
- }
+ @Override
+ public void run() {
+ Properties properties = new Properties();
+
+ properties.setProperty("mail.imap.host", receiveMailSetting.getImapHost());
+ properties.setProperty("mail.imap.port", String.valueOf(receiveMailSetting.getImapPort()));
+ properties.setProperty("mail.imap.starttls.enable", String.valueOf(enableStartTLS));
+
+ properties.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
+ properties.setProperty("mail.imap.socketFactory.fallback", "false");
+ properties.setProperty("mail.imap.socketFactory.port", String.valueOf(receiveMailSetting.getImapPort()));
+
+ properties.setProperty("mail.imap.connectiontimeout", String.valueOf(Bootstrap.SOCKET_CONNECT_TIMEOUT));
+ properties.setProperty("mail.imap.timeout", String.valueOf(timeout*1000));
+
+ Session session = Session.getInstance(properties);
+ Store store = null;
+ IMAPFolder inbox = null;
+ Future<?> future = null;
+ try {
+ store = session.getStore("imap");
+ store.connect(receiveMailSetting.getImapUser(), receiveMailSetting.getImapPassword());
+ inbox = (IMAPFolder) store.getFolder("INBOX");
+ inbox.open(Folder.READ_ONLY);
+ inbox.addMessageCountListener(new MessageCountListener() {
+
+ @Override
+ public void messagesAdded(MessageCountEvent event) {
+ for (Message message: event.getMessages())
+ listener.onReceived(message);
+ }
- @Override
- public void messagesRemoved(MessageCountEvent e) {
- }
+ @Override
+ public void messagesRemoved(MessageCountEvent e) {
+ }
- });
+ });
- AtomicReference<RuntimeException> exception = new AtomicReference<>(null);
- AtomicReference<Boolean> stopping = new AtomicReference<>(false);
- executorService.execute(new Runnable() {
+ IMAPFolder inboxCopy = inbox;
+ future = executorService.submit(new Runnable() {
- @Override
- public void run() {
- try {
- while (!stopping.get()) {
+ @Override
+ public void run() {
+ while (!Thread.interrupted()) {
try {
- inboxRef.get().idle();
- } catch (FolderClosedException e) {
- Thread.sleep(1000);
+ inboxCopy.idle();
+ } catch (MessagingException e) {
+ throw new RuntimeException(e);
}
}
- } catch (Exception e) {
- exception.set(ExceptionUtils.unchecked(e));
- } finally {
- close(store, inboxRef);
}
- }
+
+ });
- });
- return new InboxMonitor() {
-
- @Override
- public void stop() {
- IMAPFolder inbox = inboxRef.get();
- if (inbox != null) {
- stopping.set(true);
- try {
- inbox.close(false);
- } catch (Exception e) {
- }
- }
+ while (true) {
+ Thread.sleep(timeout*1000/2);
+ inbox.doCommand(new IMAPFolder.ProtocolCommand() {
+
+ public Object doCommand(IMAPProtocol p) throws ProtocolException {
+ p.simpleCommand("NOOP", null);
+ return null;
+ }
+
+ });
}
-
- @Override
- public void waitForFinish() throws InterruptedException {
- InterruptedException interruptedException = null;
- while (inboxRef.get() != null) {
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- interruptedException = e;
- stop();
- }
+ } catch (Exception e) {
+ throw ExceptionUtils.unchecked(e);
+ } finally {
+ if (future != null)
+ future.cancel(true);
+ if (inbox != null && inbox.isOpen()) {
+ try {
+ inbox.close(false);
+ } catch (Exception e) {
}
- if (interruptedException != null)
- throw interruptedException;
-
- if (exception.get() != null)
- throw exception.get();
}
-
- };
- } catch (Exception e) {
- close(store, inboxRef);
- throw ExceptionUtils.unchecked(e);
- }
- } else {
- Future<?> future = executorService.submit(new Runnable() {
-
- @Override
- public void run() {
- try {
- Thread.sleep(60000);
- } catch (InterruptedException e) {
- }
- }
-
- });
- return new InboxMonitor() {
-
- @Override
- public void stop() {
- future.cancel(true);
- }
-
- @Override
- public void waitForFinish() throws InterruptedException {
- try {
- future.get();
- } catch (InterruptedException e) {
- stop();
- throw e;
- } catch (ExecutionException e) {
- throw new RuntimeException(e);
+ if (store != null) {
+ try {
+ store.close();
+ } catch (Exception e) {
+ }
}
}
-
- };
- }
+ }
+ });
}
@Override
diff --git a/server-core/src/main/java/io/onedev/server/notification/InboxMonitor.java b/server-core/src/main/java/io/onedev/server/notification/InboxMonitor.java
deleted file mode 100644
index 89351be8f2..0000000000
--- a/server-core/src/main/java/io/onedev/server/notification/InboxMonitor.java
+++ /dev/null
@@ -1,9 +0,0 @@
-package io.onedev.server.notification;
-
-public interface InboxMonitor {
-
- void stop();
-
- void waitForFinish() throws InterruptedException;
-
-}
\ No newline at end of file
diff --git a/server-core/src/main/java/io/onedev/server/notification/MailManager.java b/server-core/src/main/java/io/onedev/server/notification/MailManager.java
index 04b7bc0819..76d8886315 100644
--- a/server-core/src/main/java/io/onedev/server/notification/MailManager.java
+++ b/server-core/src/main/java/io/onedev/server/notification/MailManager.java
@@ -1,12 +1,14 @@
package io.onedev.server.notification;
import java.util.Collection;
+import java.util.concurrent.Future;
import javax.annotation.Nullable;
import io.onedev.server.model.Issue;
import io.onedev.server.model.PullRequest;
import io.onedev.server.model.support.administration.MailSetting;
+import io.onedev.server.model.support.administration.ReceiveMailSetting;
public interface MailManager {
@@ -36,6 +38,7 @@ public interface MailManager {
@Nullable
public String getUnsubscribeAddress(PullRequest request);
- InboxMonitor monitorInbox(MailSetting mailSetting, MessageListener listener);
+ Future<?> monitorInbox(ReceiveMailSetting receiveMailSetting, boolean enableStartTLS,
+ int timeout, MessageListener listener);
}
diff --git a/server-core/src/main/java/io/onedev/server/web/page/admin/mailsetting/MailSettingPage.java b/server-core/src/main/java/io/onedev/server/web/page/admin/mailsetting/MailSettingPage.java
index a9c7e7bca3..b73ac39cbe 100644
--- a/server-core/src/main/java/io/onedev/server/web/page/admin/mailsetting/MailSettingPage.java
+++ b/server-core/src/main/java/io/onedev/server/web/page/admin/mailsetting/MailSettingPage.java
@@ -1,6 +1,9 @@
package io.onedev.server.web.page.admin.mailsetting;
import java.util.UUID;
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicReference;
import javax.mail.Message;
@@ -24,7 +27,6 @@ import io.onedev.server.entitymanager.SettingManager;
import io.onedev.server.model.User;
import io.onedev.server.model.support.administration.MailSetting;
import io.onedev.server.model.support.administration.ReceiveMailSetting;
-import io.onedev.server.notification.InboxMonitor;
import io.onedev.server.notification.MailManager;
import io.onedev.server.notification.MessageListener;
import io.onedev.server.security.SecurityUtils;
@@ -105,21 +107,22 @@ public class MailSettingPage extends AdministrationPage {
MailSetting mailSetting = mailSettingHolder.getMailSetting();
if (mailSetting.getReceiveMailSetting() != null) {
String uuid = UUID.randomUUID().toString();
- AtomicReference<InboxMonitor> monitor = new AtomicReference<>(null);
+ AtomicReference<Future<?>> futureRef = new AtomicReference<>(null);
MessageListener listener = new MessageListener() {
@Override
public void onReceived(Message message) {
try {
if (message.getSubject().contains(uuid))
- monitor.get().stop();
+ futureRef.get().cancel(true);
} catch (Exception e) {
logger.error("Error receiving message", e);
}
}
};
- monitor.set(mailManager.monitorInbox(mailSetting, listener));
+ futureRef.set(mailManager.monitorInbox(mailSetting.getReceiveMailSetting(),
+ mailSetting.isEnableStartTLS(), mailSetting.getTimeout(), listener));
EmailAddress emailAddress = EmailAddress.parse(mailSetting.getEmailAddress());
String subAddressed = emailAddress.getPrefix() + "+"
@@ -132,8 +135,12 @@ public class MailSettingPage extends AdministrationPage {
logger.log("Waiting for test mail to come back...");
try {
- monitor.get().waitForFinish();
+ futureRef.get().get();
+ } catch (CancellationException e) {
} catch (InterruptedException e) {
+ futureRef.get().cancel(true);
+ throw new RuntimeException(e);
+ } catch (ExecutionException e) {
throw new RuntimeException(e);
}