Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging of job execution fails if the job is no longer available in the job store #116

Closed
ody5sey opened this issue Oct 6, 2020 · 2 comments · Fixed by #117
Closed

Logging of job execution fails if the job is no longer available in the job store #116

ody5sey opened this issue Oct 6, 2020 · 2 comments · Fixed by #117

Comments

@ody5sey
Copy link

ody5sey commented Oct 6, 2020

Describe the bug / feature

django==3.1.2
django-apscheduler==0.4.2
database:postgresql 10.0

This is my class view
`
class LoginView(View):

def get(self, request):
    logger.info("shadow")
    # <view logic>
    sched.add_job(func=print_value, trigger='date',
                  next_run_time=datetime.datetime.now(), id="shadow1")

    return HttpResponse('result')

`

and error
django.db.utils.IntegrityError: insert or update on table "django_apscheduler_djangojobexecution" violates foreign key constraint "django_apscheduler_djangojobexecution_job_id_daf5090a_fk" DETAIL: Key (job_id)=(shadow1) is not present in table "django_apscheduler_djangojob".

@jcass77
Copy link
Owner

jcass77 commented Oct 6, 2020

Could you please provide the full stack traceback for this exception so that we can identify the root cause?

It looks like the job is scheduled to run immediately, and to execute exactly once?

It is possible that APScheduler could have removed the job from the job store already by the time that we try to log that single execution in the DjangoJobExecution table, but I was unable to reproduce this error locally.

@ody5sey
Copy link
Author

ody5sey commented Oct 6, 2020

Error notifying listener
Traceback (most recent call last):
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/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 "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/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 "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.ForeignKeyViolation: insert or update on table "django_apscheduler_djangojobexecution" violates foreign key constraint "django_apscheduler_djangojobexecution_job_id_daf5090a_fk"
DETAIL: Key (job_id)=(shadow1) is not present in table "django_apscheduler_djangojob".

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/apscheduler/schedulers/base.py", line 831, in _dispatch_event
cb(event)
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/django_apscheduler/jobstores.py", line 90, in handle_execution_event
job_execution = DjangoJobExecution.atomic_update_or_create(
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/django_apscheduler/models.py", line 190, in atomic_update_or_create
job_execution = DjangoJobExecution.objects.create(
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/django/db/models/query.py", line 447, in create
obj.save(force_insert=True, using=self.db)
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/django/db/models/base.py", line 753, in save
self.save_base(using=using, force_insert=force_insert,
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/django/db/models/base.py", line 790, in save_base
updated = self._save_table(
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/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 "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/django/db/models/base.py", line 933, in _do_insert
return manager._insert(
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/django/db/models/query.py", line 1254, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1397, in execute_sql
cursor.execute(sql, params)
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/django/db/backends/utils.py", line 98, in execute
return super().execute(sql, params)
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/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 "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/django/db/utils.py", line 90, in exit
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: insert or update on table "django_apscheduler_djangojobexecution" violates foreign key constraint "django_apscheduler_djangojobexecution_job_id_daf5090a_fk"
DETAIL: Key (job_id)=(shadow1) is not present in table "django_apscheduler_djangojob".

Error notifying listener
Traceback (most recent call last):
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/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 "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/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 "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.ForeignKeyViolation: insert or update on table "django_apscheduler_djangojobexecution" violates foreign key constraint "django_apscheduler_djangojobexecution_job_id_daf5090a_fk"
DETAIL: Key (job_id)=(shadow1) is not present in table "django_apscheduler_djangojob".

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/apscheduler/schedulers/base.py", line 831, in _dispatch_event
cb(event)
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/django_apscheduler/jobstores.py", line 47, in handle_submission_event
job_execution = DjangoJobExecution.atomic_update_or_create(
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/django_apscheduler/models.py", line 190, in atomic_update_or_create
job_execution = DjangoJobExecution.objects.create(
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/django/db/models/query.py", line 447, in create
obj.save(force_insert=True, using=self.db)
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/django/db/models/base.py", line 753, in save
self.save_base(using=using, force_insert=force_insert,
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/django/db/models/base.py", line 790, in save_base
updated = self._save_table(
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/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 "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/django/db/models/base.py", line 933, in _do_insert
return manager._insert(
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/django/db/models/query.py", line 1254, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1397, in execute_sql
cursor.execute(sql, params)
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/django/db/backends/utils.py", line 98, in execute
return super().execute(sql, params)
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/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 "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/django/db/utils.py", line 90, in exit
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Users/null/.local/share/virtualenvs/Spacex-JYRXe-wy/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: insert or update on table "django_apscheduler_djangojobexecution" violates foreign key constraint "django_apscheduler_djangojobexecution_job_id_daf5090a_fk"
DETAIL: Key (job_id)=(shadow1) is not present in table "django_apscheduler_djangojob".

@jcass77 jcass77 changed the title django-apscheduler add_job and table "django_apscheduler_djangojob" not task Logging of job execution fails if the job is no longer available in the job store Oct 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants