backend.when_online.main()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
'''what to do when miner becomes online
2
enable, provision and start monitoring
3
'''
4
from helpers.queuehelper import QueueName, QueueEntries
5
from backend.fcmapp import Component
6
7
ONLINE = Component('online')
8
9
def when_online(channel, method, properties, body):
10
    '''whan a miner is found to be online after being offline'''
11
    print("[{0}] Received miner online message".format(ONLINE.app.now()))
12
    try:
13
        miner = ONLINE.app.messagedecodeminer(body)
14
        entries = doonline(miner)
15
        ONLINE.app.enqueue(entries)
16
    except Exception as ex:
17
        ONLINE.app.logexception(ex)
18
19
def doonline(miner):
20
    '''then provision the miner'''
21
    entries = QueueEntries()
22
    savedminer = ONLINE.app.getminer(miner)
23
    if savedminer is None:
24
        savedminer = miner
25
    #update status
26
    savedminer.online_now()
27
    ONLINE.app.putminer(savedminer)
28
    ONLINE.app.updateknownminer(savedminer)
29
    #just provision the miner and start to monitor
30
    entries.add(QueueName.Q_PROVISION, ONLINE.app.messageencode(savedminer))
31
    #tell them something good happened
32
    msg = ONLINE.app.stamp('miner {0} is back online'.format(savedminer.name))
33
    entries.addalert(msg)
34
    print(msg)
35
    return entries
36
37
def main():
38
    ONLINE.listeningqueue = ONLINE.app.subscribe(QueueName.Q_ONLINE, when_online)
39
    ONLINE.listen()
40
41
if __name__ == "__main__":
42
    main()
43