-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #175 from glogiotatidis/clockjob
Add clock job to update product details.
- Loading branch information
Showing
5 changed files
with
86 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
web: ./bin/run-prod.sh | ||
clock: ./manage.py runscript cron |
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
Empty file.
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,69 @@ | ||
from __future__ import print_function | ||
import datetime | ||
import os | ||
import sys | ||
|
||
from django.core.management import call_command | ||
from django.conf import settings | ||
|
||
import requests | ||
from apscheduler.schedulers.blocking import BlockingScheduler | ||
|
||
|
||
schedule = BlockingScheduler() | ||
|
||
|
||
class scheduled_job(object): | ||
"""Decorator for scheduled jobs. Takes same args as apscheduler.schedule_job.""" | ||
def __init__(self, *args, **kwargs): | ||
self.args = args | ||
self.kwargs = kwargs | ||
|
||
def __call__(self, fn): | ||
job_name = fn.__name__ | ||
self.name = job_name | ||
self.callback = fn | ||
schedule.add_job(self.run, id=job_name, *self.args, **self.kwargs) | ||
self.log('Registered.') | ||
return self.run | ||
|
||
def run(self): | ||
self.log('starting') | ||
try: | ||
self.callback() | ||
except Exception as e: | ||
self.log('CRASHED: {}'.format(e)) | ||
raise | ||
else: | ||
self.log('finished successfully') | ||
|
||
def log(self, message): | ||
msg = '[{}] Clock job {}@{}: {}'.format( | ||
datetime.datetime.utcnow(), self.name, | ||
os.getenv('DEIS_APP', 'default_app'), message) | ||
print(msg, file=sys.stderr) | ||
|
||
|
||
def ping_dms(function): | ||
"""Pings Dead Man's Snitch after job completion if URL is set.""" | ||
def _ping(): | ||
function() | ||
if settings.DEAD_MANS_SNITCH_URL: | ||
utcnow = datetime.datetime.utcnow() | ||
payload = {'m': 'Run {} on {}'.format(function.__name__, utcnow.isoformat())} | ||
requests.get(settings.DEAD_MANS_SNITCH_URL, params=payload) | ||
_ping.__name__ = function.__name__ | ||
return _ping | ||
|
||
|
||
@scheduled_job('interval', days=1, max_instances=1, coalesce=True) | ||
@ping_dms | ||
def job_update_product_details(): | ||
call_command('update_product_details') | ||
|
||
|
||
def run(): | ||
try: | ||
schedule.start() | ||
except (KeyboardInterrupt, SystemExit): | ||
pass |
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