Exemple #1
0
    def test_status_scheduled_job(self):
        # add job
        job = self.scheduler.enqueue_in(
            timedelta(minutes=5),
            dummy_job,
        )

        # test
        rv = check_status(job, self.scheduler)
        self.assertEqual(rv, "scheduled")
Exemple #2
0
    def action_refresh_state(self, request, queryset):
        counter = 0
        for rqjob in queryset:
            status = check_status(rqjob.job_id)
            if status is not None:
                rqjob.status = status
                rqjob.save()
                counter += 1

        self.message_user(request, "Refreshed status of %d RQJob(s)." % counter)
Exemple #3
0
    def test_status_deferred_job(self):
        # Create an asynchronous queue.
        # The name `separate_queue` used here is to ensure the queue isn't
        # used anywhere else.
        queue = Queue("separate_queue", connection=self.connection)

        # add job to the queue
        job = queue.enqueue(dummy_job)
        self.assertEqual(job.get_status(), "queued")

        # force the job status to be "started"
        job.set_status("deferred")
        self.assertTrue(job.is_deferred)

        # test
        rv = check_status(job, self.scheduler)
        self.assertEqual(rv, "deferred")
Exemple #4
0
    def test_status_failed_job(self):
        # Create an asynchronous queue.
        # The name `separate_queue` used here is to ensure the queue isn't used
        # anywhere else.
        queue = Queue("separate_queue", connection=self.connection)
        worker = SimpleWorker([queue], connection=queue.connection)

        # this job will fail
        job = queue.enqueue(dummy_fail_job)
        self.assertEqual(job.get_status(), "queued")

        # run the worker
        worker.work(burst=True)

        # test
        rv = check_status(job, self.scheduler)
        self.assertEqual(rv, "failed")
Exemple #5
0
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
Exemple #6
0
    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)
Exemple #7
0
 def test_status_nonexisting_job(self):
     job_id = "doesn't exists"
     rv = check_status(job_id, self.scheduler)
     self.assertEqual(rv, None)