From 2ed757a9926b96e59555d7f3f9907954b2611b7e Mon Sep 17 00:00:00 2001 From: Nathan Flurry Date: Mon, 7 Oct 2024 05:44:25 -0700 Subject: [PATCH] chore(toolchain): allow hooking to existing game server process --- .../toolchain/src/tasks/game_server/hook.rs | 32 +++++++++++++++++++ .../toolchain/src/tasks/game_server/mod.rs | 1 + packages/toolchain/src/tasks/mod.rs | 1 + .../toolchain/src/util/process_manager/mod.rs | 15 +++++++++ 4 files changed, 49 insertions(+) create mode 100644 packages/toolchain/src/tasks/game_server/hook.rs diff --git a/packages/toolchain/src/tasks/game_server/hook.rs b/packages/toolchain/src/tasks/game_server/hook.rs new file mode 100644 index 00000000..ccc9722a --- /dev/null +++ b/packages/toolchain/src/tasks/game_server/hook.rs @@ -0,0 +1,32 @@ +use std::collections::HashMap; + +use anyhow::*; +use serde::{Deserialize, Serialize}; + +use crate::util::task; + +#[derive(Deserialize)] +pub struct Input {} + +#[derive(Serialize)] +pub struct Output { + exit_code: Option, +} + +pub struct Task; + +impl task::Task for Task { + type Input = Input; + type Output = Output; + + fn name() -> &'static str { + "game_server.hook" + } + + async fn run(task: task::TaskCtx, _input: Self::Input) -> Result { + let exit_code = crate::game_server::PROCESS_MANAGER + .hook(task.clone()) + .await?; + Ok(Output { exit_code }) + } +} diff --git a/packages/toolchain/src/tasks/game_server/mod.rs b/packages/toolchain/src/tasks/game_server/mod.rs index 99457f5f..dc6c021e 100644 --- a/packages/toolchain/src/tasks/game_server/mod.rs +++ b/packages/toolchain/src/tasks/game_server/mod.rs @@ -1,2 +1,3 @@ +pub mod hook; pub mod start; pub mod stop; diff --git a/packages/toolchain/src/tasks/mod.rs b/packages/toolchain/src/tasks/mod.rs index 0b56ba94..c6ed1ae6 100644 --- a/packages/toolchain/src/tasks/mod.rs +++ b/packages/toolchain/src/tasks/mod.rs @@ -18,6 +18,7 @@ crate::task_registry!( backend::stop::Task, deploy::Task, exec_command::Task, + game_server::hook::Task, game_server::start::Task, game_server::stop::Task, get_bootstrap_data::Task, diff --git a/packages/toolchain/src/util/process_manager/mod.rs b/packages/toolchain/src/util/process_manager/mod.rs index 3f868fdb..371e419e 100644 --- a/packages/toolchain/src/util/process_manager/mod.rs +++ b/packages/toolchain/src/util/process_manager/mod.rs @@ -111,6 +111,7 @@ impl ProcessManager { }) } + /// Starts a new task or hooks in to the existing task. pub async fn start( self: &Arc, task_ctx: TaskCtx, @@ -156,6 +157,20 @@ impl ProcessManager { self.spawn_process(command_opts).await?; }; + self.hook_inner(task_ctx).await + } + + /// Hooks in to an existing task, if exists. + pub async fn hook(self: &Arc, task_ctx: TaskCtx) -> Result> { + // Check if task exists already + if !self.status_rx.borrow().is_running() { + return Ok(None); + } + + self.hook_inner(task_ctx).await + } + + async fn hook_inner(self: &Arc, task_ctx: TaskCtx) -> Result> { // Write events to task let mut event_rx = self.event_tx.subscribe(); let log_fut = async {