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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use crate::{
    config, file, helpers,
    service::{run, stop, ProcessMetadata},
};

use chrono::serde::ts_milliseconds;
use chrono::{DateTime, Utc};
use global_placeholders::global;
use macros_rs::{clone, crashln, string, then};
use psutil::process::{MemoryInfo, Process as PsutilProcess};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::collections::{BTreeMap, HashMap};
use std::{env, path::PathBuf};

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Process {
    pub id: usize,
    pub pid: i64,
    pub name: String,
    pub path: PathBuf,
    pub script: String,
    pub env: HashMap<String, String>,
    #[serde(with = "ts_milliseconds")]
    pub started: DateTime<Utc>,
    pub restarts: u64,
    pub running: bool,
    pub crash: Crash,
    pub watch: Watch,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Crash {
    pub crashed: bool,
    pub value: u64,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Watch {
    pub enabled: bool,
    pub path: String,
    pub hash: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Runner {
    pub id: id::Id,
    pub list: BTreeMap<usize, Process>,
}

pub enum Status {
    Offline,
    Running,
}

impl Status {
    pub fn to_bool(&self) -> bool {
        match self {
            Status::Offline => false,
            Status::Running => true,
        }
    }
}

impl Runner {
    pub fn new() -> Self {
        let dump = dump::read();
        let runner = Runner { id: dump.id, list: dump.list };

        dump::write(&runner);
        return runner;
    }

    pub fn start(&mut self, name: &String, command: &String, watch: &Option<String>) -> &mut Self {
        let id = self.id.next();
        let config = config::read().runner;
        let crash = Crash { crashed: false, value: 0 };

        let watch = match watch {
            Some(watch) => Watch {
                enabled: true,
                path: string!(watch),
                hash: hash::create(file::cwd().join(watch)),
            },
            None => {
                Watch {
                    enabled: false,
                    path: string!(""),
                    hash: string!(""),
                }
            }
        };

        let pid = run(ProcessMetadata {
            args: config.args,
            name: name.clone(),
            shell: config.shell,
            command: command.clone(),
            log_path: config.log_path,
        });

        self.list.insert(
            id,
            Process {
                id,
                pid,
                watch,
                crash,
                restarts: 0,
                running: true,
                path: file::cwd(),
                name: name.clone(),
                started: Utc::now(),
                script: command.clone(),
                env: env::vars().collect(),
            },
        );
        dump::write(&self);

        return self;
    }

    pub fn restart(&mut self, id: usize, name: String, dead: bool) -> &mut Self {
        let item = self.get(id);
        let Process { path, script, .. } = item.clone();

        if let Err(err) = std::env::set_current_dir(&item.path) {
            crashln!("{} Failed to set working directory {:?}\nError: {:#?}", *helpers::FAIL, path, err);
        };

        item.stop();

        let config = config::read().runner;

        item.crash.crashed = false;
        item.pid = run(ProcessMetadata {
            command: script,
            args: config.args,
            name: name.clone(),
            shell: config.shell,
            log_path: config.log_path,
        });

        item.watch = Watch {
            enabled: false,
            path: string!(""),
            hash: string!(""),
        };

        item.name = name;
        item.running = true;
        item.started = Utc::now();
        then!(dead, item.restarts += 1);

        // assign!(item, {name, pid, watch});

        return self;
    }

    pub fn remove(&mut self, id: usize) {
        self.stop(id);
        self.list.remove(&id);
        dump::write(&self);
    }

    pub fn set_id(&mut self, id: id::Id) {
        self.id = id;
        self.id.next();
        dump::write(&self);
    }

    pub fn set_status(&mut self, id: usize, status: Status) {
        let item = self.get(id);
        item.running = status.to_bool();
        dump::write(&self);
    }

    pub fn save(&self) { dump::write(&self); }
    pub fn count(&mut self) -> usize { self.list().count() }
    pub fn is_empty(&self) -> bool { self.list.is_empty() }
    pub fn items(&mut self) -> &mut BTreeMap<usize, Process> { &mut self.list }
    pub fn info(&mut self, id: usize) -> Option<&Process> { self.list.get(&id) }
    pub fn list<'a>(&'a mut self) -> impl Iterator<Item = (&'a usize, &'a mut Process)> { self.list.iter_mut().map(|(k, v)| (k, v)) }
    pub fn get(&mut self, id: usize) -> &mut Process { self.list.get_mut(&id).unwrap_or_else(|| crashln!("{} Process ({id}) not found", *helpers::FAIL)) }

    pub fn set_crashed(&mut self, id: usize) -> &mut Self {
        let item = self.get(id);
        item.crash.crashed = true;
        return self;
    }

    pub fn new_crash(&mut self, id: usize) -> &mut Self {
        let item = self.get(id);
        item.crash.value += 1;
        return self;
    }

    pub fn stop(&mut self, id: usize) -> &mut Self {
        let item = self.get(id);
        stop(item.pid);
        item.running = false;
        item.crash.crashed = false;
        item.crash.value = 0;
        return self;
    }

    pub fn rename(&mut self, id: usize, name: String) -> &mut Self {
        let item = self.get(id);
        item.name = name;
        return self;
    }

    pub fn watch(&mut self, id: usize, path: String) -> &mut Self {
        let item = self.get(id);
        item.watch = Watch {
            enabled: true,
            path: clone!(path),
            hash: hash::create(item.path.join(path)),
        };

        return self;
    }

    pub fn json(&mut self) -> Value {
        let mut processes: Vec<ProcessItem> = Vec::new();

        #[derive(Serialize)]
        struct ProcessItem {
            pid: i64,
            id: usize,
            cpu: String,
            mem: String,
            name: String,
            restarts: u64,
            status: String,
            uptime: String,
            watch_path: String,
            start_time: DateTime<Utc>,
        }

        for (id, item) in self.items() {
            let mut memory_usage: Option<MemoryInfo> = None;
            let mut cpu_percent: Option<f32> = None;

            if let Ok(mut process) = PsutilProcess::new(item.pid as u32) {
                memory_usage = process.memory_info().ok();
                cpu_percent = process.cpu_percent().ok();
            }

            let cpu_percent = match cpu_percent {
                Some(percent) => format!("{:.2}%", percent),
                None => string!("0.00%"),
            };

            let memory_usage = match memory_usage {
                Some(usage) => helpers::format_memory(usage.rss()),
                None => string!("0b"),
            };

            let status =
                if item.running {
                    string!("online")
                } else {
                    match item.crash.crashed {
                        true => string!("crashed"),
                        false => string!("stopped"),
                    }
                };

            processes.push(ProcessItem {
                status,
                id: *id,
                pid: item.pid,
                cpu: cpu_percent,
                mem: memory_usage,
                restarts: item.restarts,
                name: item.name.clone(),
                start_time: item.started,
                watch_path: item.watch.path.clone(),
                uptime: helpers::format_duration(item.started),
            });
        }

        json!(processes)
    }
}

impl Process {
    pub fn stop(&mut self) { Runner::new().stop(self.id).save(); }
    pub fn watch(&mut self, path: String) { Runner::new().watch(self.id, path).save(); }
    pub fn rename(&mut self, name: String) { Runner::new().rename(self.id, name).save(); }

    pub fn restart(&mut self) -> &mut Process {
        Runner::new().restart(self.id, clone!(self.name), false).save();
        return self;
    }

    pub fn crashed(&mut self) -> &mut Process {
        Runner::new().new_crash(self.id).save();
        Runner::new().restart(self.id, clone!(self.name), true).save();
        return self;
    }

    pub fn json(&mut self) -> Value {
        let config = config::read().runner;

        #[derive(Serialize)]
        struct Item {
            info: Info,
            stats: Stats,
            watch: Watch,
            log: Log,
            raw: Raw,
        }

        #[derive(Serialize)]
        struct Info {
            id: usize,
            pid: i64,
            name: String,
            status: String,
            path: PathBuf,
            uptime: String,
            command: String,
        }

        #[derive(Serialize)]
        struct Stats {
            restarts: u64,
            start_time: i64,
            cpu_percent: Option<f32>,
            memory_usage: Option<MemoryInfo>,
        }

        #[derive(Serialize)]
        struct Watch {
            enabled: bool,
            hash: String,
            path: String,
        }

        #[derive(Serialize)]
        struct Log {
            out: String,
            error: String,
        }

        #[derive(Serialize)]
        struct Raw {
            running: bool,
            crashed: bool,
            crashes: u64,
        }

        let mut memory_usage: Option<MemoryInfo> = None;
        let mut cpu_percent: Option<f32> = None;

        if let Ok(mut process) = PsutilProcess::new(self.pid as u32) {
            memory_usage = process.memory_info().ok();
            cpu_percent = process.cpu_percent().ok();
        }

        let status = if self.running {
            string!("online")
        } else {
            match self.crash.crashed {
                true => string!("crashed"),
                false => string!("stopped"),
            }
        };

        json!(Item {
            info: Info {
                status,
                id: self.id,
                pid: self.pid,
                name: self.name.clone(),
                path: self.path.clone(),
                uptime: helpers::format_duration(self.started),
                command: format!("{} {} '{}'", config.shell, config.args.join(" "), self.script.clone()),
            },
            stats: Stats {
                cpu_percent,
                memory_usage,
                restarts: self.restarts,
                start_time: self.started.timestamp_millis(),
            },
            watch: Watch {
                enabled: self.watch.enabled,
                hash: self.watch.hash.clone(),
                path: self.watch.path.clone(),
            },
            log: Log {
                out: global!("pmc.logs.out", self.name.as_str()),
                error: global!("pmc.logs.error", self.name.as_str()),
            },
            raw: Raw {
                running: self.running,
                crashed: self.crash.crashed,
                crashes: self.crash.value,
            }
        })
    }
}

pub mod dump;
pub mod hash;
pub mod id;