Пример #1
0
    def run_process(cls, alchemy_cfg: Dict[str, Any], herald_cfg: Dict[str,
                                                                       Any],
                    sentry_cfg: Dict[str, Any], packs_cfg: Dict[str, Any],
                    constellation_cfg: Dict[str, Any], logging_cfg: Dict[str,
                                                                         Any]):
        """Blockingly create and run the Constellation.

        This should be used as the target of a :class:`multiprocessing.Process`."""
        ru.init_logging(logging_cfg)

        if sentry_cfg is None or not sentry_cfg["enabled"]:
            log.info("Sentry: disabled")
        else:
            try:
                ru.init_sentry(sentry_cfg)
            except ImportError:
                log.info("Sentry: not installed")

        constellation = cls(alchemy_cfg=alchemy_cfg,
                            herald_cfg=herald_cfg,
                            packs_cfg=packs_cfg,
                            constellation_cfg=constellation_cfg,
                            logging_cfg=logging_cfg)

        # Run the server
        constellation.run_blocking()
Пример #2
0
    def run_process(cls, **kwargs):
        """Blockingly create and run the Serf.

        This should be used as the target of a :class:`multiprocessing.Process`."""
        ru.init_logging(kwargs["logging_cfg"])

        if kwargs["sentry_cfg"] is None or not kwargs["sentry_cfg"]["enabled"]:
            log.info("Sentry: disabled")
        else:
            try:
                ru.init_sentry(kwargs["sentry_cfg"])
            except ImportError:
                log.info("Sentry: not installed")

        loop = aio.get_event_loop()

        serf = cls(loop=loop, **kwargs)

        try:
            serf.loop.run_until_complete(serf.run())
        except Exception as e:
            ru.sentry_exc(e, level="fatal")
