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

Commit

Permalink
feat: add process manager for game server & opengb dev server
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFlurry committed Sep 8, 2024
1 parent 214df69 commit fca03c4
Show file tree
Hide file tree
Showing 11 changed files with 358 additions and 17 deletions.
4 changes: 3 additions & 1 deletion packages/toolchain/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ use uuid::Uuid;

use crate::{
config, paths,
util::{cmd::shell_cmd, task},
util::{cmd::shell_cmd, process_manager::ProcessManagerConfig, task},
ToolchainCtx,
};

pub const PROCESS_MANAGER_DEV: ProcessManagerConfig = ProcessManagerConfig { key: "backend_dev" };

pub struct BackendCommandOpts {
pub command: &'static str,
pub opts: serde_json::Value,
Expand Down
10 changes: 10 additions & 0 deletions packages/toolchain/src/config/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ pub struct ProjectMeta {
pub cluster: Cluster,
pub tokens: Tokens,
pub environments: HashMap<Uuid, Environment>,

/// Stores the state for all of the process managers.
pub process_managers: HashMap<String, ProcessManagerState>,
}

impl ProjectMeta {
Expand All @@ -31,6 +34,7 @@ impl ProjectMeta {
cluster: Cluster { api_endpoint },
tokens: Tokens { cloud: cloud_token },
environments: HashMap::new(),
process_managers: HashMap::new(),
}
}
}
Expand Down Expand Up @@ -58,6 +62,12 @@ pub struct Backend {
pub db_url: Option<String>,
}

#[derive(Default, Clone, Serialize, Deserialize)]
pub struct ProcessManagerState {
/// PID of the currently running process. If none, no process is running.
pub pid: Option<u16>,
}

static SINGLETON: OnceCell<Mutex<Meta>> = OnceCell::const_new();

/// Gets the global config instance.
Expand Down
3 changes: 3 additions & 0 deletions packages/toolchain/src/game_server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
use crate::util::process_manager::ProcessManagerConfig;

pub const PROCESS_MANAGER: ProcessManagerConfig = ProcessManagerConfig { key: "game_server" };
1 change: 1 addition & 0 deletions packages/toolchain/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod backend;
pub mod config;
pub mod game;
pub mod game_server;
pub mod paths;
pub mod tasks;
pub mod toolchain_ctx;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl task::Task for Task {
type Output = Output;

fn name() -> &'static str {
"backend_dev"
"backend_start"
}

async fn run(task: task::TaskCtx, input: Self::Input) -> Result<Self::Output> {
Expand All @@ -34,18 +34,36 @@ impl task::Task for Task {
cmd_env.insert("OPENGB_PORT".into(), input.port.to_string());
cmd_env.insert("OPENGB_HOSTNAME".into(), "0.0.0.0".to_string());
cmd_env.insert("OPENGB_TERM_COLOR".into(), "never".into());
let exit_code = backend::run_opengb_command_from_task(
task.clone(),
backend::BackendCommandOpts {
command: "dev",
opts: serde_json::json!({
"project": config_path,
"nonInteractive": true
}),
env: cmd_env,
},
)
.await?;
// let exit_code = backend::run_opengb_command_from_task(
// task.clone(),
// backend::BackendCommandOpts {
// command: "dev",
// opts: serde_json::json!({
// "project": config_path,
// "nonInteractive": true
// }),
// env: cmd_env,
// },
// )
// .await?;

// let cmd = build_opengb_command(opts).await?;
let exit_code = backend::PROCESS_MANAGER_DEV
.start(task.clone(), todo!(), todo!(), todo!())
.await?;

// let exit_code = backend::run_opengb_command(
// task.clone(),
// backend::BackendCommandOpts {
// config_path,
// args: vec!["dev".into(), "--non-interactive".into()],
// env: cmd_env,
// cwd: input.cwd.into(),
// ports: vec![(input.port, input.port)],
// enable_postgres: true,
// },
// )
// .await?;

Ok(Output { exit_code })
}
Expand Down
26 changes: 26 additions & 0 deletions packages/toolchain/src/tasks/backend_stop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use anyhow::*;
use serde::{Deserialize, Serialize};

use crate::{backend, config, util::task};

#[derive(Deserialize)]
pub struct Input {}

#[derive(Serialize)]
pub struct Output {}

pub struct Task;

impl task::Task for Task {
type Input = Input;
type Output = Output;

fn name() -> &'static str {
"backend_stop"
}

async fn run(task: task::TaskCtx, input: Self::Input) -> Result<Self::Output> {
backend::PROCESS_MANAGER_DEV.stop().await?;
Ok(Output {})
}
}
35 changes: 35 additions & 0 deletions packages/toolchain/src/tasks/game_server_start.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use anyhow::*;
use serde::{Deserialize, Serialize};

use crate::util::task;

#[derive(Deserialize)]
pub struct Input {
pub cmd: String,
pub args: Vec<String>,
pub cwd: String,
}

#[derive(Serialize)]
pub struct Output {
exit_code: i32,
}

pub struct Task;

impl task::Task for Task {
type Input = Input;
type Output = Output;

fn name() -> &'static str {
"game_server_start"
}

async fn run(task: task::TaskCtx, input: Self::Input) -> Result<Self::Output> {
// TODO: Add abiality to pipe logs
let exit_code = crate::game_server::PROCESS_MANAGER
.start(task.clone(), input.cmd, input.args, input.cwd)
.await?;
Ok(Output { exit_code })
}
}
26 changes: 26 additions & 0 deletions packages/toolchain/src/tasks/game_server_stop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use anyhow::*;
use serde::{Deserialize, Serialize};

use crate::util::task;

#[derive(Deserialize)]
pub struct Input {}

#[derive(Serialize)]
pub struct Output {}

pub struct Task;

impl task::Task for Task {
type Input = Input;
type Output = Output;

fn name() -> &'static str {
"game_server_stop"
}

async fn run(task: task::TaskCtx, input: Self::Input) -> Result<Self::Output> {
crate::game_server::PROCESS_MANAGER.stop().await?;
Ok(Output {})
}
}
11 changes: 8 additions & 3 deletions packages/toolchain/src/tasks/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
pub mod backend_choose_local_port;
pub mod backend_dev;
pub mod backend_sdk_gen;
pub mod backend_start;
pub mod backend_stop;
pub mod check_login_state;
pub mod check_system_requirements;
pub mod deploy;
pub mod exec_command;
pub mod game_server_start;
pub mod game_server_stop;
pub mod get_bootstrap_data;
pub mod get_hub_link;
pub mod get_settings_paths;
Expand All @@ -16,18 +19,20 @@ pub mod wait_for_login;

crate::task_registry!(
backend_choose_local_port::Task,
backend_dev::Task,
backend_sdk_gen::Task,
backend_start::Task,
backend_stop::Task,
check_login_state::Task,
check_system_requirements::Task,
deploy::Task,
exec_command::Task,
game_server_start::Task,
get_bootstrap_data::Task,
get_hub_link::Task,
get_settings_paths::Task,
open::Task,
show_term::Task,
start_device_link::Task,
unlink::Task,
wait_for_login::Task,
get_settings_paths::Task,
);
1 change: 1 addition & 0 deletions packages/toolchain/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod docker;
pub mod lz4;
pub mod net;
pub mod os;
pub mod process_manager;
pub mod show_term;
pub mod task;
pub mod term;
Loading

0 comments on commit fca03c4

Please sign in to comment.