forked from mjhea0/flask-redis-queue
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
212 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
FROM python:3.6.1 | ||
|
||
# set working directory | ||
RUN mkdir -p /usr/src/app | ||
WORKDIR /usr/src/app | ||
|
||
# add requirements (to leverage Docker cache) | ||
ADD ./requirements.txt /usr/src/app/requirements.txt | ||
|
||
# install requirements | ||
RUN pip install -r requirements.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
version: '2.1' | ||
|
||
services: | ||
|
||
web: | ||
build: . | ||
image: web | ||
container_name: web | ||
environment: | ||
- APP_SETTINGS=project.server.config.DevelopmentConfig | ||
ports: | ||
- '5001:5000' | ||
command: python manage.py runserver -h 0.0.0.0 | ||
volumes: | ||
- .:/usr/src/app | ||
depends_on: | ||
- redis | ||
|
||
worker: | ||
image: web | ||
container_name: worker | ||
environment: | ||
- APP_SETTINGS=project.server.config.DevelopmentConfig | ||
command: python manage.py run_worker | ||
volumes: | ||
- .:/usr/src/app | ||
depends_on: | ||
- redis | ||
|
||
redis: | ||
image: redis:3.2.11 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,17 @@ | ||
/* custom css */ | ||
|
||
.site-content { | ||
padding-top: 75px; | ||
} | ||
html { | ||
position: relative; | ||
min-height: 100%; | ||
} | ||
body { | ||
padding-top: 2rem; | ||
margin-bottom: 60px; | ||
} | ||
.footer { | ||
position: absolute; | ||
bottom: 0; | ||
width: 100%; | ||
height: 50px; | ||
line-height: 50px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,44 @@ | ||
// custom javascript | ||
|
||
$( document ).ready(function() { | ||
$( document ).ready(() => { | ||
console.log('Sanity Check!'); | ||
}); | ||
}); | ||
|
||
$('.btn').on('click', function() { | ||
$.ajax({ | ||
url: '/tasks', | ||
data: { type: $(this).data('type') }, | ||
method: 'POST' | ||
}) | ||
.done((res) => { | ||
getStatus(res.data.task_id) | ||
}) | ||
.fail((err) => { | ||
console.log(err) | ||
}) | ||
}) | ||
|
||
function getStatus(taskID) { | ||
$.ajax({ | ||
url: `/tasks/${taskID}`, | ||
method: 'GET' | ||
}) | ||
.done((res) => { | ||
const html = ` | ||
<tr> | ||
<td>${res.data.task_id}</td> | ||
<td>${res.data.task_status}</td> | ||
<td>${res.data.task_result}</td> | ||
</tr>` | ||
$('#tasks').prepend(html) | ||
const taskStatus = res.data.task_status; | ||
if (taskStatus === 'finished' || taskStatus === 'failed') return false; | ||
setTimeout(function() { | ||
getStatus(res.data.task_id); | ||
}, 1000); | ||
}) | ||
.fail((err) => { | ||
console.log(err) | ||
}) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
<footer> | ||
<footer class="footer"> | ||
<div class="container"> | ||
<small> | ||
<span>© <a href="http://mherman.org">Michael Herman</a></span> | ||
</small> | ||
<small><span class="text-muted">© <a href="http://mherman.org">Michael Herman</a></span></small> | ||
</div> | ||
</footer> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# project/server/main/tasks.py | ||
|
||
|
||
import time | ||
|
||
|
||
def create_task(task_type): | ||
time.sleep(int(task_type) * 10) | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,57 @@ | ||
# project/server/main/views.py | ||
|
||
|
||
from flask import render_template, Blueprint | ||
import redis | ||
from rq import Queue, push_connection, pop_connection | ||
from flask import current_app, render_template, Blueprint, jsonify, request | ||
|
||
from project.server.main.tasks import create_task | ||
|
||
main_blueprint = Blueprint('main', __name__,) | ||
|
||
|
||
@main_blueprint.route('/') | ||
@main_blueprint.route('/', methods=['GET']) | ||
def home(): | ||
return render_template('main/home.html') | ||
|
||
|
||
@main_blueprint.route('/tasks', methods=['POST']) | ||
def run_task(): | ||
task_type = request.form['type'] | ||
q = Queue() | ||
task = q.enqueue(create_task, task_type) | ||
response_object = { | ||
'status': 'success', | ||
'data': { | ||
'task_id': task.get_id() | ||
} | ||
} | ||
return jsonify(response_object), 202 | ||
|
||
|
||
@main_blueprint.route('/tasks/<task_id>', methods=['GET']) | ||
def get_status(task_id): | ||
q = Queue() | ||
task = q.fetch_job(task_id) | ||
if task: | ||
response_object = { | ||
'status': 'success', | ||
'data': { | ||
'task_id': task.get_id(), | ||
'task_status': task.get_status(), | ||
'task_result': task.result, | ||
} | ||
} | ||
else: | ||
response_object = {'status': 'error'} | ||
return jsonify(response_object) | ||
|
||
|
||
@main_blueprint.before_request | ||
def push_rq_connection(): | ||
push_connection(redis.from_url(current_app.config['REDIS_URL'])) | ||
|
||
|
||
@main_blueprint.teardown_request | ||
def pop_rq_connection(exception=None): | ||
pop_connection() |
Oops, something went wrong.