Пример #3
0
def run(config_file: str):
    # Read the configuration file
    config: dict = toml.load(config_file)

    ru.init_logging(config["Logging"])

    if config["Sentry"] is None or not config["Sentry"]["enabled"]:
        log.info("Sentry: disabled")
    else:
        try:
            ru.init_sentry(config["Sentry"])
        except ImportError:
            log.info("Sentry: not installed")
        else:
            log.info("Sentry: enabled")

    processes: Dict[str, ru.RoyalnetProcess] = {}
    """A list of all processes that the launcher should start and monitor."""

    herald_cfg = config.get("Herald")
    if rh is None:
        log.info("Herald: Not installed")
    elif herald_cfg is None:
        log.warning("Herald: Not configured")
    elif not herald_cfg["enabled"]:
        log.info("Herald: Disabled")
    elif herald_cfg["mode"] == "local":
        log.info("Herald: Enabled (local server)")

        def herald_constructor() -> multiprocessing.Process:
            # Create a Herald server
            herald_server = rh.Server(
                rh.Config.from_config(name="<server>", **herald_cfg))
            # Run the Herald server on a new process
            return multiprocessing.Process(
                name="Herald.Local",
                target=herald_server.run_blocking,
                daemon=True,
                kwargs={"logging_cfg": config["Logging"]})

        processes["Herald"] = ru.RoyalnetProcess(herald_constructor, None)
    elif herald_cfg["mode"] == "remote":
        log.info("Herald: Enabled (remote server)")
    else:
        log.error(f"Invalid Herald mode: {herald_cfg['mode']}")

    # Serfs
    serfs_cfg = config.get("Serfs")
    if serfs_cfg is None:
        log.warning("__serfs__: Not configured")
    else:
        log.debug("__serfs__: Configured")

        def configure_serf(n: str, module, class_: Type[rs.Serf]):
            serf_cfg = serfs_cfg.get(n)
            if module is None:
                log.info(f"Serf.{n}: Not installed")
            elif serf_cfg is None:
                log.warning(f"Serf.{n}: Not configured")
            elif not serf_cfg["enabled"]:
                log.info(f"Serf.{n}: Disabled")
            else:

                def serf_constructor() -> multiprocessing.Process:
                    return multiprocessing.Process(name=f"Serf.{n}",
                                                   target=class_.run_process,
                                                   daemon=True,
                                                   kwargs={
                                                       "alchemy_cfg":
                                                       config["Alchemy"],
                                                       "herald_cfg":
                                                       herald_cfg,
                                                       "packs_cfg":
                                                       config["Packs"],
                                                       "sentry_cfg":
                                                       config["Sentry"],
                                                       "logging_cfg":
                                                       config["Logging"],
                                                       "serf_cfg":
                                                       serf_cfg,
                                                   })

                processes[f"Serf.{n}"] = ru.RoyalnetProcess(
                    serf_constructor, None)
                log.info(f"Serf.{n}: Enabled")

        if rst is not None:
            configure_serf("Telegram", rst, rst.TelegramSerf)
        if rsd is not None:
            configure_serf("Discord", rsd, rsd.DiscordSerf)

    # Constellation
    constellation_cfg = config.get("Constellation")
    if rc is None:
        log.info(f"Constellation: Not installed")
    elif constellation_cfg is None:
        log.warning(f"Constellation: Not configured")
    elif not constellation_cfg["enabled"]:
        log.info(f"Constellation: Disabled")
    else:

        def constellation_constructor() -> multiprocessing.Process:
            return multiprocessing.Process(name="Constellation",
                                           target=rc.Constellation.run_process,
                                           daemon=True,
                                           kwargs={
                                               "alchemy_cfg":
                                               config["Alchemy"],
                                               "herald_cfg":
                                               herald_cfg,
                                               "packs_cfg":
                                               config["Packs"],
                                               "sentry_cfg":
                                               config["Sentry"],
                                               "logging_cfg":
                                               config["Logging"],
                                               "constellation_cfg":
                                               config["Constellation"],
                                           })

        processes["Constellation"] = ru.RoyalnetProcess(
            constellation_constructor, None)

        log.info("Constellation: Enabled")

    try:
        # Monitor processes
        while True:
            log.debug("Checking process status...")
            for name, process in processes.items():
                if process.current_process is None:
                    log.info(f"{name}: Starting...")
                    process.current_process = process.constructor()
                    process.current_process.start()
                elif not process.current_process.is_alive():
                    log.error(f"{name}: Process is dead, restarting...")
                    process.current_process = process.constructor()
                    process.current_process.start()
            log.debug("Done, checking again in 60 seconds.")
            time.sleep(60)
    except KeyboardInterrupt:
        log.info("Received SIGTERM, stopping everything!")
        for name, process in processes.items():
            log.info(f"{name}: Killing...")
            process.current_process.kill()
        log.info("Goodbye!")
Пример #4
0
 def run_blocking(self, logging_cfg: Dict[str, Any]):
     ru.init_logging(logging_cfg)
     if self.loop is None:
         self.loop = asyncio.get_event_loop()
     self.serve()
