Skip to content

aaa #16

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

aaa #16

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.8.0
3.10.2
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# base image
FROM python:3.8.0-alpine
FROM python:3.10-alpine

# set working directory
RUN mkdir -p /usr/src/app
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 Michael Herman
Copyright (c) 2022 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
Expand Down
17 changes: 16 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '3.7'
version: '3.8'

services:

Expand All @@ -14,3 +14,18 @@ services:
environment:
- FLASK_DEBUG=1
- APP_SETTINGS=project.server.config.DevelopmentConfig
depends_on:
- redis

worker:
image: web
command: python manage.py run_worker
volumes:
- .:/usr/src/app
environment:
- APP_SETTINGS=project.server.config.DevelopmentConfig
depends_on:
- redis

redis:
image: redis:6.2-alpine
11 changes: 10 additions & 1 deletion manage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# manage.py

import redis
from rq import Connection, Worker

import unittest

Expand All @@ -21,6 +22,14 @@ def test():
return 0
return 1

@cli.command("run_worker")
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__":
cli()
6 changes: 3 additions & 3 deletions project/client/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ $('.btn').on('click', function() {
method: 'POST'
})
.done((res) => {
getStatus(res.data.task_id)
getStatus(res.data.task_id);
})
.fail((err) => {
console.log(err)
console.log(err);
});
});

Expand All @@ -38,6 +38,6 @@ function getStatus(taskID) {
}, 1000);
})
.fail((err) => {
console.log(err)
console.log(err);
});
}
8 changes: 3 additions & 5 deletions project/client/templates/_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<!-- meta -->
<meta name="description" content="">
<meta name="author" content="">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- styles -->
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
{{ bootstrap.load_css() }}
<link href="{{url_for('static', filename='main.css')}}" rel="stylesheet" media="screen">
{% block css %}{% endblock %}
</head>
Expand All @@ -24,9 +24,7 @@
{% include 'footer.html' %}

<!-- scripts -->
<script src="//code.jquery.com/jquery-3.2.1.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>
{{ bootstrap.load_js() }}
<script src="{{url_for('static', filename='main.js')}}" type="text/javascript"></script>
{% block js %}{% endblock %}

Expand Down
7 changes: 2 additions & 5 deletions project/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import os

from flask import Flask
from flask_bootstrap import Bootstrap

# instantiate the extensions
bootstrap = Bootstrap()
from flask_bootstrap import Bootstrap4


def create_app(script_info=None):
Expand All @@ -24,7 +21,7 @@ def create_app(script_info=None):
app.config.from_object(app_settings)

# set up extensions
bootstrap.init_app(app)
Bootstrap4(app)

# register blueprints
from project.server.main.views import main_blueprint
Expand Down
2 changes: 2 additions & 0 deletions project/server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class BaseConfig(object):
"""Base configuration."""

WTF_CSRF_ENABLED = True
REDIS_URL = "redis://redis:6379/0"
QUEUES = ["default"]


class DevelopmentConfig(BaseConfig):
Expand Down
9 changes: 9 additions & 0 deletions project/server/main/tasks.py
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
17 changes: 15 additions & 2 deletions project/server/main/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# project/server/main/views.py

import redis
from rq import Queue, Connection

from flask import render_template, Blueprint, jsonify, request
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 @@ -14,7 +18,16 @@ def home():
@main_blueprint.route("/tasks", methods=["POST"])
def run_task():
task_type = request.form["type"]
return jsonify(task_type), 202
with Connection(redis.from_url(current_app.config["REDIS_URL"])):
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"])
Expand Down
10 changes: 6 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Flask==1.1.1
Flask-Bootstrap==3.3.7.1
Flask-Testing==0.7.1
Flask-WTF==0.14.2
Bootstrap-Flask==2.0.0
Flask==2.0.2
Flask-Testing==0.8.1
Flask-WTF==1.0.0
redis==4.1.1
rq==1.10.1