-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjob.py
49 lines (41 loc) · 1.55 KB
/
job.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import logging
from datetime import datetime, timedelta
from rq_scheduler import Scheduler
from config import Config
from sms import send_sms
from db import db, set_item, get_item
scheduler = Scheduler(interval=5, connection=db)
logger = logging.getLogger('minder.job')
def schedule_the_job(item):
try:
logger.info('Aquiring lock before scheduling')
with db.lock('the_job_lock'):
scheduled = scheduler.enqueue_in(timedelta(seconds=Config.REMINDER_DELAY),
the_job,
item)
set_item('the_job', scheduled.id)
logger.info('Released lock after scheduling')
except:
logger.exception('Exception occurred')
raise
def the_job(item):
try:
logger.info('Running job')
send_sms(Config.USER_PHONE_NUMBER, 'Did you remember to turn off the {}'.format(item))
logger.info('Sent sms')
schedule_the_job(item)
logger.info('Scheduled job to run again')
except:
logger.exception('Exception occurred')
raise
def cancel_the_job():
with db.lock('the_job_lock'):
scheduled = get_item('the_job')
logger.info('Trying to cancel job {}'.format(scheduled))
logger.info('All jobs {}'.format(scheduler.get_jobs()))
if scheduled in scheduler:
logger.info('Calling scheduler.cancel()')
scheduler.cancel(scheduled)
else:
logger.info('Unable to find job to cancel')
logger.info('Released lock after cancelling')