-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtaskqueue.py
More file actions
181 lines (152 loc) · 4.71 KB
/
taskqueue.py
File metadata and controls
181 lines (152 loc) · 4.71 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#
# SPDX-FileCopyrightText: 2013-2021 Sequent Tech Inc <legal@sequentech.io>
#
# SPDX-License-Identifier: AGPL-3.0-only
#
import pickle
import base64
import json
from frestq.app import app, db
from frestq.utils import loads, dumps
from frestq.tasks import SimpleTask, TaskError
from models import Election, Authority, QueryQueue
from create_election.performer_jobs import check_election_data
import threading
def safe_dequeue():
try:
dequeue_task()
return True
except Exception as e:
return False
def start_queue(queue_continue=False):
if not queue_continue:
doing = db.session.query(QueryQueue).all()
for i in doing:
i.doing = False
db.session.delete(i)
else:
doing = db.session.query(QueryQueue).filter(QueryQueue.doing == True)
for i in doing:
i.doing = False
db.session.commit()
t = threading.Thread(target=safe_dequeue)
t.start()
def dequeue_task():
doing = db.session.query(QueryQueue).filter(QueryQueue.doing == True)
if not doing.count():
todo = db.session.query(QueryQueue)\
.with_for_update(nowait=True, of=QueryQueue)\
.order_by(QueryQueue.id)\
.first()
todo.doing = True
db.session.commit()
apply_task(todo.task, todo.data)
def queue_task(task='election', data=None):
data = data or {}
d = json.dumps(data)
qq = QueryQueue(task=task, data=d)
db.session.add(qq)
db.session.commit()
safe_dequeue()
return qq.id
def apply_task(task, data):
d = pickle.loads(base64.b64decode(data.encode('utf-8')))
if task == 'election':
r = election_task(d)
if not r:
end_task()
if task == 'tally':
r = tally_task(d)
if not r:
end_task()
def end_task():
doing = db.session.query(QueryQueue)\
.with_for_update(nowait=True)\
.filter(QueryQueue.doing == True)\
.one()
db.session.delete(doing)
db.session.commit()
safe_dequeue()
### TASKS
def election_task(data):
if not data:
print("invalid json")
return False
try:
check_election_data(data, True)
except Exception as e:
print("ERROR", e)
return False
e = Election(
id = data['id'],
title = data['title'][:255],
description = data['description'],
questions = dumps(data['questions']),
start_date = data['start_date'],
end_date = data['end_date'],
callback_url = data['callback_url'],
num_parties = len(data['authorities']),
threshold_parties = len(data['authorities']),
status = 'creating'
)
db.session.add(e)
for auth_data in data['authorities']:
authority = Authority(
name = auth_data['name'],
ssl_cert = auth_data['ssl_cert'],
orchestra_url = auth_data['orchestra_url'],
election_id = data['id']
)
db.session.add(authority)
db.session.commit()
task = SimpleTask(
receiver_url=app.config.get('ROOT_URL', ''),
action="create_election",
queue="launch_task",
data={
'election_id': data['id']
}
)
task.create_and_send()
return task
def tally_task(data):
if not data:
print("invalid json")
return False
requirements = [
{'name': u'election_id', 'isinstance': int},
{'name': u'callback_url', 'isinstance': str},
{'name': u'votes_url', 'isinstance': str},
{'name': u'votes_hash', 'isinstance': str},
]
for req in requirements:
if req['name'] not in data or not isinstance(data[req['name']],
req['isinstance']):
print(req['name'], data.get(req['name'], None), type(data[req['name']]))
print("invalid %s parameter" % req['name'])
return False
if data['election_id'] <= 0:
print("election id must be >= 1")
return False
if not data['votes_hash'].startswith("ni:///sha-256;"):
print("invalid votes_hash, must be sha256")
return False
election_id = data['election_id']
election = db.session.query(Election)\
.filter(Election.id == election_id).first()
if election is None:
print("unknown election with election_id = %s" % election_id)
return False
task = SimpleTask(
receiver_url=app.config.get('ROOT_URL', ''),
action="tally_election",
queue="launch_task",
data={
'election_id': data['election_id'],
'callback_url': data['callback_url'],
'votes_url': data['votes_url'],
'votes_hash': data['votes_hash'],
}
)
task.create_and_send()
return task