Add DjangoJobExecution fail because sometimes EVENT_JOB_EXECUTED fire before EVENT_JOB_SUBMITTED
See original GitHub issueDescribe the bug Add DjangoJobExecution fail because sometimes EVENT_JOB_EXECUTED fire before EVENT_JOB_SUBMITTED
To Reproduce
def hello(request: HttpRequest):
job = scheduler.add_job(h, trigger="date", id=str(timezone.now()), run_date=timezone.now() + timedelta(seconds=5))
print(job)
return JsonResponse({"success": True})
Expected behavior should ignore add DjangoJobExecution.
Screenshots h (trigger: date[2020-10-19 09:41:34 UTC], next run at: 2020-10-19 09:41:34 UTC) [19/Oct/2020 09:41:29] “POST /scheduler/hello HTTP/1.1” 200 17 hello //////////////TZH EVENT_JOB_EXECUTED /////////////HELLO 2020-10-19 09:41:34.862484 2020-10-19 09:41:29.846453+00:00 2020-10-19 09:41:34.846471+00:00 Executed 0.010432 1603100494.856903 None None //////////////TZH EVENT_JOB_SUBMITTED 2020-10-19 09:41:29.846453+00:00 2020-10-19 09:41:34.846471+00:00 /////////////HELLO 2020-10-19 09:41:34.939636 2020-10-19 09:41:29.846453+00:00 2020-10-19 09:41:34.846471+00:00 Started execution None None None None Error notifying listener Traceback (most recent call last): File “/home/anaconda2/envs/py3/lib/python3.8/site-packages/django_apscheduler/models.py”, line 156, in atomic_update_or_create job_execution = DjangoJobExecution.objects.select_for_update().get( File “/home/anaconda2/envs/py3/lib/python3.8/site-packages/django/db/models/query.py”, line 429, in get raise self.model.DoesNotExist( django_apscheduler.models.DjangoJobExecution.DoesNotExist: DjangoJobExecution matching query does not exist.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “/home/anaconda2/envs/py3/lib/python3.8/site-packages/django/db/backends/utils.py”, line 84, in _execute
return self.cursor.execute(sql, params)
File “/home/anaconda2/envs/py3/lib/python3.8/site-packages/django/db/backends/mysql/base.py”, line 73, in execute
return self.cursor.execute(query, args)
File “/home/anaconda2/envs/py3/lib/python3.8/site-packages/MySQLdb/cursors.py”, line 209, in execute
res = self._query(query)
File “/home/anaconda2/envs/py3/lib/python3.8/site-packages/MySQLdb/cursors.py”, line 315, in _query
db.query(q)
File “/home/anaconda2/envs/py3/lib/python3.8/site-packages/MySQLdb/connections.py”, line 240, in query
_mysql.connection.query(self, query)
MySQLdb._exceptions.IntegrityError: (1452, ‘Cannot add or update a child row: a foreign key constraint fails (wt
.django_apscheduler_djangojobexecution
, CONSTRAINT django_apscheduler_djangojobexecution_job_id_daf5090a_fk
FOREIGN KEY (job_id
) REFERENCES django_apscheduler_djangojob
(id
))’)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File “/home/anaconda2/envs/py3/lib/python3.8/site-packages/apscheduler/schedulers/base.py”, line 831, in _dispatch_event
cb(event)
File “/home/anaconda2/envs/py3/lib/python3.8/site-packages/django_apscheduler/jobstores.py”, line 50, in handle_submission_event
job_execution = DjangoJobExecution.atomic_update_or_create(
File “/home/anaconda2/envs/py3/lib/python3.8/site-packages/django_apscheduler/models.py”, line 193, in atomic_update_or_create
job_execution = DjangoJobExecution.objects.create(
File “/home/anaconda2/envs/py3/lib/python3.8/site-packages/django/db/models/manager.py”, line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File “/home/anaconda2/envs/py3/lib/python3.8/site-packages/django/db/models/query.py”, line 447, in create
obj.save(force_insert=True, using=self.db)
File “/home/anaconda2/envs/py3/lib/python3.8/site-packages/django/db/models/base.py”, line 753, in save
self.save_base(using=using, force_insert=force_insert,
File “/home/anaconda2/envs/py3/lib/python3.8/site-packages/django/db/models/base.py”, line 790, in save_base
updated = self._save_table(
File “/home/anaconda2/envs/py3/lib/python3.8/site-packages/django/db/models/base.py”, line 895, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File “/home/anaconda2/envs/py3/lib/python3.8/site-packages/django/db/models/base.py”, line 933, in _do_insert
return manager._insert(
File “/home/anaconda2/envs/py3/lib/python3.8/site-packages/django/db/models/manager.py”, line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File “/home/anaconda2/envs/py3/lib/python3.8/site-packages/django/db/models/query.py”, line 1249, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File “/home/anaconda2/envs/py3/lib/python3.8/site-packages/django/db/models/sql/compiler.py”, line 1397, in execute_sql
cursor.execute(sql, params)
File “/home/anaconda2/envs/py3/lib/python3.8/site-packages/django/db/backends/utils.py”, line 98, in execute
return super().execute(sql, params)
File “/home/anaconda2/envs/py3/lib/python3.8/site-packages/django/db/backends/utils.py”, line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File “/home/anaconda2/envs/py3/lib/python3.8/site-packages/django/db/backends/utils.py”, line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File “/home/anaconda2/envs/py3/lib/python3.8/site-packages/django/db/backends/utils.py”, line 84, in _execute
return self.cursor.execute(sql, params)
File “/home/anaconda2/envs/py3/lib/python3.8/site-packages/django/db/utils.py”, line 90, in exit
raise dj_exc_value.with_traceback(traceback) from exc_value
File “/home/anaconda2/envs/py3/lib/python3.8/site-packages/django/db/backends/utils.py”, line 84, in _execute
return self.cursor.execute(sql, params)
File “/home/anaconda2/envs/py3/lib/python3.8/site-packages/django/db/backends/mysql/base.py”, line 73, in execute
return self.cursor.execute(query, args)
File “/home/anaconda2/envs/py3/lib/python3.8/site-packages/MySQLdb/cursors.py”, line 209, in execute
res = self._query(query)
File “/home/anaconda2/envs/py3/lib/python3.8/site-packages/MySQLdb/cursors.py”, line 315, in _query
db.query(q)
File “/home/anaconda2/envs/py3/lib/python3.8/site-packages/MySQLdb/connections.py”, line 240, in query
_mysql.connection.query(self, query)
django.db.utils.IntegrityError: (1452, ‘Cannot add or update a child row: a foreign key constraint fails (wt
.django_apscheduler_djangojobexecution
, CONSTRAINT django_apscheduler_djangojobexecution_job_id_daf5090a_fk
FOREIGN KEY (job_id
) REFERENCES django_apscheduler_djangojob
(id
))’)
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (3 by maintainers)
Top GitHub Comments
@aprilahijriyan, I don’t think that the error that you are seeing is related to this issue.
Please log a new issue, and include details on (1) how you are starting the scheduler in your code and (2) how individual jobs are being scheduled.
It seems to me that
DjangoJobStore.remove_job()
is failing due to a foreign key constraint check.But that does not make sense since we have the
on_delete=models.CASCADE
option defined onDjangoJobExecution.job
(so Django is supposed to just delete all of the related job executions as well when removing a job from the job store). Do you have multiple threads running and / or do you make of aBackgroundScheduler
perhaps?full logs