Repository: carpentries/amy

Issue:
RQ Job Preview for sent mails
Currently we can view pending (scheduled) automated email information In the Django administration panel (https://amy.carpentries.org/yikoouxjewu5mri5wmqkufrq6ztk8i5y/autoemails/rqjob/).  However, once the emails go out the details in the preview view go away. This creates a challenge when looking back on a workshop to confirm that an email went out.

Is it possible for the information in the RQ job preview to stay even after the email has gone out?


Discussion:
Tagging @pbanaszkiewicz  for feedback.
I think we can change the setting to keep the successfully sent emails for longer, but they will disappear eventually. What timeframe would suit you, @maneesha @Talishask? A year or so?
Checking with workshop admin team on this: @sheraaronhurt  @elletjies  @Talishask  @danielle06   What do you think? 
yes, I year would be good. We will be able to preview them for a year? 
Failed jobs are kept for about 1 year, but those that succeeded by default stay for only 500 seconds.

From what I looked, we can keep succeeded jobs for longer than 500s, e.g. for 1 year (even more if you want).
One year sounds good.  
Is there an advantage to keeping them just one year vs. keeping them longer (or forever)?
I think one year would be good. Usually, when we need to check on an email it's during the life of the individual workshop. I feel like one year would be plenty of "just in case" time. Right now I can't think of an advantage to keeping them for longer than that but I could definitely be missing something. 

Resolving patch:
diff --git a/config/settings.py b/config/settings.py
--- a/config/settings.py
+++ b/config/settings.py
@@ -600,6 +600,7 @@
 
 RQ = {
     "JOB_CLASS": "autoemails.job.Job",
+    "DEFAULT_RESULT_TTL": 31536000,  # 1 year in seconds for keeping job results
 }
 
 


Test patch:
diff --git a/amy/autoemails/tests/test_job_ttl.py b/amy/autoemails/tests/test_job_ttl.py
new file mode 100644
--- /dev/null
+++ b/amy/autoemails/tests/test_job_ttl.py
@@ -0,0 +1,41 @@
+from datetime import timedelta
+
+from django.test.testcases import TestCase
+
+from autoemails import admin
+from autoemails.tests.base import FakeRedisTestCaseMixin, dummy_job
+
+ONE_YEAR_IN_SECONDS = 365 * 24 * 60 * 60
+
+
+class TestJobResultsLongTTL(FakeRedisTestCaseMixin, TestCase):
+    def setUp(self):
+        super().setUp()
+
+        # save scheduler and connection data
+        self._saved_scheduler = admin.scheduler
+        # overwrite
+        admin.scheduler = self.scheduler
+
+    def tearDown(self):
+        super().tearDown()
+        # bring back saved scheduler
+        admin.scheduler = self._saved_scheduler
+
+    def test_scheduled_job_has_1yr_TTL(self):
+        # Act
+        job = self.scheduler.enqueue_in(timedelta(minutes=5), dummy_job)
+
+        # Assert
+        self.assertFalse(job.is_finished)  # job still hasn't run
+        self.assertEqual(job.result_ttl, ONE_YEAR_IN_SECONDS)
+
+    def test_finished_job_has_1yr_TTL(self):
+        # Arrange
+        job = self.scheduler.enqueue_in(timedelta(minutes=5), dummy_job)
+        # Act
+        self.queue.run_job(job)
+
+        # Assert
+        self.assertTrue(job.is_finished)
+        self.assertEqual(job.result_ttl, ONE_YEAR_IN_SECONDS)