Example #1
0
    def test_lock_job_ongoing_job(self, db):
        cron_command = cronrun.Command()
        now = timezone.now()

        # Create a job that has an ongoing under MAX_ONGOING so it's still
        # locked
        Job.objects.create(app_name='testjob',
                           ongoing=now -
                           datetime.timedelta(seconds=MAX_ONGOING - 10))

        with freezegun.freeze_time(now, tz_offset=0):
            with pytest.raises(OngoingJobError):
                with cron_command.lock_job('testjob'):
                    pass
Example #2
0
    def test_lock_job_works(self, db):
        cron_command = cronrun.Command()
        now = timezone.now()

        # Create a job with no ongoing and then lock it
        Job.objects.create(app_name="testjob", ongoing=None)
        with freezegun.freeze_time(now, tz_offset=0):
            with cron_command.lock_job("testjob"):
                # Verify it's locked
                assert Job.objects.filter(app_name="testjob",
                                          ongoing=now).get()

        # Verify it's no longer locked
        assert Job.objects.filter(app_name="testjob",
                                  ongoing__isnull=True).get()
Example #3
0
    def test_lock_job_zombie_job(self, db):
        cron_command = cronrun.Command()
        now = timezone.now()

        # Create a job that has an ongoing over MAX_ONGOING so we break
        # the lock
        Job.objects.create(app_name='testjob',
                           ongoing=now -
                           datetime.timedelta(seconds=MAX_ONGOING + 10))

        with freezegun.freeze_time(now, tz_offset=0):
            with cron_command.lock_job('testjob'):
                # Verify it's locked with now as the datetime
                assert Job.objects.filter(app_name='testjob',
                                          ongoing=now).get()

        # Verify it's no longer locked
        assert Job.objects.filter(app_name='testjob',
                                  ongoing__isnull=True).get()