Ejemplo n.º 1
0
def run_forever(
    ctx: typer.Context,
    workers: int = 1,
    bind_host: str = "127.0.0.1",
    port: int = 0,
    daemonize: bool = False,
    daemonize_stdout: str = "/dev/null",
    daemonize_stderr: str = "/dev/null",
):
    if len(ctx.args) == 0:
        raise Exception("you have to provide a program to execute")
    kwargs: Dict[str, Any] = {
        "program": ctx.args[0],
        "stdout": "PIPE",
        "stderr": "PIPE",
        "args": ctx.args[1:],
    }
    config = CmdConfiguration(**kwargs)  # type: ignore
    cmd = Cmd(config)
    service = Service("forever_cmd", workers, cmd)
    daemon = Daemon(services_to_add=[service], bind_host=bind_host, port=port)
    set_instance(daemon)
    daemon.run(
        daemonize=daemonize,
        daemonize_stderr=daemonize_stderr,
        daemonize_stdout=daemonize_stdout,
    )
Ejemplo n.º 2
0
async def test_smart_stop1():
    a = ManagedProcess("foo", Cmd.make_from_shell_cmd(f"{DIR}/smart_stop.py"))
    await a.start()
    assert a.state == ManagedProcessState.RUNNING
    assert a.pid > 0
    assert a.returncode is None
    await a.stop()
    assert a.state == ManagedProcessState.DEAD
    assert a.returncode == -15
    assert a.pid is None
Ejemplo n.º 3
0
async def test_stop():
    a = ManagedProcess("foo", Cmd.make_from_shell_cmd("sleep 1"))
    await a.start()
    assert a.state == ManagedProcessState.RUNNING
    assert a.pid > 0
    assert a.returncode is None
    await a.stop()
    assert a.state == ManagedProcessState.DEAD
    assert a.returncode != 0
    assert a.pid is None
Ejemplo n.º 4
0
async def test_basic():
    a = Service(
        "foo", 3,
        Cmd.make_from_shell_cmd("sleep 10", waiting_for_restart_delay=0))
    await a.start()
    assert a.is_running()
    a.as_dict()
    await asyncio.sleep(1)
    await a.shutdown()
    await a.wait()
    assert a.is_shutdown()
Ejemplo n.º 5
0
async def test_scaling_down():
    a = Service(
        "foo", 3,
        Cmd.make_from_shell_cmd("sleep 10", waiting_for_restart_delay=0))
    await a.start()
    assert a.number_of_slots_running() == 3
    await asyncio.sleep(1)
    await a.set_slot_number(1)
    assert a.number_of_slots_running() == 1
    await asyncio.sleep(1)
    await a.shutdown()
    await a.wait()
Ejemplo n.º 6
0
async def test_start():
    a = ManagedProcess("foo", Cmd.make_from_shell_cmd("sleep 1"))
    await a.start()
    assert a.state == ManagedProcessState.RUNNING
    assert a.pid > 0
    assert a.is_alive()
    assert a.returncode is None
    await a.wait()
    assert a.state == ManagedProcessState.STOPPED
    assert a.returncode == 0
    assert a.pid is None
    assert not a.is_alive()
Ejemplo n.º 7
0
 def __init__(self, name_prefix, slot_number, cmd: Cmd):
     self.name_prefix = name_prefix
     self.slot_number: int = slot_number
     self.name = self.name_prefix + "." + str(self.slot_number)
     self.cmd: Cmd = Cmd.copy_and_add_to_context(cmd,
                                                 {"SLOT": self.slot_number})
     self.logger = mflog.get_logger("alwaysup.process_slot").bind(
         id=self.name)
     StateMixin.__init__(self, logger=self.logger)
     self.managed_process: Optional[ManagedProcess] = None
     self.set_state(ProcessSlotState.STOPPED)
     self._manage_task = asyncio.create_task(log_exceptions(self._manage()))
     self._waiting_for_restart_task = None
Ejemplo n.º 8
0
async def test_slot1():
    a = ProcessSlot(
        "foo", 0,
        Cmd.make_from_shell_cmd("sleep 1", waiting_for_restart_delay=0))
    await a.start()
    assert a.pid > 0
    a.as_dict()
    assert a.state == ProcessSlotState.RUNNING
    assert a.is_running()
    await asyncio.sleep(2)
    await a.shutdown()
    assert a.is_shutdown()
    await a.wait()
Ejemplo n.º 9
0
async def test_basic():
    x = Manager()
    assert x.is_running()
    a = Service(
        "foo", 3,
        Cmd.make_from_shell_cmd("sleep 10", waiting_for_restart_delay=0))
    await x.add_service(a)
    assert len(x.as_dict()["services"]) > 0
    assert a.is_running()
    await asyncio.sleep(1)
    await x.shutdown()
    await x.wait()
    assert x.is_shutdown()
    assert a.is_shutdown()
Ejemplo n.º 10
0
async def add_service(service_body: ServiceBody = Body(...)):
    if service_body.name is None:
        raise HTTPException(status_code=400,
                            detail="missing name property in the body")
    if service_body.program is None:
        raise HTTPException(status_code=400,
                            detail="missing program property in the body")
    manager = get_instance().manager
    if service_body.name in manager.services:
        raise HTTPException(status_code=409, detail="service already exist")
    config: CmdConfiguration = cast(CmdConfiguration, service_body)
    cmd = Cmd(config)
    service = Service(service_body.name, service_body.workers, cmd)
    await manager.add_service(service)
    return {"name": service_body.name}
Ejemplo n.º 11
0
async def test_misc():
    a = ManagedProcess("foo",
                       Cmd.make_from_shell_cmd("/foo/bar/does_not_exist"))
    await a.start()
    await a.wait()
    assert a.state == ManagedProcessState.DEAD
Ejemplo n.º 12
0
async def test_ready():
    a = ManagedProcess("foo", Cmd.make_from_shell_cmd("true"))
    assert a.state == ManagedProcessState.READY