ctl: add --raise argument to runjob command (#68028)

This commit is contained in:
Frédéric Péters 2022-08-08 15:49:19 +02:00
parent 08a8d2c29c
commit 8c63a16ff0
3 changed files with 21 additions and 0 deletions

View File

@ -437,6 +437,11 @@ class TestAfterJob(AfterJob):
self.l10n_month = WorkflowStatusItem().compute('{{ "10/10/2010"|date:"F" }}')
class TestExceptionAfterJob(AfterJob):
def execute(self):
raise ZeroDivisionError()
def test_runjob(pub):
with pytest.raises(CommandError):
call_command('runjob')
@ -476,6 +481,18 @@ def test_runjob(pub):
call_command('runjob', '--domain=example.net', '--job-id=%s' % job.id, '--force-replay')
assert AfterJob.get(job.id).completion_time != completion_time
# test exception handling
job = TestExceptionAfterJob(label='test3')
job.store()
assert AfterJob.get(job.id).status == 'registered'
call_command('runjob', '--domain=example.net', '--job-id=%s' % job.id)
assert AfterJob.get(job.id).status == 'failed'
assert 'ZeroDivisionError' in AfterJob.get(job.id).exception
# check --raise
with pytest.raises(ZeroDivisionError):
call_command('runjob', '--domain=example.net', '--job-id=%s' % job.id, '--force-replay', '--raise')
def test_ctl_print_help(capsys):
ctl = wcs.qommon.ctl.Ctl(cmd_prefixes=['wcs.ctl'])

View File

@ -28,6 +28,7 @@ class Command(TenantCommand):
parser.add_argument('--domain', action='store', required=True)
parser.add_argument('--job-id', action='store', required=True)
parser.add_argument('--force-replay', action='store_true', default=False)
parser.add_argument('--raise', action='store_true', default=False)
def handle(self, *args, **options):
domain = options.pop('domain')
@ -38,4 +39,5 @@ class Command(TenantCommand):
raise CommandError('missing job')
if options.get('force_replay'):
job.completion_time = None
job.raise_exception = options.get('raise')
job.run()

View File

@ -117,6 +117,8 @@ class AfterJob(StorableObject):
else:
self.job_cmd(job=self)
except Exception as e:
if getattr(self, 'raise_exception', False):
raise
get_publisher().record_error(exception=e, notify=True)
self.exception = traceback.format_exc()
self.status = N_('failed')