backend.when_switch   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 52
dl 0
loc 68
rs 10
c 0
b 0
f 0
wmc 8

4 Functions

Rating   Name   Duplication   Size   Complexity  
A enthread() 0 8 1
A main() 0 3 1
A doswitch() 0 21 4
A when_switch() 0 13 2
1
'''
2
listens for pool switch command
3
'''
4
import datetime
5
from threading import Thread
6
from queue import Queue
7
from colorama import Fore
8
#from pika.exceptions import ChannelClosed
9
from helpers import antminerhelper
10
from helpers.queuehelper import QueueName
11
from domain.mining import MinerCommand, MinerAccessLevel
12
from domain.logging import MinerLog
13
from backend.fcmapp import Component
14
15
COMPONENTACTION = Component('action')
16
17
def enthread(target, args):
18
    '''put a method on a queue to be run in background'''
19
    thread_queue = Queue()
20
    def wrapper():
21
        thread_queue.put(target(*args))
22
    thread = Thread(target=wrapper)
23
    thread.start()
24
    return thread_queue
25
26
def when_switch(channel, method, properties, body):
27
    '''Handler for pool switching'''
28
    try:
29
        print("[{0}] Received switch command".format(COMPONENTACTION.app.now()))
30
        minermsg = COMPONENTACTION.app.messagedecodeminercommand(body)
31
32
        qswitch = enthread(target=doswitch, args=(minermsg.miner, minermsg.command, ))
33
        qswitch.get()
34
35
        COMPONENTACTION.app.bus.acknowledge(COMPONENTACTION.listeningqueue, method.delivery_tag)
36
    except BaseException as ex:
37
        COMPONENTACTION.app.bus.reject(COMPONENTACTION.listeningqueue, method.delivery_tag)
38
        print(Fore.RED + 'Could not run switch command: ' + COMPONENTACTION.app.exceptionmessage(ex))
39
40
def doswitch(miner, command: MinerCommand):
41
    '''switch miner pool'''
42
    if command.command:
43
        txtalert = "{0} {1}".format(miner.name, command.command)
44
        print(Fore.YELLOW + txtalert)
45
        #check if privileged mode, raise alert if not in privileged mode!
46
        access = COMPONENTACTION.app.antminer.getaccesslevel(miner)
47
        if access == MinerAccessLevel.Restricted:
48
            miner.set_ftp_port(COMPONENTACTION.app.configuration.get('discover.sshport'))
49
            access = COMPONENTACTION.app.antminer.setminertoprivileged(miner)
50
            if access == MinerAccessLevel.Restricted:
51
                raise Exception('Could not set miner {0} to priviledged'.format(miner.name))
52
        antminerhelper.switch(miner, command.parameter)
53
        COMPONENTACTION.app.alert(txtalert)
54
        log = MinerLog()
55
        log.createdate = datetime.datetime.utcnow()
56
        log.minerid = miner.key()
57
        log.minername = miner.name
58
        log.action = txtalert
59
        COMPONENTACTION.app.log_mineractivity(log)
60
        COMPONENTACTION.app.send(QueueName.Q_MONITORMINER, COMPONENTACTION.app.messageencode(miner))
61
62
def main():
63
    COMPONENTACTION.listeningqueue = COMPONENTACTION.app.subscribe(QueueName.Q_SWITCH, when_switch, no_acknowledge=False)
64
    COMPONENTACTION.app.listen(COMPONENTACTION.listeningqueue)
65
66
if __name__ == "__main__":
67
    main()
68