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

Patch 1 #195

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions django_apscheduler/jobstores.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from apscheduler.schedulers.base import BaseScheduler

from django import db
from django.db import transaction, IntegrityError
from django.db import transaction, IntegrityError, router

from django_apscheduler import util
from django_apscheduler.models import DjangoJob, DjangoJobExecution
Expand Down Expand Up @@ -233,7 +233,8 @@ def get_all_jobs(self):

@util.retry_on_db_operational_error
def add_job(self, job: AppSchedulerJob):
with transaction.atomic():
db = router.db_for_write(DjangoJob)
with transaction.atomic(using=db):
try:
return DjangoJob.objects.create(
id=job.id,
Expand All @@ -246,7 +247,8 @@ def add_job(self, job: AppSchedulerJob):
@util.retry_on_db_operational_error
def update_job(self, job: AppSchedulerJob):
# Acquire lock for update
with transaction.atomic():
db = router.db_for_write(DjangoJob)
with transaction.atomic(using=db):
try:
db_job = DjangoJob.objects.select_for_update().get(id=job.id)

Expand All @@ -262,7 +264,8 @@ def update_job(self, job: AppSchedulerJob):

@util.retry_on_db_operational_error
def remove_job(self, job_id: str):
with transaction.atomic():
db = router.db_for_write(DjangoJob)
with transaction.atomic(using=db):
try:
DjangoJob.objects.select_for_update().get(id=job_id).delete()
except DjangoJob.DoesNotExist:
Expand Down
11 changes: 6 additions & 5 deletions django_apscheduler/migrations/0005_migrate_name_to_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,26 @@ def migrate_name_to_id(apps, schema_editor):
JobExecutionModel = apps.get_model("django_apscheduler", "DjangoJobExecution")
migrated_id_mappings = {}
migrated_job_executions = []

db = schema_editor.connection.alias

# Copy 'name' to 'id'.
for job in JobModel.objects.all():
for job in JobModel.objects.using(db).all():
migrated_id_mappings[job.id] = job.name
job.id = job.name
job.name = f"{job.name}_tmp"
job.save()

# Update all job execution references
for job_execution in JobExecutionModel.objects.filter(
for job_execution in JobExecutionModel.objects.using(db).filter(
job_id__in=migrated_id_mappings
):
job_execution.job_id = migrated_id_mappings[job_execution.job_id]
migrated_job_executions.append(job_execution)

JobExecutionModel.objects.bulk_update(migrated_job_executions, ["job_id"])
JobExecutionModel.objects.using(db).bulk_update(migrated_job_executions, ["job_id"])

# Remove old jobs
JobModel.objects.filter(id__in=migrated_id_mappings).delete()
JobModel.objects.using(db).filter(id__in=migrated_id_mappings).delete()


class Migration(migrations.Migration):
Expand Down
5 changes: 3 additions & 2 deletions django_apscheduler/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import timedelta, datetime

from django.db import models, transaction
from django.db import models, transaction, routers
from django.db.models import UniqueConstraint
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -162,7 +162,8 @@ def atomic_update_or_create(
finished = finished.timestamp()

try:
with transaction.atomic():
db = router.db_for_write(DjangoJobExecution)
with transaction.atomic(using=db):
job_execution = DjangoJobExecution.objects.select_for_update().get(
job_id=job_id, run_time=run_time
)
Expand Down