-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Locking backend with database (#185)
* Locking backend with database * documentation of database lock
- Loading branch information
Showing
7 changed files
with
78 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from django_cron.backends.lock.base import DjangoCronJobLock | ||
from django_cron.models import CronJobLock | ||
from django.db import transaction | ||
|
||
|
||
class DatabaseLock(DjangoCronJobLock): | ||
""" | ||
Locking cron jobs with database. Its good when you have not parallel run and want to make sure 2 jobs won't be | ||
fired at the same time - which may happened when job execution is longer that job interval. | ||
""" | ||
|
||
@transaction.atomic | ||
def lock(self): | ||
lock, created = CronJobLock.objects.get_or_create(job_name=self.job_name) | ||
if lock.locked: | ||
return False | ||
else: | ||
lock.locked = True | ||
lock.save() | ||
return True | ||
|
||
@transaction.atomic | ||
def release(self): | ||
lock = CronJobLock.objects.filter( | ||
job_name=self.job_name, | ||
locked=True | ||
).first() | ||
lock.locked = False | ||
lock.save() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import models, migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('django_cron', '0002_remove_max_length_from_CronJobLog_message'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='CronJobLock', | ||
fields=[ | ||
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), | ||
('job_name', models.CharField(max_length=200, unique=True)), | ||
('locked', models.BooleanField(default=False)), | ||
], | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters