jobs: add missing checks in runjob command (#67613)
This commit is contained in:
parent
ecb800f1c1
commit
2d6e859971
|
@ -16,6 +16,7 @@
|
|||
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
from django.db import connection, transaction
|
||||
from django.utils import timezone
|
||||
|
||||
from passerelle.base.models import Job
|
||||
|
||||
|
@ -35,6 +36,12 @@ class Command(BaseCommand):
|
|||
job = Job.objects.select_for_update(**skip_locked).get(pk=options['job_id'])
|
||||
except Job.DoesNotExist:
|
||||
raise CommandError('missing job')
|
||||
|
||||
if job.status != 'registered':
|
||||
raise CommandError('cannot run job, status is %s' % job.status)
|
||||
if job.after_timestamp and job.after_timestamp >= timezone.now():
|
||||
raise CommandError('cannot run job, should be run after %s' % job.after_timestamp)
|
||||
|
||||
job.status = 'running'
|
||||
job.save()
|
||||
# release lock
|
||||
|
|
|
@ -7,6 +7,7 @@ import isodate
|
|||
import mock
|
||||
import pytest
|
||||
from django.core.management import call_command
|
||||
from django.core.management.base import CommandError
|
||||
|
||||
import tests.utils
|
||||
from passerelle.base.models import Job, ResourceLog, SkipJob
|
||||
|
@ -165,6 +166,17 @@ def test_runjob(mocked_get, app, base_adresse, freezer):
|
|||
assert Job.objects.get(id=job.id).status == 'completed'
|
||||
assert StreetModel.objects.count() == 3
|
||||
|
||||
with pytest.raises(CommandError) as e:
|
||||
call_command('runjob', '--job-id=%s' % job.pk)
|
||||
assert 'cannot run job, status is completed' in str(e.value)
|
||||
|
||||
date = datetime.date(year=2019, month=1, day=2)
|
||||
job = base_adresse.add_job('update_streets_data', after_timestamp=date)
|
||||
|
||||
with pytest.raises(CommandError) as e:
|
||||
call_command('runjob', '--job-id=%s' % job.pk)
|
||||
assert 'cannot run job, should be run after 2019-01-02 00:00:00+00:00' in str(e.value)
|
||||
|
||||
|
||||
@mock.patch('passerelle.utils.Request.get')
|
||||
def test_jobs_api_error_log_level(mocked_get, app, base_adresse, freezer):
|
||||
|
|
Loading…
Reference in New Issue