Skip to content

Commit

Permalink
migrate to new config
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaumy committed Dec 12, 2024
1 parent c8d8eb4 commit 79b6802
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
25 changes: 25 additions & 0 deletions src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,37 @@

use std::fs::File;

use anyhow::Result;
use daemonize::Daemonize;

use crate::config::{DaemonConfig, DaemonWasmConfig};

/// run the process as daemon
pub fn spawn_daemon(cfg: DaemonConfig) -> Result<()> {
let stdout = File::create(cfg.stdout)?;
let stderr = File::create(cfg.stderr)?;

let daemonize = Daemonize::new()
.pid_file(cfg.pid_file)
.chown_pid_file(true) // is optional, see `Daemonize` documentation
.working_directory(cfg.workdir)
.user("root")
.group("root")
.umask(0o027) // Set umask, `0o027` by default.
.stdout(stdout) // by default, stdout is redirect to `/tmp/psh.stdout`.
.stderr(stderr); // by default, stderr is redirect to `/tmp/psh.stderr`.

daemonize.start()?;

Ok(())
}

pub fn get_daemon_wasm_args(cfg: DaemonWasmConfig) -> Option<Vec<String>> {
if !cfg.enable {
return None;
}
let mut vec = Vec::with_capacity(cfg.args.len() + 1);
vec.push(cfg.path);
vec.extend(cfg.args);
Some(vec)
}
34 changes: 15 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ use args::Args;
use chrono::offset::LocalResult;
use chrono::{TimeZone, Utc};
use clap::Parser;
use config::{PshConfig, RemoteConfig};
use config::RemoteConfig;
use daemon::{get_daemon_wasm_args, spawn_daemon};
use log::log_init;
use opentelemetry_otlp::ExportConfig;
use runtime::{Task, TaskRuntime};
Expand All @@ -48,20 +49,17 @@ fn main() -> Result<()> {
}

let mut args = Args::parse();
let mut psh_config = PshConfig::read_config(&args.config).unwrap_or_else(|e| {
tracing::warn!("{e}, use default Psh config.");
PshConfig::default()
});
let cfg = config::read_or_gen(args.config.clone())?;

// When running as a daemon, it ignores all other cli arguments
let component_args = if args.systemd() || args.daemon() {
psh_config.get_component_args()
get_daemon_wasm_args(cfg.daemon.wasm.clone())
} else {
args.get_component_args()
};

if args.daemon() {
daemon::Daemon::new(psh_config.daemon().clone()).daemon()?;
spawn_daemon(cfg.daemon)?;
}

let mut task_rt = TaskRuntime::new()?;
Expand All @@ -78,7 +76,7 @@ fn main() -> Result<()> {

thread::spawn(move || -> Result<()> {
let rt = tokio::runtime::Runtime::new()?;
let tasks = async_tasks(psh_config.remote.clone(), psh_config.take_token(), task_rt);
let tasks = async_tasks(cfg.remote, task_rt);
rt.block_on(tasks)?;
Ok(())
})
Expand All @@ -88,22 +86,17 @@ fn main() -> Result<()> {
Ok(())
}

async fn async_tasks(
remote_cfg: RemoteConfig,
token: String,
mut task_rt: TaskRuntime,
) -> Result<()> {
let token_ = token.clone();

async fn async_tasks(remote_cfg: RemoteConfig, mut task_rt: TaskRuntime) -> Result<()> {
let token_cloned = remote_cfg.token.clone();
let rpc_task = async move {
if !remote_cfg.enable {
let handle = task_rt.spawn(None)?;
handle.join().expect("TaskRuntime has panicked");
return Ok(());
}

let duration = Duration::from_secs(remote_cfg.rpc.duration);
let mut client = RpcClient::new(remote_cfg.rpc, token_).await?;
let duration = Duration::from_secs(remote_cfg.rpc.heartbeat_interval);
let mut client = RpcClient::new(remote_cfg.rpc, token_cloned).await?;
task_rt.spawn(Some(client.clone()))?;
client.send_info().await?;
loop {
Expand Down Expand Up @@ -135,8 +128,11 @@ async fn async_tasks(
return Ok(());
}

let export_conf: ExportConfig = remote_cfg.otlp_conf.into();
otlp::otlp_tasks(export_conf, token).await?;
let export_conf = ExportConfig {
endpoint: Some(remote_cfg.otlp.addr),
..Default::default()
};
otlp::otlp_tasks(export_conf, remote_cfg.token).await?;
Ok::<(), Error>(())
};

Expand Down
2 changes: 1 addition & 1 deletion src/services/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use anyhow::Result;
use tonic::transport::{Channel, ClientTlsConfig, Endpoint};
use tonic::Request;

use super::config::RpcConfig;
use super::pb::{self, DataRequest};
use crate::config::RpcConfig;
use crate::services::host_info::RawInfo;
use crate::services::pb::psh_service_client::PshServiceClient;
use crate::services::pb::HostInfoRequest;
Expand Down

0 comments on commit 79b6802

Please sign in to comment.