-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeflux.py
51 lines (37 loc) · 1.31 KB
/
deflux.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Application written with Flask
# http://flask.pocoo.org/docs/0.12/
from flask import Flask, jsonify, request
from redis import Redis
from celery import Celery
from cvar import Allocator
# Connect to Redis
redis = Redis(host='redis', db=0, socket_connect_timeout=2, socket_timeout=2)
app = Flask(__name__)
app.config['CELERY_BROKER_URL'] = 'redis://redis:6379'
app.config['CELERY_RESULT_BACKEND'] = 'redis://redis:6379'
# Initialize Celery
celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)
# Celery task for running allocation
# http://docs.celeryproject.org/en/latest/
@celery.task()
def allocate(coins):
allocator = Allocator(coins=coins)
allocation = allocator.allocate()
return allocation
@app.route('/api/allocations/<task_id>/', methods=['GET'])
def get_allocation(task_id):
task = allocate.AsyncResult(task_id)
response = {'state': task.state}
return jsonify(response)
@app.route('/api/allocations/', methods=['POST'])
def create_allocation():
data = request.get_json()
coins = data['coins']
# Initiate a new allocation job
result = allocate.delay(coins)
# Return the job ID (this will be through the job queue processor)
response = {'task_id': result.id}
return jsonify(response)
if __name__ == '__main__':
app.run()