1
|
|
|
''' |
2
|
|
|
#listens for restart command |
3
|
|
|
''' |
4
|
|
|
import datetime |
5
|
|
|
import json |
6
|
|
|
from threading import Thread |
7
|
|
|
from queue import Queue |
8
|
|
|
from colorama import Fore |
9
|
|
|
from helpers import antminerhelper |
10
|
|
|
from helpers.queuehelper import QueueName |
11
|
|
|
from domain.mining import MinerCommand |
12
|
|
|
from domain.logging import MinerLog |
13
|
|
|
import backend.fcmutils as utils |
14
|
|
|
from backend.fcmapp import Component |
15
|
|
|
|
16
|
|
|
COMPONENTACTION = Component('action') |
17
|
|
|
|
18
|
|
|
def enthread(target, args): |
19
|
|
|
'''put a method on a queue to be run in background''' |
20
|
|
|
thread_queue = Queue() |
21
|
|
|
def wrapper(): |
22
|
|
|
thread_queue.put(target(*args)) |
23
|
|
|
thread = Thread(target=wrapper) |
24
|
|
|
thread.start() |
25
|
|
|
return thread_queue |
26
|
|
|
|
27
|
|
|
def when_restart(channel, method, properties, body): |
28
|
|
|
'''when restart command''' |
29
|
|
|
print("[{0}] Received restart command".format(str(COMPONENTACTION.app.now()))) |
30
|
|
|
#TODO: restart should happen on a background thread, so that queue connection does not time out |
31
|
|
|
try: |
32
|
|
|
minermsg = COMPONENTACTION.app.messagedecodeminercommand(body) |
33
|
|
|
qreset = enthread(target=dorestart, args=(minermsg.miner, minermsg.command, )) |
34
|
|
|
qreset.get() |
35
|
|
|
COMPONENTACTION.app.bus.acknowledge(COMPONENTACTION.listeningqueue, method.delivery_tag) |
36
|
|
|
except json.decoder.JSONDecodeError as jex: |
37
|
|
|
COMPONENTACTION.app.bus.reject(COMPONENTACTION.listeningqueue, method.delivery_tag) |
38
|
|
|
COMPONENTACTION.app.logexception(jex) |
39
|
|
|
#could be json error so log the message |
40
|
|
|
COMPONENTACTION.app.logdebug(COMPONENTACTION.app.exceptionmessage(jex)) |
41
|
|
|
COMPONENTACTION.app.logdebug(utils.safestring(body)) |
42
|
|
|
except BaseException as ex: |
43
|
|
|
COMPONENTACTION.app.bus.reject(COMPONENTACTION.listeningqueue, method.delivery_tag) |
44
|
|
|
COMPONENTACTION.app.logexception(ex) |
45
|
|
|
print(Fore.RED+'Could not run restart command: ' + COMPONENTACTION.app.exceptionmessage(ex)) |
46
|
|
|
|
47
|
|
|
def dorestart(miner, command: MinerCommand): |
48
|
|
|
if command.command: |
49
|
|
|
showmsg = Fore.YELLOW + "{0}({1}) {2} {3}".format(miner.name, '{}:{}'.format(miner.ipaddress, miner.port), command.command, command.parameter) |
50
|
|
|
COMPONENTACTION.app.sendlog(showmsg) |
51
|
|
|
#access = COMPONENTACTION.app.antminer.getaccesslevel(miner) |
52
|
|
|
#if access == MinerAccessLevel.Restricted: |
53
|
|
|
#setting miner to privileged is taking too much cpu and hanging the controller |
54
|
|
|
#miner.set_ftp_port(COMPONENTACTION.app.configuration('discover.sshport')) |
55
|
|
|
#access = COMPONENTACTION.app.antminer.setminertoprivileged(miner) |
56
|
|
|
#if access == MinerAccessLevel.Restricted: |
57
|
|
|
# raise Exception('Could not set miner {0} to priviledged'.format(miner.name)) |
58
|
|
|
if command.parameter and command.parameter == 'reboot': |
59
|
|
|
miner.set_ftp_port(COMPONENTACTION.app.configuration.get('discover.sshport')) |
60
|
|
|
antminerhelper.reboot(miner, COMPONENTACTION.app.sshlogin()) |
61
|
|
|
else: |
62
|
|
|
antminerhelper.restart(miner) |
63
|
|
|
log = MinerLog() |
64
|
|
|
log.createdate = datetime.datetime.utcnow() |
65
|
|
|
log.minerid = miner.key() |
66
|
|
|
log.minername = miner.name |
67
|
|
|
log.action = showmsg |
68
|
|
|
COMPONENTACTION.app.log_mineractivity(log) |
69
|
|
|
#antminerhelper.waitforonline(miner) |
70
|
|
|
#do something to show what current status of miner is |
71
|
|
|
#access = COMPONENTACTION.app.antminer.setminertoprivileged(miner) |
72
|
|
|
#print(access) |
73
|
|
|
#monitor so that status of miner will show offline immediately |
74
|
|
|
COMPONENTACTION.app.send(QueueName.Q_MONITORMINER, COMPONENTACTION.app.messageencode(miner)) |
75
|
|
|
|
76
|
|
|
def main(): |
77
|
|
|
COMPONENTACTION.listeningqueue = COMPONENTACTION.app.subscribe(QueueName.Q_RESTART, when_restart, no_acknowledge=False) |
78
|
|
|
COMPONENTACTION.app.listen(COMPONENTACTION.listeningqueue) |
79
|
|
|
|
80
|
|
|
if __name__ == "__main__": |
81
|
|
|
main() |
82
|
|
|
|