1
|
|
|
'''save full cycle data''' |
2
|
|
|
from helpers.queuehelper import QueueName, QueueEntries |
3
|
|
|
from domain.mining import Pool, Miner |
4
|
|
|
from fcmapp import Component |
5
|
|
|
|
6
|
|
|
COMPONENTSAVE = Component('fullcycle') |
7
|
|
|
|
8
|
|
|
def when_save(channel, method, properties, body): |
9
|
|
|
'''event handler when log event is raised''' |
10
|
|
|
try: |
11
|
|
|
print("[{0}] Received save message".format(COMPONENTSAVE.app.now())) |
12
|
|
|
msg = COMPONENTSAVE.app.messagedecode_configuration(body) |
13
|
|
|
entries = dosave(msg) |
14
|
|
|
COMPONENTSAVE.app.enqueue(entries) |
15
|
|
|
|
16
|
|
|
except Exception as ex: |
17
|
|
|
COMPONENTSAVE.app.logexception(ex) |
18
|
|
|
|
19
|
|
|
def dosave(msg): |
20
|
|
|
entries = QueueEntries() |
21
|
|
|
if msg.entity == 'miner': |
22
|
|
|
miner = saveminer(msg) |
23
|
|
|
entries.add(QueueName.Q_MONITORMINER, COMPONENTSAVE.app.messageencode(miner)) |
24
|
|
|
entries.add(QueueName.Q_PROVISION, COMPONENTSAVE.app.messageencode(miner)) |
25
|
|
|
|
26
|
|
|
if msg.entity == 'pool': |
27
|
|
|
savepool(msg) |
28
|
|
|
return entries |
29
|
|
|
|
30
|
|
|
def saveminer(msg): |
31
|
|
|
#add or update miner |
32
|
|
|
minerid = name = ipaddress = port = None |
33
|
|
|
for pair in msg.values: |
34
|
|
|
if 'minerid' in pair: |
35
|
|
|
minerid = pair['minerid'] |
36
|
|
|
if 'name' in pair: |
37
|
|
|
name = pair['name'] |
38
|
|
|
if 'ipaddress' in pair: |
39
|
|
|
ipaddress = pair['ipaddress'] |
40
|
|
|
if 'port' in pair: |
41
|
|
|
port = pair['port'] |
42
|
|
|
miner = Miner(name,'','',ipaddress,port,'','') |
43
|
|
|
COMPONENTSAVE.app.save_miner(miner) |
44
|
|
|
return miner |
45
|
|
|
|
46
|
|
|
def savepool(msg): |
47
|
|
|
#save the new named pool |
48
|
|
|
pool_type = name = url = user = priority = None |
49
|
|
|
for pair in msg.values: |
50
|
|
|
if 'pool_type' in pair: |
51
|
|
|
pool_type = pair['pool_type'] |
52
|
|
|
if 'name' in pair: |
53
|
|
|
name = pair['name'] |
54
|
|
|
if 'url' in pair: |
55
|
|
|
url = pair['url'] |
56
|
|
|
if 'user' in pair: |
57
|
|
|
user = pair['user'] |
58
|
|
|
if 'priority' in pair: |
59
|
|
|
priority = pair['priority'] |
60
|
|
|
pool = Pool(pool_type, name, url, user, priority) |
61
|
|
|
COMPONENTSAVE.app.save_pool(pool) |
62
|
|
|
return pool |
63
|
|
|
|
64
|
|
|
def main(): |
65
|
|
|
COMPONENTSAVE.listeningqueue = COMPONENTSAVE.app.subscribe(QueueName.Q_SAVE, when_save) |
66
|
|
|
COMPONENTSAVE.app.listen(COMPONENTSAVE.listeningqueue) |
67
|
|
|
|
68
|
|
|
if __name__ == "__main__": |
69
|
|
|
main() |
70
|
|
|
|