Ejemplo n.º 1
0
    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
Ejemplo n.º 2
0
def mock_client(event_loop):
    asyncio.set_event_loop(event_loop)
    client = MockClient()
    client.start()
    yield client
    client.stop()
    asyncio_stop()
    asyncio_join()
Ejemplo n.º 3
0
 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()
Ejemplo n.º 4
0
    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'}

        # noinspection PyBroadException
        try:
            asyncio.get_event_loop().run_until_complete(self.run_task(chains=chains))
        except asyncio.CancelledError:
            logger.info('Clean exit requested')
            utils.asyncio_join()
        except Exception:
            logger.exception('Unhandled exception at top level')
            utils.asyncio_stop()
            utils.asyncio_join()
Ejemplo n.º 5
0
 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
Ejemplo n.º 6
0
    def run(self):
        while not self.finished:
            loop = asyncio.SelectorEventLoop()

            # Default event loop does not support pipes on Windows
            if sys.platform == 'win32':
                loop = asyncio.ProactorEventLoop()

            asyncio.set_event_loop(loop)

            # K8 uses SIGTERM on linux and SIGINT and windows
            exit_signal = signal.SIGINT if platform.system() == "Windows" else signal.SIGTERM
            try:
                loop.add_signal_handler(exit_signal, self.handle_signal)
            except NotImplementedError:
                # Disable graceful exit, but run anyway
                logger.exception(f'{platform.system()} does not support graceful shutdown')
            try:
                asyncio.get_event_loop().run_until_complete(self.setup())
                gather_task = asyncio.gather(*[self.run_task(i) for i in range(self.task_count)])
                asyncio.get_event_loop().run_until_complete(gather_task)
            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