Skip to content

Commit

Permalink
Add a new Server struct to create and run wacker server
Browse files Browse the repository at this point in the history
  • Loading branch information
iawia002 committed Apr 24, 2024
1 parent 0e8663a commit 49d3f84
Show file tree
Hide file tree
Showing 18 changed files with 165 additions and 167 deletions.
9 changes: 2 additions & 7 deletions Cargo.lock

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

3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ wacker = { path = "wacker", version = "0.6.0" }

anyhow = "1.0.81"
clap = { version = "4.5.4", features = ["derive"] }
log = "0.4.21"
tokio = { version = "1.36.0", features = ["rt", "rt-multi-thread", "macros"] }
tokio-stream = { version = "0.1.14", features = ["net"] }
tonic = { version = "0.11.0", features = ["zstd"] }
sled = "0.34.7"
once_cell = "1.19.0"
ahash = "0.8.11"
2 changes: 1 addition & 1 deletion wacker-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ clap.workspace = true
tokio.workspace = true
tokio-stream.workspace = true
tonic.workspace = true
once_cell.workspace = true
ahash.workspace = true

tabled = "0.15.0"
once_cell = "1.19.0"
4 changes: 2 additions & 2 deletions wacker-cli/src/commands/delete.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::{anyhow, Result};
use clap::Parser;
use tonic::transport::Channel;
use wacker::{DeleteRequest, WackerClient};
use wacker::{Client, DeleteRequest};

