From 803dbf66014de5c03207d86402bc52bf5896e820 Mon Sep 17 00:00:00 2001 From: Stanislav Kaledin Date: Tue, 8 May 2018 18:34:37 +0300 Subject: [PATCH] Fixed problem with duplicate job_id. Instead of raising error now write to log and refresh job --- django_apscheduler/jobstores.py | 19 +++++++++++-------- setup.py | 2 +- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/django_apscheduler/jobstores.py b/django_apscheduler/jobstores.py index 8e59c2b..dbad7c8 100644 --- a/django_apscheduler/jobstores.py +++ b/django_apscheduler/jobstores.py @@ -88,17 +88,20 @@ def get_all_jobs(self): @ignore_database_error() def add_job(self, job): - if DjangoJob.objects.filter( - name=job.id - ).exists(): - raise ConflictingIdError(job.id) - - DjangoJob.objects.create( + dbJob, created = DjangoJob.objects.get_or_create( + defaults=dict( + next_run_time=serialize_dt(job.next_run_time), + job_state=pickle.dumps(job.__getstate__(), self.pickle_protocol) + ), name=job.id, - next_run_time=serialize_dt(job.next_run_time), - job_state=pickle.dumps(job.__getstate__(), self.pickle_protocol) ) + if not created: + LOGGER.warning("Job with id %s already in jobstore. I'll refresh it", job.id) + dbJob.next_run_time = serialize_dt(job.next_run_time) + dbJob.job_state=pickle.dumps(job.__getstate__(), self.pickle_protocol) + dbJob.save() + @ignore_database_error() def update_job(self, job): updated = DjangoJob.objects.filter(name=job.id).update( diff --git a/setup.py b/setup.py index 268083f..560ea69 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='django-apscheduler', - version='0.2.8', + version='0.2.9', description='APScheduler for Django', classifiers=[ "Development Status :: 4 - Beta",