Пример #5
0
def run(config_filename: str):
    # Read the configuration file
    with open(config_filename, "r") as t:
        config: dict = toml.load(t)

    ru.init_logging(config["Logging"])

    if config["Sentry"] is None or not config["Sentry"]["enabled"]:
        log.info("Sentry: disabled")
    else:
        try:
            ru.init_sentry(config["Sentry"])
        except ImportError:
            log.info("Sentry: not installed")

    # Herald Server
    herald_cfg = None
    herald_process = None
    if rh is not None and "Herald" in config:
        if "Local" in config["Herald"] and config["Herald"]["Local"]["enabled"]:
            # Create a Herald server
            herald_server = rh.Server(rh.Config.from_config(name="<server>", **config["Herald"]["Local"]))
            # Run the Herald server on a new process
            herald_process = multiprocessing.Process(name="Herald.Local",
                                                     target=herald_server.run_blocking,
                                                     daemon=True,
                                                     kwargs={
                                                         "logging_cfg": config["Logging"]
                                                     })
            herald_process.start()
            herald_cfg = config["Herald"]["Local"]
            log.info("Herald: Enabled (Local)")
        elif "Remote" in config["Herald"] and config["Herald"]["Remote"]["enabled"]:
            log.info("Herald: Enabled (Remote)")
            herald_cfg = config["Herald"]["Remote"]
        else:
            log.info("Herald: Disabled")
    else:
        log.info("Herald: Disabled")

    # Serfs
    telegram_process = None
    if rst is not None and "Telegram" in config["Serfs"] and config["Serfs"]["Telegram"]["enabled"]:
        telegram_process = multiprocessing.Process(name="Serf.Telegram",
                                                   target=rst.TelegramSerf.run_process,
                                                   daemon=True,
                                                   kwargs={
                                                       "alchemy_cfg": config["Alchemy"],
                                                       "herald_cfg": herald_cfg,
                                                       "packs_cfg": config["Packs"],
                                                       "sentry_cfg": config["Sentry"],
                                                       "logging_cfg": config["Logging"],
                                                       "serf_cfg": config["Serfs"]["Telegram"],
                                                   })
        telegram_process.start()
        log.info("Serf.Telegram: Started")
    else:
        log.info("Serf.Telegram: Disabled")

    discord_process = None
    if rsd is not None and "Discord" in config["Serfs"] and config["Serfs"]["Discord"]["enabled"]:
        discord_process = multiprocessing.Process(name="Serf.Discord",
                                                  target=rsd.DiscordSerf.run_process,
                                                  daemon=True,
                                                  kwargs={
                                                      "alchemy_cfg": config["Alchemy"],
                                                      "herald_cfg": herald_cfg,
                                                      "packs_cfg": config["Packs"],
                                                      "sentry_cfg": config["Sentry"],
                                                      "logging_cfg": config["Logging"],
                                                      "serf_cfg": config["Serfs"]["Discord"],
                                                  })
        discord_process.start()
        log.info("Serf.Discord: Started")
    else:
        log.info("Serf.Discord: Disabled")

    matrix_process = None
    if rsm is not None and "Matrix" in config["Serfs"] and config["Serfs"]["Matrix"]["enabled"]:
        matrix_process = multiprocessing.Process(name="Serf.Matrix",
                                                 target=rsm.MatrixSerf.run_process,
                                                 daemon=True,
                                                 kwargs={
                                                     "alchemy_cfg": config["Alchemy"],
                                                     "herald_cfg": herald_cfg,
                                                     "packs_cfg": config["Packs"],
                                                     "sentry_cfg": config["Sentry"],
                                                     "logging_cfg": config["Logging"],
                                                     "serf_cfg": config["Serfs"]["Matrix"],
                                                 })
        matrix_process.start()
        log.info("Serf.Matrix: Started")
    else:
        log.info("Serf.Matrix: Disabled")

    # Constellation
    constellation_process = None
    if rc is not None and "Constellation" in config and config["Constellation"]["enabled"]:
        constellation_process = multiprocessing.Process(name="Constellation",
                                                        target=rc.Constellation.run_process,
                                                        daemon=True,
                                                        kwargs={
                                                            "alchemy_cfg": config["Alchemy"],
                                                            "herald_cfg": herald_cfg,
                                                            "packs_cfg": config["Packs"],
                                                            "sentry_cfg": config["Sentry"],
                                                            "logging_cfg": config["Logging"],
                                                            "constellation_cfg": config["Constellation"],
                                                        })
        constellation_process.start()
        log.info("Constellation: Started")
    else:
        log.info("Constellation: Disabled")

    log.info("All processes started!")
    if constellation_process is not None:
        log.info("Waiting for Constellation to stop...")
        constellation_process.join()
    if telegram_process is not None:
        log.info("Waiting for Serf.Telegram to stop...")
        telegram_process.join()
    if discord_process is not None:
        log.info("Waiting for Serf.Discord to stop...")
        discord_process.join()
    if matrix_process is not None:
        log.info("Waiting for Serf.Matrix to stop...")
        matrix_process.join()
    if herald_process is not None:
        log.info("Waiting for Herald to stop...")
        herald_process.join()