#[derive(Parser)]
pub struct DeleteCommand {
Expand All @@ -11,7 +11,7 @@ pub struct DeleteCommand {
}

impl DeleteCommand {
pub async fn execute(self, mut client: WackerClient<Channel>) -> Result<()> {
pub async fn execute(self, mut client: Client<Channel>) -> Result<()> {
match client.delete(DeleteRequest { id: self.id }).await {
Ok(_) => Ok(()),
Err(err) => Err(anyhow!(err.message().to_string())),
Expand Down
6 changes: 2 additions & 4 deletions wacker-cli/src/commands/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ use tabled::{
Table, Tabled,
};
use tonic::transport::Channel;
use wacker::{
WackerClient, PROGRAM_STATUS_ERROR, PROGRAM_STATUS_FINISHED, PROGRAM_STATUS_RUNNING, PROGRAM_STATUS_STOPPED,
};
use wacker::{Client, PROGRAM_STATUS_ERROR, PROGRAM_STATUS_FINISHED, PROGRAM_STATUS_RUNNING, PROGRAM_STATUS_STOPPED};

#[derive(Parser)]
pub struct ListCommand {}
Expand All @@ -36,7 +34,7 @@ static STATUS: Lazy<AHashMap<u32, &'static str>> = Lazy::new(|| {
});

impl ListCommand {
pub async fn execute(self, mut client: WackerClient<Channel>) -> Result<()> {
pub async fn execute(self, mut client: Client<Channel>) -> Result<()> {
let response = match client.list(()).await {
Ok(resp) => resp,
Err(err) => bail!(err.message().to_string()),
Expand Down
4 changes: 2 additions & 2 deletions wacker-cli/src/commands/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clap::Parser;
use std::io::{stdout, Write};
use tokio_stream::StreamExt;
use tonic::transport::Channel;
use wacker::{LogRequest, WackerClient};
use wacker::{Client, LogRequest};

#[derive(Parser)]
pub struct LogsCommand {
Expand All @@ -22,7 +22,7 @@ pub struct LogsCommand {

impl LogsCommand {
/// Executes the command.
pub async fn execute(self, mut client: WackerClient<Channel>) -> Result<()> {
pub async fn execute(self, mut client: Client<Channel>) -> Result<()> {
match client
.logs(LogRequest {
id: self.id,
Expand Down
4 changes: 2 additions & 2 deletions wacker-cli/src/commands/restart.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::{anyhow, Result};
use clap::Parser;
use tonic::transport::Channel;
use wacker::{RestartRequest, WackerClient};
use wacker::{Client, RestartRequest};

#[derive(Parser)]
pub struct RestartCommand {
Expand All @@ -11,7 +11,7 @@ pub struct RestartCommand {
}

impl RestartCommand {
pub async fn execute(self, mut client: WackerClient<Channel>) -> Result<()> {
pub async fn execute(self, mut client: Client<Channel>) -> Result<()> {
match client.restart(RestartRequest { id: self.id }).await {
Ok(_) => Ok(()),
Err(err) => Err(anyhow!(err.message().to_string())),
Expand Down
4 changes: 2 additions & 2 deletions wacker-cli/src/commands/run.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::{anyhow, Result};
use clap::Parser;
use tonic::transport::Channel;
use wacker::{RunRequest, WackerClient};
use wacker::{Client, RunRequest};

#[derive(Parser)]
pub struct RunCommand {
Expand All @@ -15,7 +15,7 @@ pub struct RunCommand {

impl RunCommand {
/// Executes the command.
pub async fn execute(self, mut client: WackerClient<Channel>) -> Result<()> {
pub async fn execute(self, mut client: Client<Channel>) -> Result<()> {
match client
.run(RunRequest {
path: self.path.to_string(),
Expand Down
4 changes: 2 additions & 2 deletions wacker-cli/src/commands/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::{anyhow, Result};
use clap::Parser;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use tonic::transport::Channel;
use wacker::{ServeRequest, WackerClient};
use wacker::{Client, ServeRequest};

const DEFAULT_ADDR: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 8080);

Expand All @@ -18,7 +18,7 @@ pub struct ServeCommand {

impl ServeCommand {
/// Executes the command.
pub async fn execute(self, mut client: WackerClient<Channel>) -> Result<()> {
pub async fn execute(self, mut client: Client<Channel>) -> Result<()> {
match client
.serve(ServeRequest {
path: self.path.to_string(),
Expand Down
4 changes: 2 additions & 2 deletions wacker-cli/src/commands/stop.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::{anyhow, Result};
use clap::Parser;
use tonic::transport::Channel;
use wacker::{StopRequest, WackerClient};
use wacker::{Client, StopRequest};

#[derive(Parser)]
pub struct StopCommand {
Expand All @@ -12,7 +12,7 @@ pub struct StopCommand {

impl StopCommand {
/// Executes the command.
pub async fn execute(self, mut client: WackerClient<Channel>) -> Result<()> {
pub async fn execute(self, mut client: Client<Channel>) -> Result<()> {
match client.stop(StopRequest { id: self.id }).await {
Ok(_) => Ok(()),
Err(err) => Err(anyhow!(err.message().to_string())),
Expand Down
7 changes: 0 additions & 7 deletions wacker-daemon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,4 @@ wacker.workspace = true

anyhow.workspace = true
clap.workspace = true
log.workspace = true
tokio = { workspace = true, features = ["signal"] }
tokio-stream.workspace = true
tonic.workspace = true
sled.workspace = true

env_logger = "0.11.1"
chrono = "0.4.34"
53 changes: 5 additions & 48 deletions wacker-daemon/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
use anyhow::{bail, Result};
use chrono::Local;
use anyhow::Result;
use clap::Parser;
use env_logger::{Builder, WriteStyle};
use log::{info, LevelFilter};
use std::fs::{create_dir_all, remove_file};
use std::io::Write;
use tokio::net::UnixListener;
use tokio::signal;
use tokio_stream::wrappers::UnixListenerStream;
use wacker::{get_db_path, get_logs_dir, get_sock_path, new_service};
use wacker::Server;

#[derive(Parser)]
#[command(name = "wackerd")]
Expand All @@ -22,47 +15,11 @@ fn version() -> &'static str {

impl WackerDaemon {
async fn execute(self) -> Result<()> {
let sock_path = get_sock_path()?;
if sock_path.exists() {
bail!("wackerd socket file exists, is wackerd already running?");
}

let logs_dir = get_logs_dir()?;
if !logs_dir.exists() {
create_dir_all(logs_dir)?;
}

let uds = UnixListener::bind(sock_path)?;
let uds_stream = UnixListenerStream::new(uds);

Builder::new()
.format(|buf, record| {
writeln!(
buf,
"[{} {} {}] {}",
Local::now().format("%Y-%m-%d %H:%M:%S"),
record.level(),
record.target(),
record.args(),
)
})
.filter_level(LevelFilter::Info)
.write_style(WriteStyle::Never)
.init();

let db = sled::open(get_db_path()?)?;

info!("server listening on {:?}", sock_path);
Ok(tonic::transport::Server::builder()
.add_service(new_service(db.clone(), logs_dir).await?)
.serve_with_incoming_shutdown(uds_stream, async {
Server::new()
.start(async {
signal::ctrl_c().await.expect("failed to listen for event");
println!();
info!("Shutting down the server");
remove_file(sock_path).expect("failed to remove existing socket file");
db.flush_async().await.expect("failed to flush the db");
})
.await?)
.await
}
}

Expand Down
7 changes: 4 additions & 3 deletions wacker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@ license.workspace = true
[dependencies]
anyhow.workspace = true
clap.workspace = true
log.workspace = true
tokio.workspace = true
tokio-stream.workspace = true
tonic.workspace = true
sled.workspace = true
once_cell.workspace = true
ahash.workspace = true

dirs = "5.0.1"
Expand All @@ -42,6 +39,10 @@ async-trait = "0.1.77"
serde = { version = "1.0.193", features = ["derive"] }
bincode = "1.3.3"
parking_lot = "0.12.1"
env_logger = "0.11.3"
chrono = "0.4.38"
sled = "0.34.7"
log = "0.4.21"

[build-dependencies]
anyhow.workspace = true
Expand Down
29 changes: 0 additions & 29 deletions wacker/src/config.rs

This file was deleted.

Loading

0 comments on commit 49d3f84

Please sign in to comment.