Skip to content

Commit

Permalink
fix:when job fails make sure its still scheduled (#46)
Browse files Browse the repository at this point in the history
Fix #45
  • Loading branch information
rstalbow authored Oct 13, 2023
1 parent 0ab96ae commit a3eebaa
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
10 changes: 6 additions & 4 deletions scheduler/models/scheduled_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@

def failure_callback(job, connection, result, *args, **kwargs):
model_name = job.meta.get('task_type', None)
scheduled_task_id = job.meta.get('scheduled_task_id', None)
if model_name is None or scheduled_task_id:
if model_name is None:
return
model = apps.get_model(app_label='scheduler', model_name=model_name)
task = model.objects.filter(id=scheduled_task_id).first()
task = model.objects.filter(job_id=job.id).first()
mail_admins(f'Task {task.id}/{task.name} has failed',
'See django-admin for logs', )
pass
if task is None:
return
task.job_id = None
task.save(schedule_job=True)


def success_callback(job, connection, result, *args, **kwargs):
Expand Down
11 changes: 11 additions & 0 deletions scheduler/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,17 @@ def test_check_rescheduled_after_execution(self):
self.assertTrue(task.is_scheduled())
self.assertNotEquals(task.job_id, first_run_id)

def test_check_rescheduled_after_execution_failed_job(self):
task = task_factory(self.TaskModelClass, callable_name='scheduler.tests.jobs.failing_job',
scheduled_time=timezone.now() + timedelta(seconds=1))
queue = task.rqueue
first_run_id = task.job_id
entry = queue.fetch_job(first_run_id)
queue.run_sync(entry)
task.refresh_from_db()
self.assertTrue(task.is_scheduled())
self.assertNotEquals(task.job_id, first_run_id)


class TestCronJob(BaseTestCases.TestBaseJob):
TaskModelClass = CronTask
Expand Down
4 changes: 2 additions & 2 deletions scheduler/tests/testtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ def sequence_gen():
seq = sequence_gen()


def task_factory(cls, instance_only=False, **kwargs):
def task_factory(cls, callable_name: str = 'scheduler.tests.jobs.test_job', instance_only=False, **kwargs):
values = dict(
name='Scheduled Job %d' % next(seq),
job_id=None,
queue=list(settings.QUEUES.keys())[0],
callable='scheduler.tests.jobs.test_job',
callable=callable_name,
enabled=True,
timeout=None)
if cls == ScheduledTask:
Expand Down

0 comments on commit a3eebaa

Please sign in to comment.