예제 #1
0
    def test_add_initial_state(self):
        fsmBuilder = TransitionsFsmBuilder()
        fsmBuilder.add_initial_state("start")
        fsmBuilder.add_state(
            "middle",
            on_leave=lambda e: print("left middle state: message=", e.args))
        fsmBuilder.add_state("running", on_enter=lambda e: print("Running", e))
        fsmBuilder.add_state("not_running",
                             on_enter=lambda e: print("Not running", e))
        fsmBuilder.add_final_state(
            "done",
            on_enter=lambda e: print("entered final state",
                                     e.kwargs.get('msg', 'None')))
        fsmBuilder.add_transition("move",
                                  "start",
                                  "middle",
                                  on_before=self.before_moving,
                                  on_after=self.after_moving)
        fsmBuilder.add_conditional_transition("run", "middle", self.is_tired,
                                              "not_running", "running")
        fsmBuilder.add_transition("stop", ["not_running", "running"],
                                  "done",
                                  on_before=self.before_stopping,
                                  on_after=self.after_stopping)

        fsm = fsmBuilder.build()

        fsm.trigger_event("move")
        self.assertEqual(fsm.current, "middle")

        fsm.trigger_event("run", 'Run Forest')
        self.assertEqual(fsm.current, "not_running")

        fsm.trigger_event("stop", msg='Stop There')
        self.assertTrue(fsm.is_complete)
예제 #2
0
    def test_exceptions(self):
        fsmBuilder = TransitionsFsmBuilder()
        fsmBuilder.add_transition_complete_callback(self.print_any_error)
        fsmBuilder.add_initial_state("start")
        fsmBuilder.add_state(
            "middle",
            on_leave=lambda e: print("left middle state: message=", e.args))
        fsmBuilder.add_state("running", on_enter=lambda e: print("Running", e))
        fsmBuilder.add_state("not_running",
                             on_enter=lambda e: print("Not running", e))
        fsmBuilder.add_final_state(
            "done",
            on_enter=lambda e: print("entered final state",
                                     e.kwargs.get('msg', 'None'), 'error:', e.
                                     error))
        fsmBuilder.add_global_transition(
            "fail", "done", on_before=lambda e: print("Processing failed", e))
        fsmBuilder.add_transition("move",
                                  "start",
                                  "middle",
                                  on_before=self.before_moving,
                                  on_after=self.after_moving)
        fsmBuilder.add_conditional_transition("run", "middle", self.is_tired,
                                              "not_running", "running")
        fsmBuilder.add_transition("stop", ["not_running", "running"],
                                  "done",
                                  on_before=self.raiseError,
                                  on_after=self.after_stopping)
        fsm = fsmBuilder.build()

        try:
            fsm.trigger_event("move")
            self.assertEqual(fsm.current(), "middle")

            fsm.trigger_event("run", 'Run Forest')
            self.assertEqual(fsm.current(), "not_running")

            fsm.trigger_event("stop", msg='Stop There')
            self.assertTrue(fsm.is_complete)
        except Exception:
            fsm.trigger_event("fail")
            self.assertTrue(fsm.is_complete)
