Passed
Push — master ( 9b88b2...933317 )
by Dave
57s
created

backend.when_provision_dispatch.main()   A

Complexity

Conditions 3

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
'''
2
#will listen to poolconfigurationchanged event and provision all known miners
3
#of a particular type
4
'''
5
from colorama import Fore
6
from helpers.antminerhelper import MinerMonitorException, stats
7
from helpers.queuehelper import QueueName, QueueEntries
8
from domain.mining import MinerAccessLevel
9
from backend.fcmapp import Component
10
11
PROVISION_DISPATCH = Component('provision')
12
13
def when_provisiondispatch(channel, method, properties, body):
14
    '''when provision event received'''
15
    print("[{0}] Received provision command".format(PROVISION_DISPATCH.app.now()))
16
    miner_type = body.decode("utf-8")
17
    try:
18
        entries = doprovisiondispatch(miner_type)
19
        PROVISION_DISPATCH.app.enqueue(entries)
20
    except Exception as ex:
21
        PROVISION_DISPATCH.app.logexception(ex)
22
23
def doprovisiondispatch(miner_type=None):
24
    '''put all miners in provision worker queue'''
25
    entries = QueueEntries()
26
    miners = PROVISION_DISPATCH.app.allminers()
27
    print("{0} miners configured".format(len(miners)))
28
    for miner in miners:
29
        if miner.is_disabled():
30
            continue
31
        try:
32
            minerstats, minerinfo, apicall, minerpool = stats(miner)
33
            if miner_type is not None and miner_type != '' and minerinfo.miner_type != miner_type:
34
                continue
35
            mineraccess = get_mineraccess(miner)
36
            print(Fore.GREEN + "{0} {1} {2}".format(miner.name, minerinfo.miner_type, mineraccess))
37
            if mineraccess == MinerAccessLevel.Restricted:
38
                print("    skipping restricted access")
39
            else:
40
                entries.add(QueueName.Q_PROVISION, PROVISION_DISPATCH.app.messageencode(miner))
41
        except MinerMonitorException as minerex:
42
            print(minerex)
43
        except BaseException as ex:
44
            print('while provisioning {0} ({1})'.format(miner.ipaddress, miner.key()))
45
            print(PROVISION_DISPATCH.app.exceptionmessage(ex))
46
    return entries
47
48
def get_mineraccess(miner):
49
    mineraccess = PROVISION_DISPATCH.app.antminer.getaccesslevel(miner)
50
    if mineraccess == MinerAccessLevel.Restricted:
51
        print(Fore.RED+"    Log: setting {0} to privileged...".format(miner.name))
52
        PROVISION_DISPATCH.app.antminer.set_privileged(miner)
53
        PROVISION_DISPATCH.app.antminer.waitforonline(miner)
54
        mineraccess = PROVISION_DISPATCH.app.antminer.getaccesslevel(miner)
55
    return mineraccess
56
57
def main():
58
    if PROVISION_DISPATCH.app.isrunnow or PROVISION_DISPATCH.app.isdebug:
59
        entries = doprovisiondispatch()
60
        PROVISION_DISPATCH.app.enqueue(entries)
61
        PROVISION_DISPATCH.app.shutdown()
62
    else:
63
        PROVISION_DISPATCH.listeningqueue = PROVISION_DISPATCH.app.subscribe(QueueName.Q_POOLCONFIGURATIONCHANGED, when_provisiondispatch)
64
        PROVISION_DISPATCH.listen()
65
66
if __name__ == "__main__":
67
    main()
68