Skip to content

Commit 85da564

Browse files
committed
refactored
1 parent db16258 commit 85da564

File tree

7 files changed

+86
-3
lines changed

7 files changed

+86
-3
lines changed

docker-compose.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,26 @@ 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:3.2.11
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

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()
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.6.4-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
@@ -7,6 +7,8 @@
77
class BaseConfig(object):
88
"""Base configuration."""
99
WTF_CSRF_ENABLED = True
10+
REDIS_URL = 'redis://redis:6379/0'
11+
QUEUES = ['default']
1012

1113

1214
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: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
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, \
7+
request, current_app
8+
9+
from project.server.main.tasks import create_task
10+
511

612
main_blueprint = Blueprint('main', __name__,)
713

@@ -14,9 +20,32 @@ def home():
1420
@main_blueprint.route('/tasks', methods=['POST'])
1521
def run_task():
1622
task_type = request.form['type']
17-
return jsonify(task_type), 202
23+
with Connection(redis.from_url(current_app.config['REDIS_URL'])):
24+
q = Queue()
25+
task = q.enqueue(create_task, task_type)
26+
response_object = {
27+
'status': 'success',
28+
'data': {
29+
'task_id': task.get_id()
30+
}
31+
}
32+
return jsonify(response_object), 202
1833

1934

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

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ Flask-Bootstrap==3.3.7.1
33
Flask-Script==2.0.6
44
Flask-Testing==0.6.2
55
Flask-WTF==0.14.2
6+
redis==2.10.6
7+
rq==0.10.0

0 commit comments

Comments
 (0)