-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.py
More file actions
executable file
·50 lines (37 loc) · 1.62 KB
/
worker.py
File metadata and controls
executable file
·50 lines (37 loc) · 1.62 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
"""
The background thread might be held up with a long running task! Check
https://keys.hihoward.com/admin/commands/commandrun/
You can see the production logs of this worker thusly:
heroku logs -t -p worker -a <this app's name on Heroku>
You can see how much memory the redis queue is using by going here and clicking
on Redis To Go
https://dashboard.heroku.com/apps/<this app's name on Heroku>
You can restart the worker (not the same as restarting the queue!) with
heroku ps:restart worker -a <this app's name on Heroku>
Errors are automatically caught and mailed to us as long as the
util/background.py @background decorator is the mechanism used to pass tasks
to the worker.
Communicating to this worker requires a redis queue with limited memory. A
catastrophic failure we had one time was, I was passing bulky data to a
@background function. This filled up the queue, which froze, and then the
background worker became unreachable until I restarted the redis queue.
See util/background.py for many more comments, such as how to restart the
queue.
"""
import os
import django
from django.conf import settings
import redis
from rq import Worker, Queue, Connection
# This file is imported by both workers and web. Settings are already
# configured on web but not on workers.
if not settings.configured:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'main.settings')
django.setup()
listen = ['high', 'default', 'low']
redis_url = os.getenv('REDISTOGO_URL', 'redis://localhost:6379')
conn = redis.from_url(redis_url)
if __name__ == '__main__':
with Connection(conn):
worker = Worker(map(Queue, listen))
worker.work()