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

chore(toolchain): allow hooking to existing game server process #563

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading