-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock.py
More file actions
executable file
·82 lines (62 loc) · 2.92 KB
/
clock.py
File metadata and controls
executable file
·82 lines (62 loc) · 2.92 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""
This is basically our cron. It's in New York time.
Error reporting isn't perfect here. Lint catches syntax errors, the deploy
process catches import errors, and as long as the functions you import and run
are decorated with @background or @background_command then any exceptions they
raise will get emailed. But there are still cracks in the system you can sneak
bugs into, such as running a @background function that's on a models.Manager.
See djaveThread/background.py for a more detailed explanation of why that fails
specifically, but the point is, it is possible to swallow errors here, so be
careful. Follow conventions. Don't get fancy.
If something fishy is going on despite all the precautions, check the logs.
heroku logs -d clock -t -a <this-heroku-app-name e.g. davidsmith7-stage>
You can also look in the djaveThread.models.LoggedCommand table to ensure your
@background_command is running successfully.
You can't import `background` here because any function you decorate with it
will trigger "Functions from the __main__ module cannot be processed by
workers". So you have to import functions that are decorated with background.
And they all should be, because the clock itself should never do any actual
work. It should only kick tasks off to background threads.
Hence, no work may be placed directly in this file. This file is strictly for
scheduling.
Cron scheduling:
https://apscheduler.readthedocs.io/en/latest/modules/triggers/cron.html
Interval scheduling:
https://apscheduler.readthedocs.io/en/latest/modules/triggers/interval.html
Uh, it seems to me that interval scheduling is not very reliable. I'm looking
at a case where it went 3 hours and 8 minutes for the 1 hour interval, and
there wasn't anything happening to block it or anything. But that could also be
our reddis provider.
"""
import os
import sys
import django
# Otherwise you get "django.core.exceptions.ImproperlyConfigured: Requested
# setting INSTALLED_APPS, but settings are not configured. You must either
# define the environment variable DJANGO_SETTINGS_MODULE or call
# settings.configure() before accessing settings." This is the entire reason we
# have flake8 (./lint.sh) globally ignore E402 module level import not at top
# of file
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'main.settings')
django.setup()
from apscheduler.schedulers.blocking import BlockingScheduler
"""
from djaveS3.models import (
clean_up_never_used, clean_up_no_longer_needed, resize_all)
from djaveAPI.models.webhook import send_webhooks
"""
sched = BlockingScheduler()
"""
@sched.scheduled_job('cron', hour='1')
def photo_stuff():
clean_up_never_used()
clean_up_no_longer_needed()
resize_all()
@sched.scheduled_job('cron', minute='0,5,10,15,20,25,30,35,40,45,50,55')
def webhook_stuff():
send_webhooks()
"""
# test.sh fires this file up with --no-run so continuous integration breaks
# when this file contains import errors.
if '--no-run' not in sys.argv:
sched.start()