|
| 1 | +# Deploy the rq background worker to Render.com |
| 2 | + |
| 3 | +When deploying to Render.com, it's much easier if we don't have to pass the `REDIS_URL` and the queue name directly to the command. |
| 4 | + |
| 5 | +So instead, let's create a `settings.py` file and put our `rq` worker configuration there: |
| 6 | + |
| 7 | +```python title="settings.py" |
| 8 | +import os |
| 9 | +from dotenv import load_dotenv |
| 10 | + |
| 11 | +load_dotenv() |
| 12 | + |
| 13 | +REDIS_URL = os.getenv("REDIS_URL", "redis://localhost:6379") |
| 14 | +QUEUES = ["emails", "default"] |
| 15 | +``` |
| 16 | + |
| 17 | +The names of the variables are important, see [the documentation](https://python-rq.org/docs/workers/#using-a-config-file) for all the options that are currently supported. |
| 18 | + |
| 19 | +To run the `rq` worker using this settings file use `rq worker -c settings`. |
| 20 | + |
| 21 | +Let's add this to our repo, and then deploy the background worker to Render.com. |
| 22 | + |
| 23 | +First create a new background worker: |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +Then, give it a name and fill in its basic settings. The default works for the most part. Make sure it's in the same region as or close to your Postgres and Redis databases: |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +Add the environment variables it needs. Although in this case it doesn't need the `DATABASE_URL`, you can add it if you will be adding other tasks that do use the database in the near future. If not, leave it out. |
| 32 | + |
| 33 | +:::warning Internal URL |
| 34 | +If your Redis database is with Render.com, you'd want to use the Redis database **Internal URL**, but I encountered some issues with it where the `redis` package didn't recognise the URL. Try it, but fall back to the external URL if it doesn't work. |
| 35 | +::: |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +Finally, this "background worker" is just a Python program without networking capabilities. So if we leave it as is, it will actually just run our Dockerfile and the Dockerfile's `CMD` command (which starts our web application). Therefore we want to give it a custom Docker command that starts the background worker. |
| 40 | + |
| 41 | +In that command, I'll go into the `/app` directory of the Docker container, and run the `rq` worker passing in the `settings.py` file. |
| 42 | + |
| 43 | +The command is `/bin/bash -c cd /app && rq worker -c settings`. |
| 44 | + |
| 45 | +This is what it looks like in Render.com: |
| 46 | + |
| 47 | + |
0 commit comments