servicer-labs / servicer

A CLI to simplify service management on systemd
https://servicer.dev
MIT License
427 stars 9 forks source link

ExecStart command FileNotFound #1

Closed christiankozalla closed 1 year ago

christiankozalla commented 1 year ago

I wanted to try servicer on a Deno app and faced mainly two issues.

Steps to reproduce

First issue: Could not find Deno

thread 'main' panicked at 'Could not find executable for deno: CannotFindBinaryPath', src/handlers/handle_create_service.rs:169:18
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

As a workarount I used

ExecStart command fails with subcommand and flags

Unfortunately the command to start the Deno app is not deno main.ts (as in node index.js)

The correct command to start the app would be

So I used sudo ser edit deno-backend and changed the ExecStart command ExecStart=/path/to/deno run -A --unstable main.ts

But this gave the following error after running sudo ser start deno-backend

service started: /org/freedesktop/systemd1/job/195454
thread 'tokio-runtime-worker' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "No such file or directory" }', src/handlers/handle_show_status.rs:144:48
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: JoinError::Panic(Id(31), ...)', src/handlers/handle_show_status.rs:151:6

Happy to provide more information if needed!

secretshardul commented 1 year ago

First issue: Could not find Deno

Acknowledged. The problem is that ser create runs in sudo but deno does not show up in the path. I need sudo to write a service file in /etc/systemd/system.

which deno # works

sudo su
which deno # nothing

# This works- need to implement this in rust
sudo -u hp bash -i -c "which deno"

Fix on the way.

For complex commands like deno run -A --unstable main.ts, it'll be better to have something like ser create "deno run -A --unstable main.ts". Currently can pass flags using sudo ser create main.js --interpreter deno -- -A --unstable but compound paths deno run are not supported yet.

ExecStart command fails with subcommand and flags

`Err` value: Os { code: 2, kind: NotFound, message: "No such file or directory" }', src/handlers/handle_show_status.rs:144:48

Strange, it's unable to read the proc file of an active service. Did your service start after running that command? Does cat /proc/1/stat print a set of numbers?

pub async fn get_cpu_times(service_statuses: Vec<ServiceStatus>) -> Vec<u64> {
    futures::future::try_join_all(service_statuses.into_iter().map(|status| {
        tokio::spawn(async move {
            if status.active == "active" {
                get_cpu_time(status.pid).await.unwrap()
            } else {
                0
            }
        })
    }))
    .await
    .unwrap()
}
pub async fn get_cpu_time(pid: u32) -> Result<u64, Box<dyn std::error::Error>> {
    let stat_path = format!("/proc/{}/stat", pid);
    let stat_content = tokio::fs::read_to_string(stat_path).await?;
    let stat_fields: Vec<&str> = stat_content.split_whitespace().collect();

    // The 14th field in /proc/<pid>/stat represents utime (user mode CPU time) in clock ticks
    // The 15th field represents stime (kernel mode CPU time) in clock ticks
    let utime: u64 = stat_fields[13].parse()?;
    let stime: u64 = stat_fields[14].parse()?;

    Ok(utime + stime)
}
christiankozalla commented 1 year ago

I retried the second issue "ExecStart command fails with subcommand and flags" This time on my local machine (Ubuntu 22.04), before was on a VPS (also Ubuntu 22.04).. Now I go through the steps again.

  1. sudo ser create main.ts --name deno-backend --interpreter "$(which deno)"
  2. sudo ser edit deno-backend
  3. Change ExecStart to ExecStart=/home/christian/.deno/bin/deno run -A --unstable main.ts
  4. sudo ser start deno-backend

And it works! It prints the table that lists the service as active. cat /proc/1/stat prints a set of number.

secretshardul commented 1 year ago

@christiankozalla deno should now get automatically detected with https://github.com/servicer-labs/servicer/commit/0a60956c447e64b2824a7a48e59be87715f95c55. No need to pass which deno from the CLI.