Skip to main content

studio_worker/
lib.rs

1//! Library surface for the `studio-worker` binary.
2//!
3//! Exposes the worker's modules so integration tests (and downstream
4//! tooling) can drive the contract without going through the CLI.
5
6// Enable the unstable `#[coverage(off)]` attribute used across the
7// crate (behind `#[cfg_attr(coverage_nightly, ...)]`) to exclude
8// host-, network-, and platform-dependent code from `cargo llvm-cov`.
9// `coverage_nightly` is set only by cargo-llvm-cov on a nightly
10// toolchain; on stable it's unset, so this gate (and every
11// `coverage(off)` annotation) compiles to nothing. Without it,
12// `cargo +nightly llvm-cov` fails to compile the crate (E0658).
13#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
14
15pub mod auto_register;
16pub mod autostart;
17pub mod cli;
18pub mod config;
19pub mod engine;
20pub mod http;
21pub mod runtime;
22pub mod service;
23pub mod sys;
24pub mod telemetry;
25#[doc(hidden)]
26pub mod test_support;
27pub mod types;
28#[cfg(feature = "ui")]
29pub mod ui;
30pub mod update;
31pub mod ws;
32
33pub const AGENT_VERSION: &str = env!("CARGO_PKG_VERSION");
34
35/// Sentry release identifier in the `<pkg>@<version>` form expected by
36/// Sentry's organisation-wide *Releases* feature.  Bare version strings
37/// collide across projects in the same org, so we namespace with the
38/// crate name.  Matches what `sentry::release_name!()` would expand to.
39pub const RELEASE_NAME: &str = concat!(env!("CARGO_PKG_NAME"), "@", env!("CARGO_PKG_VERSION"));
40
41/// Tracing target for CLI lifecycle events.  Stable so operators can
42/// filter with `RUST_LOG=studio_worker::cli=info`.
43const CLI_TRACE_TARGET: &str = "studio_worker::cli";
44
45/// Emit a single startup breadcrumb naming the agent version and the
46/// subcommand about to run.  With no `SENTRY_DSN` set (the default),
47/// nothing else anchors "which version started, running what" in
48/// `journalctl` — which matters most right after an auto-update
49/// re-execs the binary or a service manager restarts the unit.
50fn log_cli_startup(command: &cli::Command) {
51    tracing::info!(
52        target: CLI_TRACE_TARGET,
53        op = "startup",
54        version = AGENT_VERSION,
55        command = command.name(),
56        "studio-worker starting"
57    );
58}
59
60/// Dispatch table for the CLI subcommands.  Lives in the library so we
61/// can drive it from tests without invoking the binary.
62pub async fn run_cli(args: cli::Cli) -> anyhow::Result<()> {
63    log_cli_startup(&args.command);
64    match args.command {
65        cli::Command::Run => runtime::run(args.config.as_deref()).await,
66        cli::Command::Register {
67            api_base_url,
68            reset,
69        } => {
70            runtime::register(
71                args.config.as_deref(),
72                runtime::RegisterArgs {
73                    api_base_url,
74                    reset,
75                },
76            )
77            .await
78        }
79        cli::Command::Status => runtime::status(args.config.as_deref()).await,
80        cli::Command::InstallService => service::install(args.config.as_deref()),
81        cli::Command::UninstallService => service::uninstall(),
82        cli::Command::SetThreshold { gb } => runtime::set_threshold(args.config.as_deref(), gb),
83        cli::Command::Config => runtime::show_config(args.config.as_deref()),
84        cli::Command::CheckUpdate => runtime::check_update(args.config.as_deref()).await,
85        cli::Command::Ui => run_ui(args.config.as_deref()).await,
86    }
87}
88
89#[cfg(feature = "ui")]
90async fn run_ui(config_path: Option<&str>) -> anyhow::Result<()> {
91    ui::run(config_path)
92}
93
94#[cfg(not(feature = "ui"))]
95async fn run_ui(_config_path: Option<&str>) -> anyhow::Result<()> {
96    anyhow::bail!(
97        "this build of studio-worker was compiled without the `ui` cargo feature \
98         (it is on by default \u{2014} you built with `--no-default-features`).\n\
99         Reinstall with `cargo install studio-worker` (UI is the default), or use \
100         the desktop installer from the releases page, to enable the native UI."
101    )
102}
103
104#[cfg(test)]
105mod tests {
106    use super::*;
107    use crate::test_support::capture;
108
109    #[test]
110    fn startup_breadcrumb_names_version_and_command() {
111        let logs = capture(|| log_cli_startup(&cli::Command::Run));
112        assert!(logs.contains("INFO"), "expected INFO event, got: {logs}");
113        assert!(
114            logs.contains("studio_worker::cli"),
115            "expected the cli target, got: {logs}"
116        );
117        assert!(
118            logs.contains("op=\"startup\""),
119            "expected op=startup, got: {logs}"
120        );
121        assert!(
122            logs.contains("command=\"run\""),
123            "expected command field, got: {logs}"
124        );
125        assert!(
126            logs.contains(AGENT_VERSION),
127            "expected agent version, got: {logs}"
128        );
129    }
130}