backend.when_provision_dispatch.process_miner()   B
last analyzed

Complexity

Conditions 7

Size

Total Lines 16
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 15
nop 3
dl 0
loc 16
rs 8
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
        process_miner(miner, entries, miner_type)
32
    return entries
33
34
def process_miner(miner, entries, miner_type):
35
    try:
36
        minerstats, minerinfo, apicall, minerpool = stats(miner)
37
        if miner_type is not None and miner_type != '' and minerinfo.miner_type != miner_type:
38
            return
39
        mineraccess = get_mineraccess(miner)
40
        print(Fore.GREEN + "{0} {1} {2}".format(miner.name, minerinfo.miner_type, mineraccess))
41
        if mineraccess == MinerAccessLevel.Restricted:
42
            print("    skipping restricted access")
43
        else:
44
            entries.add(QueueName.Q_PROVISION, PROVISION_DISPATCH.app.messageencode(miner))
45
    except MinerMonitorException as minerex:
46
        print(minerex)
47
    except BaseException as ex:
48
        print('while provisioning {0} ({1})'.format(miner.ipaddress, miner.key()))
49
        print(PROVISION_DISPATCH.app.exceptionmessage(ex))
50
51
def get_mineraccess(miner):
52
    mineraccess = PROVISION_DISPATCH.app.antminer.getaccesslevel(miner)
53
    if mineraccess == MinerAccessLevel.Restricted:
54
        print(Fore.RED+"    Log: setting {0} to privileged...".format(miner.name))
55
        PROVISION_DISPATCH.app.antminer.set_privileged(miner)
56
        PROVISION_DISPATCH.app.antminer.waitforonline(miner)
57
        mineraccess = PROVISION_DISPATCH.app.antminer.getaccesslevel(miner)
58
    return mineraccess
59
60
def main():
61
    if PROVISION_DISPATCH.app.isrunnow or PROVISION_DISPATCH.app.isdebug:
62
        entries = doprovisiondispatch()
63
        PROVISION_DISPATCH.app.enqueue(entries)
64
        PROVISION_DISPATCH.app.shutdown()
65
    else:
66
        PROVISION_DISPATCH.listeningqueue = PROVISION_DISPATCH.app.subscribe(QueueName.Q_POOLCONFIGURATIONCHANGED, when_provisiondispatch)
67
        PROVISION_DISPATCH.listen()
68
69
if __name__ == "__main__":
70
    main()
71