Skip to content

updated python version, converted to the flask cli, added rq dashboard #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.6.1
3.6.4
13 changes: 0 additions & 13 deletions .travis.yml

This file was deleted.

5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM python:3.6.1
# base image
FROM python:3.6.4-alpine

# set working directory
RUN mkdir -p /usr/src/app
Expand All @@ -8,4 +9,4 @@ WORKDIR /usr/src/app
ADD ./requirements.txt /usr/src/app/requirements.txt

# install requirements
RUN pip install -r requirements.txt
RUN pip install -r requirements.txt
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
The MIT License (MIT)
Copyright (c) 2017 Michael Herman
Copyright (c) 2018 Michael Herman

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,3 @@ $ docker-compose up -d
```

Open your browser to http://localhost:5001

### Example

![](app.png)
Binary file removed app.png
Binary file not shown.
22 changes: 15 additions & 7 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
version: '2.1'
version: '3.5'

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
command: python manage.py run -h 0.0.0.0
volumes:
- .:/usr/src/app
environment:
- FLASK_DEBUG=1
- APP_SETTINGS=project.server.config.DevelopmentConfig
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
environment:
- APP_SETTINGS=project.server.config.DevelopmentConfig
depends_on:
- redis

redis:
image: redis:3.2.11

dashboard:
build: ./project/dashboard
image: dashboard
container_name: dashboard
ports:
- '9181:9181'
command: rq-dashboard -H redis
15 changes: 8 additions & 7 deletions manage.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# manage.py


import os
import unittest

import redis
from rq import Connection, Worker
from flask_script import Manager
from flask.cli import FlaskGroup

from project.server import app
from project.server import create_app


manager = Manager(app)
app = create_app()
cli = FlaskGroup(create_app=create_app)


@manager.command
@cli.command()
def test():
"""Runs the unit tests without test coverage."""
tests = unittest.TestLoader().discover('project/tests', pattern='test*.py')
Expand All @@ -24,13 +24,14 @@ def test():
return 1


@manager.command
@cli.command()
def run_worker():
redis_url = app.config['REDIS_URL']
redis_connection = redis.from_url(redis_url)
with Connection(redis_connection):
worker = Worker(app.config['QUEUES'])
worker.work()


if __name__ == '__main__':
manager.run()
cli()
9 changes: 4 additions & 5 deletions project/client/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ $('.btn').on('click', function() {
})
.fail((err) => {
console.log(err)
})
})
});
});

function getStatus(taskID) {
$.ajax({
Expand All @@ -30,7 +30,7 @@ function getStatus(taskID) {
<td>${res.data.task_status}</td>
<td>${res.data.task_result}</td>
</tr>`
$('#tasks').prepend(html)
$('#tasks').prepend(html);
const taskStatus = res.data.task_status;
if (taskStatus === 'finished' || taskStatus === 'failed') return false;
setTimeout(function() {
Expand All @@ -39,6 +39,5 @@ function getStatus(taskID) {
})
.fail((err) => {
console.log(err)
})
});
}

6 changes: 3 additions & 3 deletions project/client/templates/_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<meta name="author" content="">
<meta name="viewport" content="width=device-width,initial-scale=1">
<!-- styles -->
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<link href="{{url_for('static', filename='main.css')}}" rel="stylesheet" media="screen">
{% block css %}{% endblock %}
</head>
Expand All @@ -25,8 +25,8 @@

<!-- scripts -->
<script src="//code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<script src="{{url_for('static', filename='main.js')}}" type="text/javascript"></script>
{% block js %}{% endblock %}

Expand Down
7 changes: 7 additions & 0 deletions project/dashboard/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM python:3.6.4-alpine

RUN pip install rq-dashboard

EXPOSE 9181

CMD ["rq-dashboard"]
33 changes: 23 additions & 10 deletions project/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,31 @@
from flask_bootstrap import Bootstrap


app = Flask(
__name__,
template_folder='../client/templates',
static_folder='../client/static'
)
# instantiate the extensions
bootstrap = Bootstrap()


