From 8caf6620437a949b8a1b5670572ca48860eb5f79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20D=C3=A9nari=C3=A9?= Date: Mon, 11 Nov 2019 14:29:32 +0100 Subject: [PATCH] PLF-8447 : "Send me a digest email" on notification settings is not working (#412) (#600) * PLF-8447 : Resume Daily and weekly digest mails jobs (#406) * PLF-8447 : Resume Daily and weekly digest mails jobs This commit add an upgrade plugin to resume jobs in environments in which the migration is already done. * PLF-8447 : Resume Daily and weekly digest mails jobs Before this fix, the Upgrade Plugin was not launched if the platform is already in version 6.0 In fact, for tribe, this plugin will be not launched. The fix change the target version for the UP so that it will be started also on 6.0.x * PLF-8447 : Send Digest mail are not working Before this fix, when the Upgrade Plugin is applied after a lot af weeks without running, all pending notification will be treated and sent. This Fix remove all daily and weekly notifications which are pending. * Use MailNotificationStorage instead of DAO * PLF-8447 : manage error. If an error occurs, throw it to relaunch the UP at next startup * PLF-8447 : limit the digest to version after 5.2.0 * PLF-8447 : the delete digest must be run if oldVersion is 5.2.0 * Update exception management * Add unit test * Update file headers --- .../plugins/ResumeDigestJobUpgradePlugin.java | 79 +++++++++++++++++++ .../ResumeDigestJobUpgradePluginTest.java | 60 ++++++++++++++ .../upgrade/upgrade-configuration.xml | 28 +++++++ 3 files changed, 167 insertions(+) create mode 100644 component/upgrade/plugins/src/main/java/org/exoplatform/platform/upgrade/plugins/ResumeDigestJobUpgradePlugin.java create mode 100644 component/upgrade/plugins/src/test/java/org/exoplatform/platform/upgrade/plugins/ResumeDigestJobUpgradePluginTest.java diff --git a/component/upgrade/plugins/src/main/java/org/exoplatform/platform/upgrade/plugins/ResumeDigestJobUpgradePlugin.java b/component/upgrade/plugins/src/main/java/org/exoplatform/platform/upgrade/plugins/ResumeDigestJobUpgradePlugin.java new file mode 100644 index 0000000000..82eed237e1 --- /dev/null +++ b/component/upgrade/plugins/src/main/java/org/exoplatform/platform/upgrade/plugins/ResumeDigestJobUpgradePlugin.java @@ -0,0 +1,79 @@ +package org.exoplatform.platform.upgrade.plugins; + +import org.exoplatform.commons.api.notification.NotificationContext; +import org.exoplatform.commons.api.notification.service.storage.MailNotificationStorage; +import org.exoplatform.commons.notification.impl.NotificationContextImpl; +import org.exoplatform.commons.notification.impl.jpa.email.JPAMailNotificationStorage; +import org.exoplatform.commons.notification.impl.jpa.email.dao.MailDigestDAO; +import org.exoplatform.commons.notification.job.NotificationJob; +import org.exoplatform.commons.upgrade.UpgradeProductPlugin; +import org.exoplatform.commons.version.util.VersionComparator; +import org.exoplatform.container.xml.InitParams; +import org.exoplatform.services.log.ExoLogger; +import org.exoplatform.services.log.Log; +import org.exoplatform.services.scheduler.JobSchedulerService; + + +/** + * Upgrade plugin to resume daily and weekly digest jobs. + * This jobs have been paused during migration of notification from JCR to RDBMS + * This UP resume both jobs + * In addition, if the migration comes from version 5.2.0+, we completly delete stored digest. + * In fact, during the time the jobs were paused, the digest were stored in DB. + * If we don't delete it, the first start will create old for old digests. + * + * Finally, if we upgrade from a version before 5.2.0, we don't delete digest : + * The jobs were not paused, and there is no "old" digests stored. + */ + +public class ResumeDigestJobUpgradePlugin extends UpgradeProductPlugin { + + private JobSchedulerService schedulerService; + private MailNotificationStorage mailNotificationStorage; + + private static final Log LOG = ExoLogger.getLogger(ResumeDigestJobUpgradePlugin.class); + + + public ResumeDigestJobUpgradePlugin(JobSchedulerService schedulerService, MailNotificationStorage mailNotificationStorage, InitParams initParams) { + super(initParams); + this.mailNotificationStorage=mailNotificationStorage; + this.schedulerService = schedulerService; + } + + + @Override + public void processUpgrade(String oldVersion, String newVersion) { + + + try { + //Force remove digest items only source migration version is after 5.2.0 + //Before this version, there is no problem of blocking digest, so no need of delete it + if (VersionComparator.isAfter(oldVersion, "5.2.0") || + VersionComparator.isSame(oldVersion, "5.2.0")) { + NotificationContext context = NotificationContextImpl.cloneInstance(); + + //force remove weekly notification digest + context.append(NotificationJob.JOB_DAILY, false); + context.append(NotificationJob.JOB_WEEKLY, true); + mailNotificationStorage.removeMessageAfterSent(context); + + //force remove daily notification digest + context.append(NotificationJob.JOB_WEEKLY, false); + context.append(NotificationJob.JOB_DAILY, true); + mailNotificationStorage.removeMessageAfterSent(context); + } + + schedulerService.resumeJob("NotificationDailyJob", "Notification"); + schedulerService.resumeJob("NotificationWeeklyJob", "Notification"); + } catch (Exception e) { + LOG.error("Error when resuming daily and weekly job",e); + throw new RuntimeException("An error occurred when resuming daily and weekly job"); + } + } + + @Override + public boolean shouldProceedToUpgrade(String newVersion, String previousVersion) { + return VersionComparator.isAfter(newVersion, previousVersion); + } + +} diff --git a/component/upgrade/plugins/src/test/java/org/exoplatform/platform/upgrade/plugins/ResumeDigestJobUpgradePluginTest.java b/component/upgrade/plugins/src/test/java/org/exoplatform/platform/upgrade/plugins/ResumeDigestJobUpgradePluginTest.java new file mode 100644 index 0000000000..54dd39264c --- /dev/null +++ b/component/upgrade/plugins/src/test/java/org/exoplatform/platform/upgrade/plugins/ResumeDigestJobUpgradePluginTest.java @@ -0,0 +1,60 @@ +package org.exoplatform.platform.upgrade.plugins; + +import org.exoplatform.commons.api.notification.NotificationContext; +import org.exoplatform.commons.api.notification.service.storage.MailNotificationStorage; +import org.exoplatform.container.xml.InitParams; +import org.exoplatform.services.scheduler.JobSchedulerService; +import org.junit.Test; + +import static org.mockito.Mockito.*; + +public class ResumeDigestJobUpgradePluginTest { + + @Test + public void testResumeDigestJobUpgradePluginFrom510To520() throws Exception { + //Given + JobSchedulerService schedulerService = mock(JobSchedulerService.class); + MailNotificationStorage mailNotificationStorage = mock(MailNotificationStorage.class); + + // When + ResumeDigestJobUpgradePlugin plugin = new ResumeDigestJobUpgradePlugin(schedulerService, + mailNotificationStorage, new InitParams()); + plugin.processUpgrade("5.1.0", "5.2.0"); + + // Then + verify(mailNotificationStorage, times(0)).removeMessageAfterSent(any(NotificationContext.class)); + verify(schedulerService,times(2)).resumeJob(anyString(),anyString()); + } + + @Test + public void testResumeDigestJobUpgradePluginFrom520To600() throws Exception { + //Given + JobSchedulerService schedulerService = mock(JobSchedulerService.class); + MailNotificationStorage mailNotificationStorage = mock(MailNotificationStorage.class); + + // When + ResumeDigestJobUpgradePlugin plugin = new ResumeDigestJobUpgradePlugin(schedulerService, + mailNotificationStorage, new InitParams()); + plugin.processUpgrade("5.2.0", "6.0.0"); + + // Then + verify(mailNotificationStorage, times(2)).removeMessageAfterSent(any(NotificationContext.class)); + verify(schedulerService,times(2)).resumeJob(anyString(),anyString()); + } + + @Test + public void testResumeDigestJobUpgradePluginFrom510To600() throws Exception { + //Given + JobSchedulerService schedulerService = mock(JobSchedulerService.class); + MailNotificationStorage mailNotificationStorage = mock(MailNotificationStorage.class); + + // When + ResumeDigestJobUpgradePlugin plugin = new ResumeDigestJobUpgradePlugin(schedulerService, + mailNotificationStorage, new InitParams()); + plugin.processUpgrade("5.1.0", "6.0.0"); + + // Then + verify(mailNotificationStorage, times(0)).removeMessageAfterSent(any(NotificationContext.class)); + verify(schedulerService,times(2)).resumeJob(anyString(),anyString()); + } +} diff --git a/extension/webapp/src/main/webapp/WEB-INF/conf/platform/upgrade/upgrade-configuration.xml b/extension/webapp/src/main/webapp/WEB-INF/conf/platform/upgrade/upgrade-configuration.xml index 716f80ca12..2554eb798e 100644 --- a/extension/webapp/src/main/webapp/WEB-INF/conf/platform/upgrade/upgrade-configuration.xml +++ b/extension/webapp/src/main/webapp/WEB-INF/conf/platform/upgrade/upgrade-configuration.xml @@ -642,5 +642,33 @@ + + ResumeDigestJobUpgradePlugin + addUpgradePlugin + org.exoplatform.platform.upgrade.plugins.ResumeDigestJobUpgradePlugin + Resume the digest notification jobs daily and weekly (deactivated by mail notification migration) + + + product.group.id + The groupId of the product + org.exoplatform.platform + + + plugin.execution.order + The plugin execution order + 2 + + + plugin.upgrade.execute.once + Execute this upgrade pluginonly once + true + + + plugin.upgrade.target.version + Target version of the plugin + 6.0.0 + + +