コード例 #1
0
ファイル: lifecycle.py プロジェクト: ctebbe/pymaker
    def __exit__(self, exc_type, exc_val, exc_tb):
        # Initialization phase
        self.logger.info(f"Keeper connected to {self.web3.providers[0]}")
        self.logger.info(f"Keeper operating as {self.web3.eth.defaultAccount}")
        self._check_account_unlocked()
        self._wait_for_init()

        # Initial delay
        if self.delay > 0:
            self.logger.info(f"Waiting for {self.delay} seconds of initial delay...")
            time.sleep(self.delay)

        # Startup phase
        if self.startup_function:
            self.logger.info("Executing keeper startup logic")
            self.startup_function()

        # Bind `on_block`, bind `every`
        # Enter the main loop
        self._start_watching_blocks()
        self._start_every_timers()
        self._main_loop()

        # Enter shutdown process
        self.logger.info("Shutting down the keeper")

        # Disable all filters
        if any_filter_thread_present():
            self.logger.info("Waiting for all threads to terminate...")
            stop_all_filter_threads()

        # If the `on_block` callback is still running, wait for it to terminate
        if self._on_block_callback is not None:
            self.logger.info("Waiting for outstanding callback to terminate...")
            self._on_block_callback.wait()

        # If any every (timer) callback is still running, wait for it to terminate
        if len(self.every_timers) > 0:
            self.logger.info("Waiting for outstanding timers to terminate...")
            for timer in self.every_timers:
                timer[1].wait()

        # Shutdown phase
        if self.shutdown_function:
            self.logger.info("Executing keeper shutdown logic...")
            self.shutdown_function()
            self.logger.info("Shutdown logic finished")
        self.logger.info("Keeper terminated")
        exit(10 if self.fatal_termination else 0)
コード例 #2
0
    def __exit__(self, exc_type, exc_val, exc_tb):
        # Initialization phase
        if self.web3:
            self.logger.info(f"Keeper connected to {self.web3.providers[0]}")
            if self.web3.eth.defaultAccount:
                self.logger.info(
                    f"Keeper operating as {self.web3.eth.defaultAccount}")
                self._check_account_unlocked()
            else:
                self.logger.info(
                    f"Keeper not operating as any particular account")
                # web3 calls do not work correctly if defaultAccount is empty
                self.web3.eth.defaultAccount = "0x0000000000000000000000000000000000000000"
        else:
            self.logger.info(f"Keeper initializing")

        # Wait for sync and peers
        if self.web3 and self.do_wait_for_sync:
            self._wait_for_init()

        # Initial delay
        if self.delay > 0:
            self.logger.info(
                f"Waiting for {self.delay} seconds of initial delay...")
            time.sleep(self.delay)

        # Initial checks
        if len(self.wait_for_functions) > 0:
            self.logger.info("Waiting for initial checks to pass...")

            for index, (wait_for_function,
                        max_wait) in enumerate(self.wait_for_functions,
                                               start=1):
                start_time = time.time()
                while True:
                    try:
                        result = wait_for_function()
                    except Exception as e:
                        self.logger.exception(
                            f"Initial check #{index} failed with an exception: '{e}'"
                        )
                        result = False

                    if result:
                        break

                    if time.time() - start_time >= max_wait:
                        self.logger.warning(
                            f"Initial check #{index} took more than {max_wait} seconds to pass, skipping"
                        )
                        break

                    time.sleep(0.1)

        # Startup phase
        if self.startup_function:
            self.logger.info("Executing keeper startup logic")
            self.startup_function()

        # Bind `on_block`, bind `every`
        # Enter the main loop
        self._start_watching_blocks()
        self._start_every_timers()
        self._main_loop()

        # Enter shutdown process
        self.logger.info("Shutting down the keeper")

        # Disable all filters
        if any_filter_thread_present():
            self.logger.info("Waiting for all threads to terminate...")
            stop_all_filter_threads()

        # If the `on_block` callback is still running, wait for it to terminate
        if self._on_block_callback is not None:
            self.logger.info(
                "Waiting for outstanding callback to terminate...")
            self._on_block_callback.wait()

        # If any every (timer) callback is still running, wait for it to terminate
        if len(self.every_timers) > 0:
            self.logger.info("Waiting for outstanding timers to terminate...")
            for timer in self.every_timers:
                timer[1].wait()

        # If any condition callback is still running, wait for it to terminate
        if len(self.condition_timers) > 0:
            self.logger.info(
                "Waiting for outstanding conditions to terminate...")
            for timer in self.condition_timers:
                timer[2].wait()

        # Shutdown phase
        if self.shutdown_function:
            self.logger.info("Executing keeper shutdown logic...")
            self.shutdown_function()
            self.logger.info("Shutdown logic finished")
        self.logger.info("Keeper terminated")
        exit(10 if self.fatal_termination else 0)