Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bstiel committed Jul 14, 2018
0 parents commit b0bceeb
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .dockerignore
@@ -0,0 +1,6 @@
.env
.md
.yml
.git
README.md
Dockerfile
106 changes: 106 additions & 0 deletions .gitignore
@@ -0,0 +1,106 @@
broker/

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
8 changes: 8 additions & 0 deletions Dockerfile
@@ -0,0 +1,8 @@
FROM python:3
ENV LANG=C.UTF-8 LC_ALL=C.UTF-8

COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY ./ /app
WORKDIR /app
20 changes: 20 additions & 0 deletions app/app.py
@@ -0,0 +1,20 @@
import os
import logging
from flask import Flask, Response, request, jsonify
from tasks import long_running_task
from celery import chord


app = Flask(__name__)
app.config['DEBUG'] = True
logger = logging.getLogger(__name__)


@app.route('/', methods=['POST'])
def index():
task = long_running_task.s().delay()
return jsonify({'id': '', 'status': ''}), 201


if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
11 changes: 11 additions & 0 deletions app/tasks.py
@@ -0,0 +1,11 @@
import random
from worker import app


@app.task(bind=True, name='long_running_task')
def long_running_task(self):
n = 100000
total = 0
for i in range(0, n):
total += random.randint(1, 1000)
return total / n
25 changes: 25 additions & 0 deletions app/worker.py
@@ -0,0 +1,25 @@
import os
from celery import Celery


broker_url = os.getenv('CELERY_BROKER_URL', 'filesystem://')
broker_dir = os.getenv('CELERY_BROKER_FOLDER', './broker')

for f in ['out', 'processed']:
if not os.path.exists(os.path.join(broker_dir, f)):
os.makedirs(os.path.join(broker_dir, f))


app = Celery(__name__)
app.conf.update({
'broker_url': broker_url,
'broker_transport_options': {
'data_folder_in': os.path.join(broker_dir, 'out'),
'data_folder_out': os.path.join(broker_dir, 'out'),
'data_folder_processed': os.path.join(broker_dir, 'processed')
},
'imports': ('tasks',),
'result_persistent': False,
'task_serializer': 'json',
'result_serializer': 'json',
'accept_content': ['json']})
24 changes: 24 additions & 0 deletions docker-compose.yml
@@ -0,0 +1,24 @@
version: '3.5'
services:
app:
build: ./
image: &app app
restart: "no"
command: ["python", "app.py"]
environment: &env
- CELERY_BROKER_URL=filesystem://
- CELERY_BROKER_FOLDER=/app/broker
ports:
- 8000:5000
volumes:
- ./app:/app

worker:
build: ./
image: *app
restart: "no"
environment: *env
command: ["celery", "worker", "--app=worker.app", "--concurrency=1", "--hostname=worker@%h", "--loglevel=INFO"]
volumes:
- ./app:/app

2 changes: 2 additions & 0 deletions requirements.txt
@@ -0,0 +1,2 @@
celery==4.2.0
flask==1.0.2

0 comments on commit b0bceeb

Please sign in to comment.