| Total Complexity | 8 |
| Total Lines | 48 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | '''save full cycle data''' |
||
| 2 | from helpers.queuehelper import QueueName, QueueEntries |
||
| 3 | from domain.mining import Pool, Miner |
||
| 4 | from backend.fcmapp import Component |
||
| 5 | |||
| 6 | COMPONENTSAVE = Component('fullcycle') |
||
| 7 | |||
| 8 | def when_save(channel, method, properties, body): |
||
| 9 | '''event handler when log event is raised''' |
||
| 10 | try: |
||
| 11 | print("[{0}] Received save message".format(COMPONENTSAVE.app.now())) |
||
| 12 | msg = COMPONENTSAVE.app.messagedecode_configuration(body) |
||
| 13 | entries = dosave(msg) |
||
| 14 | COMPONENTSAVE.app.enqueue(entries) |
||
| 15 | |||
| 16 | except Exception as ex: |
||
| 17 | COMPONENTSAVE.app.logexception(ex) |
||
| 18 | |||
| 19 | def dosave(msg): |
||
| 20 | entries = QueueEntries() |
||
| 21 | if msg.entity == 'miner': |
||
| 22 | miner = saveminer(msg) |
||
| 23 | entries.add(QueueName.Q_MONITORMINER, COMPONENTSAVE.app.messageencode(miner)) |
||
| 24 | entries.add(QueueName.Q_PROVISION, COMPONENTSAVE.app.messageencode(miner)) |
||
| 25 | |||
| 26 | if msg.entity == 'pool': |
||
| 27 | savepool(msg) |
||
| 28 | return entries |
||
| 29 | |||
| 30 | def saveminer(msg): |
||
| 31 | #add or update miner |
||
| 32 | miner = Miner.create(msg.values) |
||
| 33 | COMPONENTSAVE.app.save_miner(miner) |
||
| 34 | return miner |
||
| 35 | |||
| 36 | def savepool(msg): |
||
| 37 | #save the new named pool |
||
| 38 | pool = Pool.create(msg.values) |
||
| 39 | COMPONENTSAVE.app.pools.save_pool(pool) |
||
| 40 | return pool |
||
| 41 | |||
| 42 | def main(): |
||
| 43 | COMPONENTSAVE.listeningqueue = COMPONENTSAVE.app.subscribe(QueueName.Q_SAVE, when_save) |
||
| 44 | COMPONENTSAVE.app.listen(COMPONENTSAVE.listeningqueue) |
||
| 45 | |||
| 46 | if __name__ == "__main__": |
||
| 47 | main() |
||
| 48 |