diff --git a/testresults/src/org/labkey/testresults/TestResultsController.java b/testresults/src/org/labkey/testresults/TestResultsController.java index 9c140539..468bba15 100644 --- a/testresults/src/org/labkey/testresults/TestResultsController.java +++ b/testresults/src/org/labkey/testresults/TestResultsController.java @@ -1416,6 +1416,7 @@ public Object execute(EmailCronForm form, BindException errors) throws Exception case "start": // if already started if (scheduler.checkExists(jobKeyEmail)) { + saveEmailEnabled(true); res.put("Message", "Job already exists"); res.put("Response", "true"); return new ApiSimpleResponse(res); @@ -1424,6 +1425,7 @@ public Object execute(EmailCronForm form, BindException errors) throws Exception scheduler = start(scheduler, jobKeyEmail); // start // ensure job exists after started if (scheduler.checkExists(jobKeyEmail)) { + saveEmailEnabled(true); res.put("Message", "Job Created"); res.put("Response", "true"); } else { @@ -1434,6 +1436,7 @@ public Object execute(EmailCronForm form, BindException errors) throws Exception case "stop": scheduler.deleteJob(jobKeyEmail); if (!scheduler.checkExists(jobKeyEmail)) { + saveEmailEnabled(false); res.put("Message", "Job successfully removed"); res.put("Response", "false"); } else { @@ -1490,6 +1493,12 @@ public Object execute(EmailCronForm form, BindException errors) throws Exception return new ApiSimpleResponse(res); } + private void saveEmailEnabled(boolean enabled) + { + TestResultsModule.setEmailEnabled(enabled); + _log.info("The daily test results email was {} by {}", enabled ? "enabled" : "disabled", getUser().getEmail()); + } + @NotNull public static Scheduler start(Scheduler scheduler, JobKey jobKeyEmail) throws SchedulerException { diff --git a/testresults/src/org/labkey/testresults/TestResultsModule.java b/testresults/src/org/labkey/testresults/TestResultsModule.java index 8819677b..cb3d0eb2 100644 --- a/testresults/src/org/labkey/testresults/TestResultsModule.java +++ b/testresults/src/org/labkey/testresults/TestResultsModule.java @@ -20,6 +20,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.labkey.api.data.Container; +import org.labkey.api.data.PropertyManager; import org.labkey.api.module.DefaultModule; import org.labkey.api.module.ModuleContext; import org.labkey.api.security.Directive; @@ -49,6 +50,9 @@ public class TestResultsModule extends DefaultModule public static final String JOB_NAME = "TestResultsEmailTrigger"; public static final String JOB_GROUP = "TestResultsGroup"; + private static final String TESTRESULTS_EMAIL_PROPS = "TestResultsEmail"; + private static final String PROP_EMAIL_ENABLED = "enabled"; + // for saving public static final String TR_VIEW = "testresults-view"; @@ -120,6 +124,11 @@ public Set getSchemaNames() @NotNull public void startBackgroundThreads() { + if (!isEmailEnabled()) + { + LOG.info("The daily test results email is not enabled. A site admin can start it on the training data page."); + return; + } try { Scheduler scheduler = new StdSchedulerFactory().getScheduler(); @@ -132,4 +141,19 @@ public void startBackgroundThreads() } } + + /** + * Returns true if a site admin has started the daily email. A server with no saved setting returns false. + */ + public static boolean isEmailEnabled() + { + return Boolean.parseBoolean(PropertyManager.getProperties(TESTRESULTS_EMAIL_PROPS).get(PROP_EMAIL_ENABLED)); + } + + public static void setEmailEnabled(boolean enabled) + { + PropertyManager.WritablePropertyMap map = PropertyManager.getWritableProperties(TESTRESULTS_EMAIL_PROPS, true); + map.put(PROP_EMAIL_ENABLED, String.valueOf(enabled)); + map.save(); + } } \ No newline at end of file