Start listening when mining service is ready
[stratum-mining.git] / mining / __init__.py
index 4709e14..73e6d15 100644 (file)
@@ -1,16 +1,22 @@
 from service import MiningService
 from subscription import MiningSubscription
+from twisted.internet import defer
 
-def setup():
+@defer.inlineCallbacks
+def setup(on_startup):
     '''Setup mining service internal environment.
     You should not need to change this. If you
     want to use another Worker manager or Share manager,
     you should set proper reference to Interfaces class
     *before* you call setup() in the launcher script.'''
-        
+
     from stratum import settings
     from interfaces import Interfaces
     
+    # Let's wait until share manager and worker manager boot up
+    (yield Interfaces.share_manager.on_load)
+    (yield Interfaces.worker_manager.on_load)
+    
     from lib.block_updater import BlockUpdater
     from lib.template_registry import TemplateRegistry
     from lib.bitcoin_rpc import BitcoinRPC
@@ -22,8 +28,11 @@ def setup():
                              settings.BITCOIN_TRUSTED_USER,
                              settings.BITCOIN_TRUSTED_PASSWORD)
     
+    coinbaser = SimpleCoinbaser(bitcoin_rpc, settings.CENTRAL_WALLET)
+    (yield coinbaser.on_load)
+    
     registry = TemplateRegistry(BlockTemplate,
-                                SimpleCoinbaser(bitcoin_rpc, settings.CENTRAL_WALLET),
+                                coinbaser,
                                 bitcoin_rpc,
                                 settings.INSTANCE_ID,
                                 MiningSubscription.on_block)
@@ -35,4 +44,9 @@ def setup():
     # Set up polling mechanism for detecting new block on the network
     # This is just failsafe solution when -blocknotify
     # mechanism is not working properly    
-    BlockUpdater(registry, bitcoin_rpc)
\ No newline at end of file
+    BlockUpdater(registry, bitcoin_rpc)
+    
+    import stratum.logger
+    log = stratum.logger.get_logger('mining')
+    log.info("MINING SERVICE IS READY")
+    on_startup.callback(True)