Compare commits
3 commits
40c61fa9ff
...
dfc0c1dc80
Author | SHA1 | Date | |
---|---|---|---|
dfc0c1dc80 | |||
77d35e8a09 | |||
c85eee85e9 |
4 changed files with 32 additions and 30 deletions
|
@ -13,9 +13,6 @@ clap = { workspace = true }
|
||||||
# fs/network
|
# fs/network
|
||||||
kxio = { workspace = true }
|
kxio = { workspace = true }
|
||||||
|
|
||||||
# Actors
|
|
||||||
actix-rt = { workspace = true }
|
|
||||||
|
|
||||||
[lints.clippy]
|
[lints.clippy]
|
||||||
nursery = { level = "warn", priority = -1 }
|
nursery = { level = "warn", priority = -1 }
|
||||||
# pedantic = "warn"
|
# pedantic = "warn"
|
||||||
|
|
|
@ -26,8 +26,7 @@ enum Server {
|
||||||
Start,
|
Start,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::main]
|
fn main() {
|
||||||
async fn main() {
|
|
||||||
let fs = fs::new(PathBuf::default());
|
let fs = fs::new(PathBuf::default());
|
||||||
let net = Network::new_real();
|
let net = Network::new_real();
|
||||||
let repo = git_next_git::repository::real();
|
let repo = git_next_git::repository::real();
|
||||||
|
@ -43,7 +42,7 @@ async fn main() {
|
||||||
}
|
}
|
||||||
Server::Start => {
|
Server::Start => {
|
||||||
let sleep_duration = std::time::Duration::from_secs(10);
|
let sleep_duration = std::time::Duration::from_secs(10);
|
||||||
git_next_server::start(fs, net, repo, sleep_duration).await;
|
git_next_server::start(fs, net, repo, sleep_duration);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,6 @@ use actix::prelude::*;
|
||||||
use actix::Recipient;
|
use actix::Recipient;
|
||||||
use inotify::{EventMask, Inotify, WatchMask};
|
use inotify::{EventMask, Inotify, WatchMask};
|
||||||
use std::{path::PathBuf, time::Duration};
|
use std::{path::PathBuf, time::Duration};
|
||||||
use tracing::{debug, info};
|
|
||||||
|
|
||||||
const CHECK_INTERVAL: Duration = Duration::from_secs(1);
|
const CHECK_INTERVAL: Duration = Duration::from_secs(1);
|
||||||
|
|
||||||
|
@ -22,7 +21,6 @@ pub enum Error {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct FileWatcher {
|
pub struct FileWatcher {
|
||||||
path: PathBuf,
|
|
||||||
inotify: Inotify,
|
inotify: Inotify,
|
||||||
recipient: Recipient<FileUpdated>,
|
recipient: Recipient<FileUpdated>,
|
||||||
run_interval: Option<SpawnHandle>,
|
run_interval: Option<SpawnHandle>,
|
||||||
|
@ -31,11 +29,10 @@ impl FileWatcher {
|
||||||
pub fn new(path: PathBuf, recipient: Recipient<FileUpdated>) -> Result<Self, Error> {
|
pub fn new(path: PathBuf, recipient: Recipient<FileUpdated>) -> Result<Self, Error> {
|
||||||
let inotify = Inotify::init()?;
|
let inotify = Inotify::init()?;
|
||||||
inotify.watches().add(
|
inotify.watches().add(
|
||||||
path.clone(),
|
path,
|
||||||
WatchMask::MODIFY | WatchMask::CREATE | WatchMask::DELETE | WatchMask::ATTRIB,
|
WatchMask::MODIFY | WatchMask::CREATE | WatchMask::DELETE | WatchMask::ATTRIB,
|
||||||
)?;
|
)?;
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
path,
|
|
||||||
inotify,
|
inotify,
|
||||||
recipient,
|
recipient,
|
||||||
run_interval: None,
|
run_interval: None,
|
||||||
|
@ -46,7 +43,7 @@ impl Actor for FileWatcher {
|
||||||
type Context = Context<Self>;
|
type Context = Context<Self>;
|
||||||
|
|
||||||
fn started(&mut self, ctx: &mut Self::Context) {
|
fn started(&mut self, ctx: &mut Self::Context) {
|
||||||
info!("Starting file watcher actor");
|
tracing::info!("Starting file watcher actor");
|
||||||
self.run_interval
|
self.run_interval
|
||||||
.replace(ctx.run_interval(CHECK_INTERVAL, |_act, ctx| {
|
.replace(ctx.run_interval(CHECK_INTERVAL, |_act, ctx| {
|
||||||
ctx.notify(WatchFile);
|
ctx.notify(WatchFile);
|
||||||
|
@ -58,11 +55,10 @@ impl Handler<WatchFile> for FileWatcher {
|
||||||
type Result = ();
|
type Result = ();
|
||||||
|
|
||||||
fn handle(&mut self, _msg: WatchFile, _ctx: &mut Self::Context) -> Self::Result {
|
fn handle(&mut self, _msg: WatchFile, _ctx: &mut Self::Context) -> Self::Result {
|
||||||
debug!("Watching {} for activity...", self.path.display());
|
|
||||||
let mut buffer = [0u8; 4096];
|
let mut buffer = [0u8; 4096];
|
||||||
if let Ok(mut events) = self.inotify.read_events(&mut buffer) {
|
if let Ok(mut events) = self.inotify.read_events(&mut buffer) {
|
||||||
if events.any(|event| event.mask.contains(EventMask::MODIFY)) {
|
if events.any(|event| event.mask.contains(EventMask::MODIFY)) {
|
||||||
info!("File modified");
|
tracing::info!("File modified");
|
||||||
self.recipient.do_send(FileUpdated);
|
self.recipient.do_send(FileUpdated);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,8 @@ use git_next_git::repository::RepositoryFactory;
|
||||||
use git_next_server_actor::Server;
|
use git_next_server_actor::Server;
|
||||||
use kxio::{fs::FileSystem, network::Network};
|
use kxio::{fs::FileSystem, network::Network};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use tracing::{error, info, level_filters::LevelFilter};
|
use tracing::{error, info};
|
||||||
|
use tracing_subscriber::EnvFilter;
|
||||||
|
|
||||||
pub fn init(fs: FileSystem) {
|
pub fn init(fs: FileSystem) {
|
||||||
let file_name = "git-next-server.toml";
|
let file_name = "git-next-server.toml";
|
||||||
|
@ -29,7 +30,7 @@ pub fn init(fs: FileSystem) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn start(
|
pub fn start(
|
||||||
fs: FileSystem,
|
fs: FileSystem,
|
||||||
net: Network,
|
net: Network,
|
||||||
repo: Box<dyn RepositoryFactory>,
|
repo: Box<dyn RepositoryFactory>,
|
||||||
|
@ -38,22 +39,31 @@ pub async fn start(
|
||||||
init_logging();
|
init_logging();
|
||||||
|
|
||||||
info!("Starting Server...");
|
info!("Starting Server...");
|
||||||
let server = Server::new(fs.clone(), net.clone(), repo, sleep_duration).start();
|
let execution = async move {
|
||||||
server.do_send(FileUpdated);
|
//-
|
||||||
|
let server = Server::new(fs.clone(), net.clone(), repo, sleep_duration).start();
|
||||||
|
server.do_send(FileUpdated);
|
||||||
|
|
||||||
info!("Starting File Watcher...");
|
info!("Starting File Watcher...");
|
||||||
let fw = match FileWatcher::new("git-next-server.toml".into(), server.recipient()) {
|
let fw = match FileWatcher::new("git-next-server.toml".into(), server.recipient()) {
|
||||||
Ok(fw) => fw,
|
Ok(fw) => fw,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!(?err, "Failed to start file watcher");
|
error!(?err, "Failed to start file watcher");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
fw.start();
|
||||||
|
|
||||||
|
info!("Server running - Press Ctrl-C to stop...");
|
||||||
|
let _ = actix_rt::signal::ctrl_c().await;
|
||||||
|
info!("Ctrl-C received, shutting down...");
|
||||||
|
System::current().stop();
|
||||||
|
};
|
||||||
|
let system = System::new();
|
||||||
|
Arbiter::current().spawn(execution);
|
||||||
|
if let Err(err) = system.run() {
|
||||||
|
tracing::error!(?err, "")
|
||||||
};
|
};
|
||||||
fw.start();
|
|
||||||
|
|
||||||
info!("Server running - Press Ctrl-C to stop...");
|
|
||||||
let _ = actix_rt::signal::ctrl_c().await;
|
|
||||||
info!("Ctrl-C received, shutting down...");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init_logging() {
|
pub fn init_logging() {
|
||||||
|
@ -64,7 +74,7 @@ pub fn init_logging() {
|
||||||
.with_target(false)
|
.with_target(false)
|
||||||
.with_file(true)
|
.with_file(true)
|
||||||
.with_line_number(true)
|
.with_line_number(true)
|
||||||
.with_filter(LevelFilter::INFO);
|
.with_filter(EnvFilter::from_default_env());
|
||||||
tracing_subscriber::registry()
|
tracing_subscriber::registry()
|
||||||
.with(console_subscriber::ConsoleLayer::builder().spawn())
|
.with(console_subscriber::ConsoleLayer::builder().spawn())
|
||||||
.with(subscriber)
|
.with(subscriber)
|
||||||
|
|
Loading…
Reference in a new issue