예제 #3
0
    def get_fsm_builder(self):
        fsm_builder = TransitionsFsmBuilder()
        fsm_builder.add_initial_state(
            ConfigState.INITIAL,
            on_leave=lambda e: logger.debug(
                "Loading baking configuration file ..."),
        )
        fsm_builder.add_state(ConfigState.READ,
                              on_enter=self.do_read_configuration_file)
        fsm_builder.add_state(ConfigState.BUILT, on_enter=self.do_build_parser)
        fsm_builder.add_state(ConfigState.PARSED, on_enter=self.do_parse_cfg)
        fsm_builder.add_state(ConfigState.VALIDATED,
                              on_enter=self.do_validate_cfg)
        fsm_builder.add_state(ConfigState.PROCESSED,
                              on_enter=self.do_process_cfg)
        fsm_builder.add_final_state(
            ConfigState.COMPLETED,
            on_enter=lambda e: self.__callback(self.get_conf()))

        fsm_builder.add_transition(ConfigEvent.READ, ConfigState.INITIAL,
                                   ConfigState.READ)
        fsm_builder.add_transition(ConfigEvent.BUILD, ConfigState.READ,
                                   ConfigState.BUILT)
        fsm_builder.add_transition(ConfigEvent.PARSE, ConfigState.BUILT,
                                   ConfigState.PARSED)
        fsm_builder.add_transition(ConfigEvent.VALIDATE, ConfigState.PARSED,
                                   ConfigState.VALIDATED)
        fsm_builder.add_transition(ConfigEvent.PROCESS, ConfigState.VALIDATED,
                                   ConfigState.PROCESSED)
        fsm_builder.add_transition(ConfigEvent.COMPLETE, ConfigState.PROCESSED,
                                   ConfigState.COMPLETED)
        return fsm_builder
    def __init__(self, args, nw_cfg, node_client, callback):
        self.__args = args
        self.__nw_cfg = nw_cfg
        self.__node_client = node_client

        self.__config_text = None
        self.__parser = None

        fsm_builder = TransitionsFsmBuilder()
        fsm_builder.add_initial_state(
            ConfigState.INITIAL,
            on_leave=lambda e: logger.debug(
                "Loading baking configuration file ..."))
        fsm_builder.add_state(ConfigState.READ, on_enter=self.do_read_cfg_file)
        fsm_builder.add_state(ConfigState.BUILT, on_enter=self.do_build_parser)
        fsm_builder.add_state(ConfigState.PARSED, on_enter=self.do_parse_cfg)
        fsm_builder.add_state(ConfigState.VALIDATED,
                              on_enter=self.do_validate_cfg)
        fsm_builder.add_state(ConfigState.PROCESSED,
                              on_enter=self.do_process_cfg)
        fsm_builder.add_final_state(
            ConfigState.COMPLETED,
            on_enter=lambda e: callback(self.get_conf()))

        fsm_builder.add_transition(ConfigEvent.READ, ConfigState.INITIAL,
                                   ConfigState.READ)
        fsm_builder.add_transition(ConfigEvent.BUILD, ConfigState.READ,
                                   ConfigState.BUILT)
        fsm_builder.add_transition(ConfigEvent.PARSE, ConfigState.BUILT,
                                   ConfigState.PARSED)
        fsm_builder.add_transition(ConfigEvent.VALIDATE, ConfigState.PARSED,
                                   ConfigState.VALIDATED)
        fsm_builder.add_transition(ConfigEvent.PROCESS, ConfigState.VALIDATED,
                                   ConfigState.PROCESSED)
        fsm_builder.add_transition(ConfigEvent.COMPLETE, ConfigState.PROCESSED,
                                   ConfigState.COMPLETED)

        self.fsm = fsm_builder.build()
