-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatamanager.py
59 lines (48 loc) · 1.66 KB
/
datamanager.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
import logging
import threading
import time
import apiclient
import localstore
logging.basicConfig(filename='vwyf.log',level=logging.INFO)
# interface for flipdot daemon
def get_next_question():
q = localstore.get_next_question()
return q.id, q.question, q.option_a, q.option_b
# return current ratio (#ofA) / (#ofAllVotes)
def log_vote(question_id, isVoteA):
answer = 'A' if isVoteA else 'B'
return localstore.add_answer(question_id, answer)
def log_question(question_id):
localstore.log_question(question_id)
# blocking network call
def _sync_questions_with_server():
logging.info('fetching questions')
fetched, questions = apiclient.get_questions()
logging.info('fetched questions' + str(fetched))
if (fetched):
logging.info('updating questions')
localstore.update_local_questions(questions);
return fetched
# blocking network call
def _save_answers_to_server():
return localstore.save_answers_to_server(apiclient.post_answers)
def _data_daemon_worker():
num_of_loops = 0
while True:
num_of_loops += 1
try:
logging.info("running data daemon loop: " + str(num_of_loops))
_sync_questions_with_server()
_save_answers_to_server()
except:
logging.error("data daemon loop failed at: " + str(num_of_loops))
finally:
time.sleep(60)
# Note that we set 'check_same_thread' to false for sqlalchemy To avoid potential
# concurrency issue with SQLite:
# 1) only the main thread can write to answers and question_logs table
# 2) only the data daemon thread can write to questions table
def start_data_daemon():
data_daemon = threading.Thread(target=_data_daemon_worker)
data_daemon.daemon = True
data_daemon.start()