1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
pub mod api;
pub mod helper;
pub mod webui;
pub mod webui_ws;
pub mod worker_ws;
use anyhow::{Result, anyhow};
use api::{api_complete_json, api_loading_json, api_queue_json};
use axum::{Router, routing::get};
use log::{debug, error, info};
use serde_json::{Map, Value};
use std::{
collections::{HashMap, HashSet},
env::args,
path::Path,
sync::Arc,
time::Instant,
};
use tokio::{
fs::{File, read_to_string, rename},
io::AsyncWriteExt,
net::TcpListener,
sync::{
RwLock, broadcast,
mpsc::{self},
},
};
use webui::{webui, webui_script, webui_style};
use webui_ws::{WebuiEvent, webui_websocket};
use worker_ws::{WorkerID, WorkerResponse, worker_websocket};
pub struct Worker {
accept: usize,
name: String,
task_kinds: Vec<String>,
assigned_tasks: HashSet<String>,
send: mpsc::Sender<WorkerResponse>,
}
pub struct State {
worker_id_counter: WorkerID,
workers: HashMap<WorkerID, Worker>,
webui_broadcast: broadcast::Sender<Arc<WebuiEvent>>,
config: Value,
metadata: HashMap<String, Map<String, Value>>,
queue: HashSet<String>,
loading: HashSet<String>,
complete: HashSet<String>,
}
#[tokio::main]
async fn main() -> Result<()> {
env_logger::init_from_env("LOG");
let mut state = State::default();
state.load_config().await?;
state.load().await?;
let router = Router::new()
.route("/", get(webui))
.route("/style.css", get(webui_style))
.route("/webui_live.js", get(webui_script))
.route("/webui_ws", get(webui_websocket))
.route("/worker_ws", get(worker_websocket))
.route("/api/queue.json", get(api_queue_json))
.route("/api/complete.json", get(api_complete_json))
.route("/api/loading.json", get(api_loading_json))
.with_state(Arc::new(RwLock::new(state)));
let listener = TcpListener::bind("127.0.0.1:8080").await?;
axum::serve(listener, router).await?;
Ok(())
}
impl Default for State {
fn default() -> Self {
Self {
config: Default::default(),
worker_id_counter: Default::default(),
workers: Default::default(),
webui_broadcast: broadcast::channel(1024).0,
metadata: Default::default(),
queue: Default::default(),
loading: Default::default(),
complete: Default::default(),
}
}
}
impl State {
pub async fn load_config(&mut self) -> Result<()> {
let path = args()
.nth(1)
.ok_or(anyhow!("first argument is config path"))?;
self.config = serde_yml::from_str::<serde_json::Value>(&read_to_string(path).await?)?;
Ok(())
}
pub async fn load(&mut self) -> Result<()> {
debug!("loading state");
let t = Instant::now();
if AsRef::<Path>::as_ref("metadata.json").exists()
&& AsRef::<Path>::as_ref("queue.json").exists()
&& AsRef::<Path>::as_ref("complete.json").exists()
{
self.metadata = serde_json::from_str(&read_to_string("metadata.json").await?)?;
self.queue = serde_json::from_str(&read_to_string("queue.json").await?)?;
self.complete = serde_json::from_str(&read_to_string("complete.json").await?)?;
info!("state loaded (took {:?})", t.elapsed());
} else {
error!("some state files are missing, skipping load")
}
Ok(())
}
pub async fn save(&mut self) -> Result<()> {
let t = Instant::now();
File::create("metadata.json~")
.await?
.write_all(&serde_json::to_vec(&self.metadata)?)
.await?;
File::create("queue.json~")
.await?
.write_all(&serde_json::to_vec(&self.queue)?)
.await?;
File::create("complete.json~")
.await?
.write_all(&serde_json::to_vec(&self.complete)?)
.await?;
rename("metadata.json~", "metadata.json").await?;
rename("queue.json~", "queue.json").await?;
rename("complete.json~", "complete.json").await?;
info!("state saved (took {:?})", t.elapsed());
Ok(())
}
}
|