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

feat: add flox support and show more examples #23

Merged
merged 17 commits into from
Mar 28, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
fix(webui): show loading on start/stop services
tsirysndr committed Mar 28, 2023
commit 6c96b1a12b6ae9ece6520d451993ae2a04694f89
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -190,5 +190,8 @@ You can use the [setup-superviseur](https://github.com/marketplace/actions/setup
version: 'v0.1.0-alpha.4'
- run: superviseur --help
```
## 📖 Examples
See the [examples](examples) directory for more examples.
## 📝 License
[MPL](LICENSE)
7 changes: 0 additions & 7 deletions src/server/control.rs
Original file line number Diff line number Diff line change
@@ -21,13 +21,6 @@ use crate::{
StatusRequest, StatusResponse, StopRequest, StopResponse,
},
},
graphql::{
self,
schema::objects::subscriptions::{
AllServicesRestarted, AllServicesStarted, AllServicesStopped,
},
simple_broker::SimpleBroker,
},
superviseur::core::{ProcessEvent, Superviseur, SuperviseurCommand},
types::{
self,
237 changes: 1 addition & 236 deletions src/superviseur/core.rs
Original file line number Diff line number Diff line change
@@ -9,10 +9,6 @@ use std::{

use anyhow::{anyhow, Error, Ok};
use futures::Future;
use nix::{
sys::signal::{self, Signal},
unistd::Pid,
};
use tokio::sync::mpsc;

use crate::{
@@ -32,7 +28,7 @@ use crate::{
},
};

use super::{dependencies::DependencyGraph, wait::wait_for_service, watch::WatchForChanges};
use super::{dependencies::DependencyGraph, watch::WatchForChanges};

#[derive(Clone)]
pub struct Superviseur {}
@@ -239,157 +235,6 @@ impl SuperviseurInternal {
}

fn handle_start(&mut self, service: Service, project: String) -> Result<(), Error> {
/*
println!("starting {}", service.clone().name);
self.event_tx
.send(ProcessEvent::Starting(
service.name.clone(),
project.clone(),
))
.unwrap();

self.start_dependencies(service.clone(), project.clone())?;
self.wait_for_service_deps(service.clone(), project.clone())?;

// skip if already started
let mut processes = self.processes.lock().unwrap();
if let Some(process) = processes
.iter()
.find(|(p, key)| p.name == service.name && key == &project)
.map(|(p, _)| p)
{
if process.state == State::Running || process.state == State::Starting {
return Ok(());
}
}

let envs = service.env.clone();
let working_dir = service.working_dir.clone();

let mut child = match service.clone().flox {
Some(flox) => {
// verify if flox is installed
std::process::Command::new("sh")
.arg("-c")
.arg("flox --version")
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.spawn()
.expect("flox is not installed, see https://floxdev.com/docs/");

let command = format!(
"flox print-dev-env -A {}",
flox.environment.replace(".#", "")
);
let mut child = std::process::Command::new("sh")
.arg("-c")
.arg(command)
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.spawn()
.unwrap();
child.wait().unwrap();

let command = format!(
"flox activate -e {} -- {}",
flox.environment, &service.command
);
println!("command: {}", command);
std::process::Command::new("sh")
.arg("-c")
.arg(command)
.current_dir(working_dir)
.envs(envs)
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.spawn()
.unwrap()
}
None => std::process::Command::new("sh")
.arg("-c")
.arg(&service.command)
.current_dir(working_dir)
.envs(envs)
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.spawn()
.unwrap(),
};

let mut process = &mut processes
.iter_mut()
.find(|(p, key)| p.name == service.name && key == &project)
.unwrap()
.0;
process.pid = Some(child.id());
self.event_tx
.send(ProcessEvent::Started(service.name.clone(), project.clone()))
.unwrap();
println!("started {}", service.clone().name);

process.up_time = Some(chrono::Utc::now());
let service_key = format!("{}-{}", project, service.name);
self.childs
.lock()
.unwrap()
.insert(service_key, process.pid.unwrap() as i32);

let stdout = child.stdout.take().unwrap();
let stderr = child.stderr.take().unwrap();

let cloned_service = service.clone();

thread::spawn(move || {
let service = cloned_service;
let id = service.id.unwrap_or("-".to_string());
// write stdout to file
let mut log_file = std::fs::File::create(service.stdout).unwrap();

let stdout = std::io::BufReader::new(stdout);
for line in stdout.lines() {
let line = line.unwrap();
let line = format!("{}\n", line);
SimpleBroker::publish(TailLogStream {
id: id.clone(),
line: line.clone(),
});
SimpleBroker::publish(LogStream {
id: id.clone(),
line: line.clone(),
});
log_file.write_all(line.as_bytes()).unwrap();
}

// write stderr to file
let mut err_file = std::fs::File::create(service.stderr).unwrap();
let stderr = std::io::BufReader::new(stderr);
for line in stderr.lines() {
let line = line.unwrap();
err_file.write_all(line.as_bytes()).unwrap();
}
});

let cmd_tx = self.cmd_tx.clone();
let event_tx = self.event_tx.clone();
thread::spawn(move || {
let _status = child.wait().unwrap();
// println!("child exited with status: {}", status);
if service.autorestart {
cmd_tx
.send(SuperviseurCommand::Start(service.clone(), project.clone()))
.unwrap();

event_tx
.send(ProcessEvent::Restarted(service.name, project))
.unwrap();
return;
}
event_tx
.send(ProcessEvent::Stopped(service.name, project))
.unwrap();
});
*/

self.event_tx
.send(ProcessEvent::Starting(
service.name.clone(),
@@ -411,85 +256,6 @@ impl SuperviseurInternal {
}

fn handle_stop(&self, service: Service, project: String) -> Result<(), Error> {
/*
self.event_tx
.send(ProcessEvent::Stopping(
service.name.clone(),
project.clone(),
))
.unwrap();
println!(
"service: {} | stop_command: {:?}",
service.name, service.stop_command
);
if let Some(stop_command) = service.stop_command.clone() {
let envs = service.env.clone();
let working_dir = service.working_dir.clone();

match service.clone().flox {
Some(flox) => {
let stop_command =
format!("flox activate -e {} -- {}", flox.environment, stop_command);
let mut child = std::process::Command::new("sh")
.arg("-c")
.arg(stop_command)
.current_dir(working_dir)
.envs(envs)
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.spawn()
.unwrap();
child.wait().unwrap();
}
None => {
let mut child = std::process::Command::new("sh")
.arg("-c")
.arg(stop_command)
.current_dir(working_dir)
.envs(envs)
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.spawn()
.unwrap();
child.wait().unwrap();
}
};
let mut childs = self.childs.lock().unwrap();
let service_key = format!("{}-{}", project.clone(), service.name.clone());
childs.remove(&service_key);

self.event_tx
.send(ProcessEvent::Stopped(service.name.clone(), project.clone()))
.unwrap();
if restart {
self.cmd_tx
.send(SuperviseurCommand::Start(service, project))
.unwrap();
}
return Ok(());
}
let mut childs = self.childs.lock().unwrap();
let service_key = format!("{}-{}", project.clone(), service.name.clone());
match childs.get(&service_key) {
Some(pid) => {
println!("Stopping service {} (pid: {})", service.name.clone(), pid);
signal::kill(Pid::from_raw(*pid), Signal::SIGTERM)?;
childs.remove(&service_key);

self.event_tx
.send(ProcessEvent::Stopped(service.name.clone(), project.clone()))
.unwrap();
if restart {
self.cmd_tx
.send(SuperviseurCommand::Start(service.clone(), project))
.unwrap();
}
println!("Service {} stopped", service.name);
Ok(())
}
None => Ok(()),
}
*/
self.event_tx
.send(ProcessEvent::Stopping(
service.name.clone(),
@@ -511,7 +277,6 @@ impl SuperviseurInternal {
}

fn handle_restart(&mut self, service: Service, project: String) -> Result<(), Error> {
// self.handle_stop(service.clone(), project.clone(), true)
self.handle_stop(service.clone(), project.clone())?;
self.handle_start(service.clone(), project.clone())?;
Ok(())
6 changes: 3 additions & 3 deletions webui/build/asset-manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"files": {
"main.css": "/static/css/main.61403641.css",
"main.js": "/static/js/main.c8850221.js",
"main.js": "/static/js/main.905ef365.js",
"static/js/787.cb4325f6.chunk.js": "/static/js/787.cb4325f6.chunk.js",
"static/media/ubuntu-all-400-normal.woff": "/static/media/ubuntu-all-400-normal.8629f83ad2c45e75a914.woff",
"static/media/RockfordSans-ExtraBold.otf": "/static/media/RockfordSans-ExtraBold.1513e8fd97078bfb7708.otf",
@@ -17,11 +17,11 @@
"static/media/ubuntu-greek-ext-400-normal.woff2": "/static/media/ubuntu-greek-ext-400-normal.7f5049065c02fb5e0628.woff2",
"index.html": "/index.html",
"main.61403641.css.map": "/static/css/main.61403641.css.map",
"main.c8850221.js.map": "/static/js/main.c8850221.js.map",
"main.905ef365.js.map": "/static/js/main.905ef365.js.map",
"787.cb4325f6.chunk.js.map": "/static/js/787.cb4325f6.chunk.js.map"
},
"entrypoints": [
"static/css/main.61403641.css",
"static/js/main.c8850221.js"
"static/js/main.905ef365.js"
]
}
2 changes: 1 addition & 1 deletion webui/build/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/><title>Superviseur Dashboard</title><script defer="defer" src="/static/js/main.c8850221.js"></script><link href="/static/css/main.61403641.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/><title>Superviseur Dashboard</title><script defer="defer" src="/static/js/main.905ef365.js"></script><link href="/static/css/main.61403641.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -9,6 +9,8 @@ export default {
onStop: { action: "onStop" },
onRestart: { action: "onRestart" },
allServicesAreRunning: { control: "boolean" },
starting: { control: "boolean" },
stopping: { control: "boolean" },
},
} as ComponentMeta<typeof Actions>;

@@ -20,4 +22,6 @@ export const Default = Template.bind({});

Default.args = {
allServicesAreRunning: true,
starting: false,
stopping: false,
};
Loading