app_settings = os.getenv('APP_SETTINGS', 'project.server.config.DevelopmentConfig')
app.config.from_object(app_settings)
def create_app(script_info=None):

bootstrap = Bootstrap(app)
# instantiate the app
app = Flask(
__name__,
template_folder='../client/templates',
static_folder='../client/static'
)

# set config
app_settings = os.getenv('APP_SETTINGS')
app.config.from_object(app_settings)

from project.server.main.views import main_blueprint
app.register_blueprint(main_blueprint)
# set up extensions
bootstrap.init_app(app)

# register blueprints
from project.server.main.views import main_blueprint
app.register_blueprint(main_blueprint)

# shell context for flask cli
app.shell_context_processor({'app': app})

return app
9 changes: 1 addition & 8 deletions project/server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,18 @@

class BaseConfig(object):
"""Base configuration."""
DEBUG = False
WTF_CSRF_ENABLED = True
REDIS_URL = 'redis://redis:6379/0'
QUEUES = ['default']


class DevelopmentConfig(BaseConfig):
"""Development configuration."""
DEBUG = True
WTF_CSRF_ENABLED = False


class TestingConfig(BaseConfig):
"""Testing configuration."""
DEBUG = True
TESTING = True
WTF_CSRF_ENABLED = False
PRESERVE_CONTEXT_ON_EXCEPTION = False


class ProductionConfig(BaseConfig):
"""Production configuration."""
DEBUG = False
2 changes: 1 addition & 1 deletion project/server/main/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

def create_task(task_type):
time.sleep(int(task_type) * 10)
return True
return True
28 changes: 11 additions & 17 deletions project/server/main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@


import redis
from rq import Queue, push_connection, pop_connection
from flask import current_app, render_template, Blueprint, jsonify, request
from rq import Queue, Connection
from flask import render_template, Blueprint, jsonify, \
request, current_app

from project.server.main.tasks import create_task


main_blueprint = Blueprint('main', __name__,)


Expand All @@ -18,8 +20,9 @@ def home():
@main_blueprint.route('/tasks', methods=['POST'])
def run_task():
task_type = request.form['type']
q = Queue()
task = q.enqueue(create_task, task_type)
with Connection(redis.from_url(current_app.config['REDIS_URL'])):
q = Queue()
task = q.enqueue(create_task, task_type)
response_object = {
'status': 'success',
'data': {
Expand All @@ -31,8 +34,9 @@ def run_task():

@main_blueprint.route('/tasks/<task_id>', methods=['GET'])
def get_status(task_id):
q = Queue()
task = q.fetch_job(task_id)
with Connection(redis.from_url(current_app.config['REDIS_URL'])):
q = Queue()
task = q.fetch_job(task_id)
if task:
response_object = {
'status': 'success',
Expand All @@ -41,17 +45,7 @@ def get_status(task_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()
4 changes: 3 additions & 1 deletion project/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

from flask_testing import TestCase

from project.server import app
from project.server import create_app

app = create_app()


class BaseTestCase(TestCase):
Expand Down
18 changes: 4 additions & 14 deletions project/tests/test__config.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# project/server/tests/test_config.py
# project/server/tests/test__config.py


import unittest

from flask import current_app
from flask_testing import TestCase

from project.server import app
from project.server import create_app

app = create_app()


class TestDevelopmentConfig(TestCase):
Expand Down Expand Up @@ -34,17 +36,5 @@ def test_app_is_testing(self):
self.assertTrue(app.config['WTF_CSRF_ENABLED'] is False)


class TestProductionConfig(TestCase):

def create_app(self):
app.config.from_object('project.server.config.ProductionConfig')
return app

def test_app_is_production(self):
self.assertFalse(current_app.config['TESTING'])
self.assertTrue(app.config['DEBUG'] is False)
self.assertTrue(app.config['WTF_CSRF_ENABLED'] is True)


if __name__ == '__main__':
unittest.main()
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Flask-Script==2.0.6
Flask-Testing==0.6.2
Flask-WTF==0.14.2
redis==2.10.6
rq==0.8.2
rq==0.10.0