def run(self, chains=None): """Run the main event loop Args: chains (set(str)): Set of chains to operate on. Defaults to {'home', 'side'} """ if chains is None: chains = {'home', 'side'} configure_event_loop() while True: try: asyncio.get_event_loop().run_until_complete( self.run_task(chains=chains)) except asyncio.CancelledError: logger.info('Clean exit requested, exiting') asyncio_join() exit(0) except Exception: logger.exception('Unhandled exception at top level') asyncio_stop() asyncio_join() self.tries += 1 wait = min(MAX_WAIT, self.tries * self.tries) logger.critical( 'Detected unhandled exception, sleeping for %s seconds then resetting task', wait) time.sleep(wait) continue
def run_oneshot(self): """ Runs run_task once """ configure_event_loop() asyncio.get_event_loop().run_until_complete( self.client.run_task(chains=self.chains, listen_for_events=False))
def __init__(self, polyswarmd_addr, keyfile, password, api_key=None, tx_error_fatal=False): self.polyswarmd_uri = polyswarmd_addr logger.debug('self.polyswarmd_uri: %s', self.polyswarmd_uri) self.api_key = api_key self.tx_error_fatal = tx_error_fatal self.params = {} with open(keyfile, 'r') as f: self.priv_key = w3.eth.account.decrypt(f.read(), password) self.account = w3.eth.account.from_key(self.priv_key).address logger.info('Using account: %s', self.account) self.rate_limit = None # Do not init nonce manager here. Need to wait until we can guarantee that our event loop is set. self.nonce_managers = {} self.__schedules = {} self.tries = 0 self.bounties = None self.staking = None self.offers = None self.relay = None self.balances = None # Setup a liveliness instance self.liveness_recorder = LocalLivenessRecorder() # Events from client self.on_run = events.OnRunCallback() self.on_stop = events.OnStopCallback() # Events from polyswarmd self.on_new_block = events.OnNewBlockCallback() self.on_new_bounty = events.OnNewBountyCallback() self.on_new_assertion = events.OnNewAssertionCallback() self.on_reveal_assertion = events.OnRevealAssertionCallback() self.on_new_vote = events.OnNewVoteCallback() self.on_quorum_reached = events.OnQuorumReachedCallback() self.on_settled_bounty = events.OnSettledBountyCallback() self.on_initialized_channel = events.OnInitializedChannelCallback() self.on_deprecated = events.OnDeprecatedCallback() # Events scheduled on block deadlines self.on_reveal_assertion_due = events.OnRevealAssertionDueCallback() self.on_vote_on_bounty_due = events.OnVoteOnBountyDueCallback() self.on_settle_bounty_due = events.OnSettleBountyDueCallback() self.on_withdraw_stake_due = events.OnWithdrawStakeDueCallback() utils.configure_event_loop()
def run(self): configure_event_loop() loop = asyncio.get_event_loop() try: self.start(loop) # This stops any leftover tasks after out main task finishes asyncio_stop() except asyncio.CancelledError: logger.info('Clean exit requested, exiting') asyncio_join() except Exception: logger.exception('Unhandled exception at top level') asyncio_stop() asyncio_join()
def run(self): while not self.finished: configure_event_loop() loop = asyncio.get_event_loop() try: self.start(loop) # This stops any leftover tasks after out main task finishes asyncio_stop() except asyncio.CancelledError: logger.info('Clean exit requested, exiting') asyncio_join() exit(0) except Exception: logger.exception('Unhandled exception at top level') asyncio_stop() asyncio_join() self.tries += 1 wait = min(MAX_WAIT, self.tries * self.tries) logger.critical( f'Detected unhandled exception, sleeping for {wait} seconds then resetting task' ) time.sleep(wait) continue