Skip to content

Commit 80a1302

Browse files
committed
bump dependencies, lint, update readme
1 parent 8d8956a commit 80a1302

File tree

8 files changed

+91
-4
lines changed

8 files changed

+91
-4
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@ Spin up the containers:
1010
$ docker-compose up -d --build
1111
```
1212

13-
Open your browser to http://localhost:5004
13+
Open your browser to http://localhost:5004 to view the app or to http://localhost:9181 to view the RQ dashboard.
14+
15+
### Want to learn how to build this?
16+
17+
Check out the [post](https://testdriven.io/asynchronous-tasks-with-flask-and-redis-queue).

docker-compose.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,28 @@ services:
1414
environment:
1515
- FLASK_DEBUG=1
1616
- APP_SETTINGS=project.server.config.DevelopmentConfig
17+
depends_on:
18+
- redis
19+
20+
worker:
21+
image: web
22+
command: python manage.py run_worker
23+
volumes:
24+
- .:/usr/src/app
25+
environment:
26+
- APP_SETTINGS=project.server.config.DevelopmentConfig
27+
depends_on:
28+
- redis
29+
30+
redis:
31+
image: redis:5.0.7-alpine
32+
33+
dashboard:
34+
build: ./project/dashboard
35+
image: dashboard
36+
container_name: dashboard
37+
ports:
38+
- 9181:9181
39+
command: rq-dashboard -H redis
40+
depends_on:
41+
- redis

manage.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import unittest
55

6+
import redis
7+
from rq import Connection, Worker
68
from flask.cli import FlaskGroup
79

810
from project.server import create_app
@@ -22,5 +24,14 @@ def test():
2224
return 1
2325

2426

27+
@cli.command("run_worker")
28+
def run_worker():
29+
redis_url = app.config["REDIS_URL"]
30+
redis_connection = redis.from_url(redis_url)
31+
with Connection(redis_connection):
32+
worker = Worker(app.config["QUEUES"])
33+
worker.work()
34+
35+
2536
if __name__ == "__main__":
2637
cli()

project/dashboard/Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM python:3.8.0-alpine
2+
3+
RUN pip install rq-dashboard
4+
5+
EXPOSE 9181
6+
7+
CMD ["rq-dashboard"]

project/server/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class BaseConfig(object):
99
"""Base configuration."""
1010

1111
WTF_CSRF_ENABLED = True
12+
REDIS_URL = "redis://redis:6379/0"
13+
QUEUES = ["default"]
1214

1315

1416
class DevelopmentConfig(BaseConfig):

project/server/main/tasks.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# project/server/main/tasks.py
2+
3+
4+
import time
5+
6+
7+
def create_task(task_type):
8+
time.sleep(int(task_type) * 10)
9+
return True

project/server/main/views.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# project/server/main/views.py
22

33

4-
from flask import render_template, Blueprint, jsonify, request
4+
import redis
5+
from rq import Queue, Connection
6+
from flask import render_template, Blueprint, jsonify, request, current_app
7+
8+
from project.server.main.tasks import create_task
59

610
main_blueprint = Blueprint("main", __name__,)
711

@@ -14,9 +18,32 @@ def home():
1418
@main_blueprint.route("/tasks", methods=["POST"])
1519
def run_task():
1620
task_type = request.form["type"]
17-
return jsonify(task_type), 202
21+
with Connection(redis.from_url(current_app.config["REDIS_URL"])):
22+
q = Queue()
23+
task = q.enqueue(create_task, task_type)
24+
response_object = {
25+
"status": "success",
26+
"data": {
27+
"task_id": task.get_id()
28+
}
29+
}
30+
return jsonify(response_object), 202
1831

1932

2033
@main_blueprint.route("/tasks/<task_id>", methods=["GET"])
2134
def get_status(task_id):
22-
return jsonify(task_id)
35+
with Connection(redis.from_url(current_app.config["REDIS_URL"])):
36+
q = Queue()
37+
task = q.fetch_job(task_id)
38+
if task:
39+
response_object = {
40+
"status": "success",
41+
"data": {
42+
"task_id": task.get_id(),
43+
"task_status": task.get_status(),
44+
"task_result": task.result,
45+
},
46+
}
47+
else:
48+
response_object = {"status": "error"}
49+
return jsonify(response_object)

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ Flask==1.1.1
22
Flask-Bootstrap==3.3.7.1
33
Flask-Testing==0.7.1
44
Flask-WTF==0.14.2
5+
redis==3.3.11
6+
rq==1.1.0

0 commit comments

Comments
 (0)