summaryrefslogtreecommitdiff
path: root/docs/patterns/index.md
blob: 512918eb469dbc402afef680304d2752c9b0a029 (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
---
title: "RQ: Using RQ on Heroku"
layout: patterns
---

## Using RQ on Heroku

To setup RQ on [Heroku][1], first add it to your
`requirements.txt` file:

    redis>=3
    rq>=0.13

Create a file called `run-worker.py` with the following content (assuming you
are using [Heroku Data For Redis][2] with Heroku):

```python
import os
import redis
from redis import Redis
from rq import Queue, Connection
from rq.worker import HerokuWorker as Worker


listen = ['high', 'default', 'low']

redis_url = os.getenv('REDIS_URL')
if not redis_url:
    raise RuntimeError("Set up Heroku Data For Redis first, \
    make sure the its config var is named 'REDIS_URL'.")
    
conn = redis.from_url(redis_url)

if __name__ == '__main__':
    with Connection(conn):
        worker = Worker(map(Queue, listen))
        worker.work()
```

Then, add the command to your `Procfile`:

    worker: python -u run-worker.py

Now, all you have to do is spin up a worker:

```console
$ heroku scale worker=1
```

If the from_url function fails to parse your credentials, you might need to do so manually:

```console
conn = redis.Redis(
    host=host,
    password=password,
    port=port,
    ssl=True,
    ssl_cert_reqs=None
)
```
The details are from the 'settings' page of your Redis add-on on the Heroku dashboard.

and for using the cli:

```console
rq info --config rq_conf
``````

Where the rq_conf.py file looks like:
```console
REDIS_HOST = "host"
REDIS_PORT = port
REDIS_PASSWORD = "password"
REDIS_SSL = True
REDIS_SSL_CA_CERTS = None
REDIS_DB = 0
REDIS_SSL_CERT_REQS = None
``````

## Putting RQ under foreman

[Foreman][3] is probably the process manager you use when you host your app on
Heroku, or just because it's a pretty friendly tool to use in development.

When using RQ under `foreman`, you may experience that the workers are a bit quiet sometimes. This is because of Python buffering the output, so `foreman`
cannot (yet) echo it. Here's a related [Wiki page][4].

Just change the way you run your worker process, by adding the `-u` option (to
force stdin, stdout and stderr to be totally unbuffered):

    worker: python -u run-worker.py

[1]: https://heroku.com
[2]: https://devcenter.heroku.com/articles/heroku-redis
[3]: https://github.com/ddollar/foreman
[4]: https://github.com/ddollar/foreman/wiki/Missing-Output
[5]: https://elements.heroku.com/addons/heroku-redis