Skip to content
This repository has been archived by the owner on Dec 21, 2024. It is now read-only.

Commit

Permalink
chore(toolchain): allow hooking to existing game server process
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFlurry committed Oct 7, 2024
1 parent 8e1c48b commit 2ed757a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
32 changes: 32 additions & 0 deletions packages/toolchain/src/tasks/game_server/hook.rs
Original file line number Diff line number Diff line change
@@ -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<i32>,
}

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<Self::Output> {
let exit_code = crate::game_server::PROCESS_MANAGER
.hook(task.clone())
.await?;
Ok(Output { exit_code })
}
}
1 change: 1 addition & 0 deletions packages/toolchain/src/tasks/game_server/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod hook;
pub mod start;
pub mod stop;
1 change: 1 addition & 0 deletions packages/toolchain/src/tasks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions packages/toolchain/src/util/process_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ impl ProcessManager {
})
}

/// Starts a new task or hooks in to the existing task.
pub async fn start<CommandFut>(
self: &Arc<Self>,
task_ctx: TaskCtx,
Expand Down Expand Up @@ -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<Self>, task_ctx: TaskCtx) -> Result<Option<i32>> {
// 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<Self>, task_ctx: TaskCtx) -> Result<Option<i32>> {
// Write events to task
let mut event_rx = self.event_tx.subscribe();
let log_fut = async {
Expand Down

0 comments on commit 2ed757a

Please sign in to comment.