def test_time_unaware_aware(self): # add job job = self.scheduler.enqueue_in( timedelta(minutes=5), dummy_job, ) job_id = job.get_id() # test rv = scheduled_execution_time(job_id, self.scheduler, naive=True) self.assertEqual(rv.tzinfo, None) rv = scheduled_execution_time(job_id, self.scheduler, naive=False) self.assertEqual(rv.tzinfo, pytz.UTC)
def test_preview_scheduled_job(self): # prepare fake data self.prepare_data() # schedule a real job (NewInstructorAction) action = NewInstructorAction( trigger=self.trigger, objects=dict(event=self.event, task=self.task), ) # it's important to call `action._email()`, because it prepares # `action.context` email = action._email() job = self.scheduler.enqueue_in(timedelta(minutes=10), action) rqjob = RQJob.objects.create(job_id=job.id, trigger=self.trigger) scheduled = scheduled_execution_time(job.id, scheduler=self.scheduler) # log admin user self._logSuperuserIn() url = reverse("admin:autoemails_rqjob_preview", args=[rqjob.pk]) rv = self.client.get(url) self.assertEqual(rv.status_code, 200) # We can fetch the Job, it's coming from NewInstructorAction.__call__ self.assertEqual(rv.context["rqjob"], rqjob) self.assertEqual(rv.context["job"], job) self.assertEqual(rv.context["job_scheduled"], scheduled) self.assertEqual(rv.context["instance"], action) self.assertEqual(rv.context["trigger"], self.trigger) self.assertEqual(rv.context["template"], self.trigger.template) # can't compare emails directly, __eq__ is not implemented self.assertTrue(compare_emails(rv.context["email"], email)) self.assertEqual(rv.context["adn_context"], action.context)
def test_time_for_run_job(self): # add job job = self.scheduler.enqueue_in( timedelta(minutes=5), dummy_job, ) job_id = job.get_id() # move the job to queue self.scheduler.enqueue_job(job) # test rv = scheduled_execution_time(job_id, self.scheduler) self.assertEqual(rv, None)
def test_time_for_scheduled_job(self): # add job job = self.scheduler.enqueue_in( timedelta(minutes=5), dummy_job, ) job_id = job.get_id() # test rv = scheduled_execution_time(job_id, self.scheduler) self.assertNotEqual(rv, None) epochtime = to_unix(rv) now = to_unix(datetime.utcnow() + timedelta(minutes=5)) self.assertAlmostEqual(epochtime, now, delta=1)
def schedule_repeating_job(trigger, action_class: BaseRepeatedAction, _scheduler=None): action_name = action_class.__name__ action = action_class(trigger=trigger) meta = dict( action=action, template=trigger.template, email=None, context=None, email_action_class=action_class.EMAIL_ACTION_CLASS, ) job = _scheduler.schedule( scheduled_time=datetime.utcnow( ), # Time for first execution, in UTC timezone func=action, # Function to be queued interval=action.INTERVAL, # Time before the function is called again repeat=action. REPEAT, # Repeat this number of times (None means repeat forever) meta=meta, ) scheduled_at = scheduled_execution_time(job.get_id(), scheduler=scheduler, naive=False) logger.debug("%s: job created [%r]", action_name, job) # save job ID in the object logger.debug( "%s: saving job in jobs table", action_name, ) rqj = RQJob.objects.create( job_id=job.get_id(), trigger=trigger, scheduled_execution=scheduled_at, status=check_status(job), mail_status="", interval=job.meta.get("interval"), result_ttl=job.result_ttl, action_name=action_name, ) return rqj
def test_preview_invoked_job(self): # prepare fake data self.prepare_data() # schedule a real job (NewInstructorAction) action = NewInstructorAction( trigger=self.trigger, objects=dict(event=self.event, task=self.task), ) # it's important to call `action._email()`, because it prepares # `action.context` email = action._email() # some cheating, normally the `action.email` is implemented in # `__call__` action.email = email job = self.scheduler.enqueue_in(timedelta(minutes=10), action) rqjob = RQJob.objects.create(job_id=job.id, trigger=self.trigger) # Speed up the job! Enqueue and run immediately. self.scheduler.enqueue_job(job) scheduled = scheduled_execution_time(job.id, scheduler=self.scheduler) # log admin user self._logSuperuserIn() url = reverse('admin:autoemails_rqjob_preview', args=[rqjob.pk]) rv = self.client.get(url) self.assertEqual(rv.status_code, 200) # We can fetch the Job, it's coming from NewInstructorAction.__call__ self.assertEqual(rv.context['rqjob'], rqjob) self.assertEqual(rv.context['job'], job) self.assertEqual(rv.context['job_scheduled'], scheduled) self.assertEqual(rv.context['instance'], action) self.assertEqual(rv.context['trigger'], self.trigger) self.assertEqual(rv.context['template'], self.trigger.template) # can't compare emails directly, __eq__ is not implemented self.assertTrue(compare_emails(rv.context['email'], email)) self.assertEqual(rv.context['adn_context'], action.context)
def preview(self, request, object_id): """Show job + email preview (all details and statuses).""" rqjob = get_object_or_404(RQJob, id=object_id) logger.debug(f"Previewing job {rqjob.job_id}...") try: job = Job.fetch(rqjob.job_id, connection=scheduler.connection) job_scheduled = scheduled_execution_time(job.get_id(), scheduler) instance = job.instance status = check_status(job) logger.debug(f"Job {rqjob.job_id} fetched") # the job may not exist anymore, then we can't retrieve any data except NoSuchJobError: job = None job_scheduled = None instance = None status = None trigger = None template = None email = None adn_context = None logger.debug(f"Job {rqjob.job_id} unavailable") # we can try and read properties else: try: trigger = instance.trigger template = instance.template email = instance._email() adn_context = instance.context except AttributeError: trigger = None template = None email = None adn_context = None reschedule_form = None if job and not job.is_failed: now_utc = datetime.utcnow() + timedelta(minutes=10) reschedule_form = RescheduleForm( initial=dict(scheduled_execution=job_scheduled or now_utc) ) template_form = None if instance and job and not job.is_failed: template_form = TemplateForm( initial=dict(template=instance.template.body_template) ) # find prev / next RQJob in the list previous = RQJob.objects.filter(pk__lt=rqjob.pk).order_by("id").last() next_ = RQJob.objects.filter(pk__gt=rqjob.pk).order_by("id").first() context = dict( self.admin_site.each_context(request), cl=self.get_changelist_instance(request), title=f"Preview {rqjob}", rqjob=rqjob, job=job, job_scheduled=job_scheduled, instance=instance, status=status, trigger=trigger, template=template, email=email, adn_context=adn_context, reschedule_form=reschedule_form, template_form=template_form, previous=previous, next=next_, ) return TemplateResponse(request, "rqjob_preview.html", context)
def test_time_for_nonexisting_job(self): job_id = "doesn't exists" rv = scheduled_execution_time(job_id, self.scheduler) self.assertEqual(rv, None)