예제 #5
0
    def get_fsm_builder(self):
        fsm_builder = TransitionsFsmBuilder()
        fsm_builder.add_initial_state(
            TrdState.INITIAL, on_leave=lambda e: logger.debug("TRD is starting...")
        )
        fsm_builder.add_state(TrdState.CMD_ARGS_PARSED, on_enter=self.do_parse_args)
        fsm_builder.add_state(
            TrdState.CMD_ARGS_GIVEN, on_enter=self.print_argument_configuration
        )
        fsm_builder.add_state(TrdState.BANNER_PRINTED, on_enter=self.do_print_banner)
        fsm_builder.add_state(
            TrdState.LOGGERS_INITIATED, on_enter=self.do_initiate_loggers
        )
        fsm_builder.add_state(
            TrdState.NODE_CLIENT_BUILT, on_enter=self.do_build_node_client
        )
        fsm_builder.add_state(
            TrdState.NW_CONFIG_BUILT, on_enter=self.do_build_nw_config
        )
        fsm_builder.add_state(TrdState.CONFIG_LOADED, on_enter=self.do_load_config)
        fsm_builder.add_state(TrdState.DIRS_SET_UP, on_enter=self.do_set_up_dirs)
        fsm_builder.add_state(
            TrdState.SIGNALS_REGISTERED, on_enter=self.do_register_signals
        )
        fsm_builder.add_state(TrdState.LOCKED, on_enter=self.do_lock)
        fsm_builder.add_state(
            TrdState.NOT_LOCKED, on_enter=lambda e: logger.debug("No locking needed!")
        )
        fsm_builder.add_state(TrdState.FEES_INIT, on_enter=self.do_init_service_fees)
        fsm_builder.add_state(TrdState.PLUGINS_LOADED, on_enter=self.do_load_plugins)
        fsm_builder.add_state(
            TrdState.PRODUCERS_READY, on_enter=self.do_launch_producers
        )
        fsm_builder.add_state(
            TrdState.CONSUMERS_READY, on_enter=self.do_launch_consumers
        )
        fsm_builder.add_state(
            TrdState.NO_CONSUMERS_READY,
            on_enter=lambda e: logger.debug("No consumers needed!"),
        )
        fsm_builder.add_state(TrdState.READY, on_enter=self.print_ready)
        fsm_builder.add_final_state(TrdState.SHUTTING, on_enter=self.do_shut_down)

        fsm_builder.add_conditional_transition(
            TrdEvent.LAUNCH,
            TrdState.INITIAL,
            self.is_args_not_set,
            TrdState.CMD_ARGS_PARSED,
            TrdState.CMD_ARGS_GIVEN,
        )
        fsm_builder.add_transition(
            TrdEvent.PRINT_BANNER,
            [TrdState.CMD_ARGS_PARSED, TrdState.CMD_ARGS_GIVEN],
            TrdState.BANNER_PRINTED,
        )
        fsm_builder.add_transition(
            TrdEvent.INITIATE_LOGGERS,
            TrdState.BANNER_PRINTED,
            TrdState.LOGGERS_INITIATED,
        )
        fsm_builder.add_transition(
            TrdEvent.BUILD_NODE_CLIENT,
            TrdState.LOGGERS_INITIATED,
            TrdState.NODE_CLIENT_BUILT,
        )
        fsm_builder.add_transition(
            TrdEvent.BUILD_NW_CONFIG,
            TrdState.NODE_CLIENT_BUILT,
            TrdState.NW_CONFIG_BUILT,
        )
        fsm_builder.add_transition(
            TrdEvent.LOAD_CONFIG, TrdState.NW_CONFIG_BUILT, TrdState.CONFIG_LOADED
        )
        fsm_builder.add_transition(
            TrdEvent.SET_UP_DIRS, TrdState.CONFIG_LOADED, TrdState.DIRS_SET_UP
        )
        fsm_builder.add_transition(
            TrdEvent.REGISTER_SIGNALS, TrdState.DIRS_SET_UP, TrdState.SIGNALS_REGISTERED
        )

        fsm_builder.add_conditional_transition(
            TrdEvent.LOCK,
            TrdState.SIGNALS_REGISTERED,
            self.is_dry_run,
            TrdState.NOT_LOCKED,
            TrdState.LOCKED,
        )
        fsm_builder.add_transition(
            TrdEvent.INIT_FEES,
            [TrdState.LOCKED, TrdState.NOT_LOCKED],
            TrdState.FEES_INIT,
        )
        fsm_builder.add_transition(
            TrdEvent.LOAD_PLUGINS, TrdState.FEES_INIT, TrdState.PLUGINS_LOADED
        )

        fsm_builder.add_transition(
            TrdEvent.LAUNCH_PRODUCERS, TrdState.PLUGINS_LOADED, TrdState.PRODUCERS_READY
        )
        fsm_builder.add_conditional_transition(
            TrdEvent.LAUNCH_CONSUMERS,
            TrdState.PRODUCERS_READY,
            self.is_dry_run_no_consumers,
            TrdState.NO_CONSUMERS_READY,
            TrdState.CONSUMERS_READY,
        )
        fsm_builder.add_transition(
            TrdEvent.GO_READY,
            [TrdState.CONSUMERS_READY, TrdState.NO_CONSUMERS_READY],
            TrdState.READY,
        )
        fsm_builder.add_transition(
            TrdEvent.SHUT_DOWN_ON_DEMAND, TrdState.READY, TrdState.SHUTTING
        )
        fsm_builder.add_global_transition(
            TrdEvent.SHUT_DOWN_ON_ERROR, TrdState.SHUTTING
        )

        return fsm_builder
    def __init__(self, args):
        self.__lock_taken = False
        self.__args = args
        self.__node_client = None
        self.__nw_config = None
        self.__cfg = None
        self.__baking_dirs = None
        self.__srvc_fee_calc = None
        self.__plugins_manager = None
        self.__payments_queue = queue.Queue(BUF_SIZE)

        fsm_builder = TransitionsFsmBuilder()
        fsm_builder.add_initial_state(
            TrdState.INITIAL,
            on_leave=lambda e: logger.debug("TRD is starting..."))
        fsm_builder.add_state(TrdState.CMD_ARGS_PARSED,
                              on_enter=self.do_parse_args)
        fsm_builder.add_state(TrdState.CMD_ARGS_GIVEN,
                              on_enter=self.print_argument_configuration)
        fsm_builder.add_state(TrdState.BANNER_PRINTED,
                              on_enter=self.do_print_banner)
        fsm_builder.add_state(TrdState.LOGGERS_INITIATED,
                              on_enter=self.do_initiate_loggers)
        fsm_builder.add_state(TrdState.NODE_CLIENT_BUILT,
                              on_enter=self.do_build_node_client)
        fsm_builder.add_state(TrdState.NW_CONFIG_BUILT,
                              on_enter=self.do_build_nw_config)
        fsm_builder.add_state(TrdState.CONFIG_LOADED,
                              on_enter=self.do_load_config)
        fsm_builder.add_state(TrdState.DIRS_SET_UP,
                              on_enter=self.do_set_up_dirs)
        fsm_builder.add_state(TrdState.SIGNALS_REGISTERED,
                              on_enter=self.do_register_signals)
        fsm_builder.add_state(TrdState.LOCKED, on_enter=self.do_lock)
        fsm_builder.add_state(
            TrdState.NOT_LOCKED,
            on_enter=lambda e: logger.debug("No locking needed!"))
        fsm_builder.add_state(TrdState.FEES_INIT,
                              on_enter=self.do_init_service_fees)
        fsm_builder.add_state(TrdState.PLUGINS_LOADED,
                              on_enter=self.do_load_plugins)
        fsm_builder.add_state(TrdState.PRODUCERS_READY,
                              on_enter=self.do_launch_producers)
        fsm_builder.add_state(TrdState.CONSUMERS_READY,
                              on_enter=self.do_launch_consumers)
        fsm_builder.add_state(
            TrdState.NO_CONSUMERS_READY,
            on_enter=lambda e: logger.debug("No consumers needed!"))
        fsm_builder.add_state(TrdState.READY, on_enter=self.print_ready)
        fsm_builder.add_final_state(TrdState.SHUTTING,
                                    on_enter=self.do_shut_down)

        fsm_builder.add_conditional_transition(TrdEvent.LAUNCH,
                                               TrdState.INITIAL,
                                               self.is_args_not_set,
                                               TrdState.CMD_ARGS_PARSED,
                                               TrdState.CMD_ARGS_GIVEN)
        fsm_builder.add_transition(
            TrdEvent.PRINT_BANNER,
            [TrdState.CMD_ARGS_PARSED, TrdState.CMD_ARGS_GIVEN],
            TrdState.BANNER_PRINTED)
        fsm_builder.add_transition(TrdEvent.INITIATE_LOGGERS,
                                   TrdState.BANNER_PRINTED,
                                   TrdState.LOGGERS_INITIATED)
        fsm_builder.add_transition(TrdEvent.BUILD_NODE_CLIENT,
                                   TrdState.LOGGERS_INITIATED,
                                   TrdState.NODE_CLIENT_BUILT)
        fsm_builder.add_transition(TrdEvent.BUILD_NW_CONFIG,
                                   TrdState.NODE_CLIENT_BUILT,
                                   TrdState.NW_CONFIG_BUILT)
        fsm_builder.add_transition(TrdEvent.LOAD_CONFIG,
                                   TrdState.NW_CONFIG_BUILT,
                                   TrdState.CONFIG_LOADED)
        fsm_builder.add_transition(TrdEvent.SET_UP_DIRS,
                                   TrdState.CONFIG_LOADED,
                                   TrdState.DIRS_SET_UP)
        fsm_builder.add_transition(TrdEvent.REGISTER_SIGNALS,
                                   TrdState.DIRS_SET_UP,
                                   TrdState.SIGNALS_REGISTERED)

        fsm_builder.add_conditional_transition(TrdEvent.LOCK,
                                               TrdState.SIGNALS_REGISTERED,
                                               self.is_dry_run,
                                               TrdState.NOT_LOCKED,
                                               TrdState.LOCKED)
        fsm_builder.add_transition(TrdEvent.INIT_FEES,
                                   [TrdState.LOCKED, TrdState.NOT_LOCKED],
                                   TrdState.FEES_INIT)
        fsm_builder.add_transition(TrdEvent.LOAD_PLUGINS, TrdState.FEES_INIT,
                                   TrdState.PLUGINS_LOADED)

        fsm_builder.add_transition(TrdEvent.LAUNCH_PRODUCERS,
                                   TrdState.PLUGINS_LOADED,
                                   TrdState.PRODUCERS_READY)
        fsm_builder.add_conditional_transition(TrdEvent.LAUNCH_CONSUMERS,
                                               TrdState.PRODUCERS_READY,
                                               self.is_dry_run_no_consumers,
                                               TrdState.NO_CONSUMERS_READY,
                                               TrdState.CONSUMERS_READY)
        fsm_builder.add_transition(
            TrdEvent.GO_READY,
            [TrdState.CONSUMERS_READY, TrdState.NO_CONSUMERS_READY],
            TrdState.READY)
        fsm_builder.add_global_transition(TrdEvent.SHUT_DOWN,
                                          TrdState.SHUTTING)

        self.fsm = fsm_builder.build()