-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
69 lines (51 loc) · 1.46 KB
/
app.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from flask import Flask, request, jsonify
from celery.result import AsyncResult
from tasks import celery_app, calc_tags, calc_tags_long
app = Flask(__name__)
@app.route("/tags", methods=["POST"])
def tags():
request_data = request.get_json()
if not request_data:
return "WHERE IS MY JSON, MATE?"
url = None
if "website" in request_data:
url = request_data["website"]
if not url:
return "GOT NO URL"
task = calc_tags.delay(url)
response = {
"task_id": task.id,
}
return jsonify(response)
@app.route("/tags/<task_id>", methods=["GET"])
def tags_get(task_id):
task = AsyncResult(task_id, app=celery_app)
if task.state == "PENDING":
response = {
"state": task.state,
"ready": task.ready(),
}
return jsonify(response)
result = task.get(timeout=10)
response = {
"the_data": result,
}
return jsonify(response)
@app.route("/tags_long", methods=["POST"])
def tags_long():
"""create long running task"""
request_data = request.get_json()
if not request_data:
return "WHERE IS MY JSON, MATE?"
url = None
if "website" in request_data:
url = request_data["website"]
if not url:
return "GOT NO URL"
task = calc_tags_long.delay(url)
response = {
"task_id": task.id,
}
return jsonify(response)
if __name__ == "__main__":
app.run(debug=False, host="0.0.0.0", port=5000)