Skip to content

Commit 98bd15f

Browse files
authored
Merge pull request #85 from tecladocode/jose/cou-158-rest-add-lecture-on-deploying-rq-worker
2 parents f31ac52 + b3babe0 commit 98bd15f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+2428
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
![Create a new service of type background worker in Render.com](./assets/render-create-bg-worker.png)
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+
![Filling in the Render basic worker information with its name set to 'rest-api-background-worker', environment set to 'docker', and region set to 'Frankfurt'](./assets/render-bg-worker-basic-settings.png)
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+
![Environment variables added in Render.com including DATABASE_URL, REDIS_URL, MAILGUN_API_KEY, and MAILGUN_DOMAIN, with their respective values](./assets/render-bg-worker-env-vars.png)
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+
![Screenshot showing the Docker command in Render.com](./assets/render-bg-worker-docker-command.png)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
DATABASE_URL=
2+
MAILGUN_API_KEY=
3+
MAILGUN_DOMAIN=
4+
REDIS_URL=
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FLASK_APP=app
2+
FLASK_DEBUG=True
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.env
2+
.venv
3+
.vscode
4+
__pycache__
5+
data.db
6+
*.pyc
7+
.DS_Store
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.10.6
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# CONTRIBUTING
2+
3+
## How to run the Dockerfile locally
4+
5+
```
6+
docker run -dp 5000:5000 -w /app -v "$(pwd):/app" IMAGE_NAME sh -c "flask run"
7+
```

0 commit comments

Comments
 (0)