Skip to content
Open
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
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ config = "0.13.0"
const_format = "0.2"
crossbeam-utils = "0.8"
dashmap = "6.1"
daemonize = "0.5"
datafusion = "50"
datafusion-common = "50"
datafusion-expr = "50"
Expand Down
1 change: 1 addition & 0 deletions src/cmd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ tonic.workspace = true
tracing-appender.workspace = true

[target.'cfg(unix)'.dependencies]
daemonize.workspace = true
pprof = { version = "0.14", features = [
"flamegraph",
] }
Expand Down
47 changes: 47 additions & 0 deletions src/cmd/src/standalone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,46 @@
/// The working home directory of this standalone instance.
#[clap(long)]
data_home: Option<String>,
/// Run as a daemon (Unix only).
#[clap(short = 'd', long)]
daemon: bool,
}

impl StartCommand {
/// Daemonize the process (Unix only).
#[cfg(unix)]
fn daemonize(log_dir: &str, data_home: &str) -> Result<()> {
use daemonize::Daemonize;
use std::path::PathBuf;

Check warning on line 230 in src/cmd/src/standalone.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

Diff in /home/runner/work/greptimedb/greptimedb/src/cmd/src/standalone.rs

// Ensure data_home and log_dir exist
fs::create_dir_all(data_home).context(error::CreateDirSnafu { dir: data_home })?;
fs::create_dir_all(log_dir).context(error::CreateDirSnafu { dir: log_dir })?;

let stdout_path = PathBuf::from(log_dir).join("greptime.stdout");
let stderr_path = PathBuf::from(log_dir).join("greptimedb.stderr");

let stdout = fs::File::create(&stdout_path).context(error::FileIoSnafu)?;
let stderr = fs::File::create(&stderr_path).context(error::FileIoSnafu)?;

let daemonize = Daemonize::new()
.working_directory(data_home)
.stdout(stdout)
.stderr(stderr);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we keep a pid file so we can implement greptime standalone stop


daemonize
.start()
.map_err(|e| {
common_error::ext::BoxedError::new(common_error::ext::PlainError::new(
format!("Failed to daemonize: {}", e),
common_error::status_code::StatusCode::Internal,
))
})
.context(error::OtherSnafu)?;

Ok(())
}

/// Load the GreptimeDB options from various sources (command line, config file or env).
pub fn load_options(
&self,
Expand Down Expand Up @@ -312,6 +349,10 @@
opts.user_provider = Some(user_provider.clone());
}

if self.daemon {
opts.daemon = true;
}

Ok(())
}

Expand All @@ -320,6 +361,12 @@
#[allow(clippy::diverging_sub_expression)]
/// Build GreptimeDB instance with the loaded options.
pub async fn build(&self, opts: GreptimeOptions<StandaloneOptions>) -> Result<Instance> {
// Handle daemonization before initializing anything else
#[cfg(unix)]
if opts.component.daemon {
Self::daemonize(&opts.component.logging.dir, &opts.component.storage.data_home)?;
}

Check warning on line 368 in src/cmd/src/standalone.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

Diff in /home/runner/work/greptimedb/greptimedb/src/cmd/src/standalone.rs

common_runtime::init_global_runtimes(&opts.runtime);

let guard = common_telemetry::init_global_logging(
Expand Down
2 changes: 2 additions & 0 deletions src/standalone/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use servers::http::HttpOptions;
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(default)]
pub struct StandaloneOptions {
pub daemon: bool,
pub enable_telemetry: bool,
pub default_timezone: Option<String>,
pub default_column_prefix: Option<String>,
Expand Down Expand Up @@ -66,6 +67,7 @@ pub struct StandaloneOptions {
impl Default for StandaloneOptions {
fn default() -> Self {
Self {
daemon: false,
enable_telemetry: true,
default_timezone: None,
default_column_prefix: None,
Expand Down
Loading