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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
//! Supervisor manages a collection of worker processes.
use std::{
    collections::{hash_map::DefaultHasher, HashMap},
    hash::Hasher,
    io,
    path::{Path, PathBuf},
    sync::Arc,
    time::Duration,
};

use tokio::{
    net::{UnixListener, UnixStream},
    process::Command,
    sync::oneshot::{self, Sender},
    sync::{mpsc, Mutex},
    time,
};

use log::{error, info, warn};
use once_cell::sync::OnceCell;
use rand::Rng;

use super::{Result, SOCKET, WORKER_ID};

type IpcHandler = Box<dyn Fn(UnixStream, mpsc::Sender<Message>) + Send + Sync>;

/// Get the supervisor state.
fn supervisor_state() -> &'static Mutex<SupervisorState> {
    static INSTANCE: OnceCell<Mutex<SupervisorState>> = OnceCell::new();
    INSTANCE.get_or_init(|| Mutex::new(SupervisorState { workers: vec![] }))
}

/// Control messages sent by the server handler to the
/// supervisor.
pub enum Message {
    /// Shutdown a worker process using it's opaque identifier.
    ///
    /// If the worker is a daemon it will *not be restarted*.
    Shutdown {
        /// Opaque identifier for the worker.
        id: String,
    },

    /// Spawn a new worker process.
    Spawn {
        /// Task definition for the new process.
        task: Task,
    },
}

/// Defines a worker process command.
#[derive(Debug, Clone)]
pub struct Task {
    cmd: String,
    args: Vec<String>,
    envs: HashMap<String, String>,
    daemon: bool,
    detached: bool,
    limit: usize,
    factor: usize,
}

impl Task {
    /// Create a new task.
    pub fn new(cmd: &str) -> Self {
        Self {
            cmd: cmd.to_string(),
            args: Vec::new(),
            envs: HashMap::new(),
            daemon: false,
            detached: false,
            limit: 5,
            factor: 0,
        }
    }

    /// Set command arguments.
    pub fn args<I, S>(mut self, args: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        let args = args
            .into_iter()
            .map(|s| s.as_ref().to_string())
            .collect::<Vec<_>>();
        self.args = args;
        self
    }

    /// Set command environment variables.
    pub fn envs<I, K, V>(mut self, vars: I) -> Self
    where
        I: IntoIterator<Item = (K, V)>,
        K: AsRef<str>,
        V: AsRef<str>,
    {
        let envs = vars
            .into_iter()
            .map(|(k, v)| (k.as_ref().to_string(), v.as_ref().to_string()))
            .collect::<HashMap<_, _>>();
        self.envs = envs;
        self
    }

    /// Set the daemon flag for the worker command.
    ///
    /// Daemon processes are restarted if they die without being explicitly
    /// shutdown by the supervisor.
    pub fn daemon(mut self, flag: bool) -> Self {
        self.daemon = flag;
        self
    }

    /// Set the detached flag for the worker command.
    ///
    /// If a worker is detached it will not connect to the IPC socket.
    pub fn detached(mut self, flag: bool) -> Self {
        self.detached = flag;
        self
    }

    /// Set the retry limit when restarting dead workers.
    ///
    /// Only applies to tasks that have the `daemon` flag set;
    /// non-daemon tasks are not restarted. If this value is
    /// set to zero then it overrides the `daemon` flag and no
    /// attempts to restart the process are made.
    ///
    /// The default value is `5`.
    pub fn retry_limit(mut self, limit: usize) -> Self {
        self.limit = limit;
        self
    }

    /// Set the retry factor in milliseconds.
    ///
    /// The default value is `0` which means retry attempts
    /// are performed immediately.
    pub fn retry_factor(mut self, factor: usize) -> Self {
        self.factor = factor;
        self
    }

    /// Get a retry state for this task.
    fn retry(&self) -> Retry {
        Retry {
            limit: self.limit,
            factor: self.factor,
            attempts: 0,
        }
    }
}

#[derive(Clone, Copy)]
struct Retry {
    /// The limit on the number of times to attempt
    /// to restart a process.
    limit: usize,
    /// The retry delay factor in milliseconds.
    factor: usize,
    /// The current number of attempts.
    attempts: usize,
}

/// Build a supervisor.
pub struct SupervisorBuilder {
    socket: PathBuf,
    commands: Vec<Task>,
    ipc_handler: Option<IpcHandler>,
    shutdown: Option<oneshot::Receiver<()>>,
}

impl SupervisorBuilder {
    /// Create a new supervisor builder.
    pub fn new() -> Self {
        let socket = std::env::temp_dir().join("psup.sock");
        Self {
            socket,
            commands: Vec::new(),
            ipc_handler: None,
            shutdown: None,
        }
    }

    /// Set the IPC server handler.
    pub fn server<F: 'static>(mut self, handler: F) -> Self
    where
        F: Fn(UnixStream, mpsc::Sender<Message>) + Send + Sync,
    {
        self.ipc_handler = Some(Box::new(handler));
        self
    }

    /// Set the socket path.
    pub fn path<P: AsRef<Path>>(mut self, path: P) -> Self {
        self.socket = path.as_ref().to_path_buf();
        self
    }

    /// Add a worker process.
    pub fn add_worker(mut self, task: Task) -> Self {
        self.commands.push(task);
        self
    }

    /// Register a shutdown handler with the supervisor.
    ///
    /// When a message is received on the shutdown receiver all
    /// managed processes are killed.
    pub fn shutdown(mut self, rx: oneshot::Receiver<()>) -> Self {
        self.shutdown = Some(rx);
        self
    }

    /// Return the supervisor.
    pub fn build(self) -> Supervisor {
        Supervisor {
            socket: self.socket,
            commands: self.commands,
            ipc_handler: self.ipc_handler.map(Arc::new),
            shutdown: self.shutdown,
        }
    }
}

/// Supervisor manages long-running worker processes.
pub struct Supervisor {
    socket: PathBuf,
    commands: Vec<Task>,
    ipc_handler: Option<Arc<IpcHandler>>,
    shutdown: Option<oneshot::Receiver<()>>,
}

impl Supervisor {
    /// Start the supervisor running.
    ///
    /// Listens on the socket path and starts any initial workers.
    pub async fn run(&mut self) -> Result<()> {
        // Set up the server listener and control channel.
        if let Some(ref ipc_handler) = self.ipc_handler {
            let socket = self.socket.clone();
            let control_socket = self.socket.clone();

            let (control_tx, mut control_rx) = mpsc::channel::<Message>(1024);
            let (tx, rx) = oneshot::channel::<()>();
            let handler = Arc::clone(ipc_handler);

            // Handle global shutdown signal, kills all the workers
            if let Some(shutdown) = self.shutdown.take() {
                tokio::spawn(async move {
                    let _ = shutdown.await;
                    let mut state = supervisor_state().lock().await;
                    let workers = state.workers.drain(..);
                    for worker in workers {
                        let tx = worker.shutdown.clone();
                        let _ = tx.send(worker).await;
                    }
                });
            }

            tokio::spawn(async move {
                while let Some(msg) = control_rx.recv().await {
                    match msg {
                        Message::Shutdown { id } => {
                            let mut state = supervisor_state().lock().await;
                            let mut worker = state.remove(&id);
                            drop(state);
                            if let Some(worker) = worker.take() {
                                let tx = worker.shutdown.clone();
                                let _ = tx.send(worker).await;
                            } else {
                                warn!("Could not find worker to shutdown with id: {}", id);
                            }
                        }
                        Message::Spawn { task } => {
                            // FIXME: return the id to the caller?
                            let id = id();
                            let retry = task.retry();
                            spawn_worker(
                                id,
                                task,
                                control_socket.clone(),
                                retry,
                            );
                        }
                    }
                }
            });

            tokio::spawn(async move {
                listen(&socket, tx, handler, control_tx)
                    .await
                    .expect("Supervisor failed to bind to socket");
            });

            let _ = rx.await?;
            info!("Supervisor is listening {}", self.socket.display());
        }

        // Spawn initial worker processes.
        for task in self.commands.iter() {
            self.spawn(task.clone());
        }

        Ok(())
    }

    /// Spawn a worker task.
    pub fn spawn(&self, task: Task) -> String {
        let id = id();
        let retry = task.retry();
        spawn_worker(id.clone(), task, self.socket.clone(), retry);
        id
    }

    /*
    /// Get the workers mapped from opaque identifier to process PID.
    pub fn workers() -> HashMap<String, u32> {
        let state = supervisor_state().lock().unwrap();
        state.workers.iter()
            .map(|w| (w.id.clone(), w.pid))
            .collect::<HashMap<_, _>>()
    }
    */
}

/// State of the supervisor worker processes.
struct SupervisorState {
    workers: Vec<WorkerState>,
}

impl SupervisorState {
    fn remove(&mut self, id: &str) -> Option<WorkerState> {
        let res = self.workers.iter().enumerate().find_map(|(i, w)| {
            if &w.id == id {
                Some(i)
            } else {
                None
            }
        });
        if let Some(position) = res {
            Some(self.workers.swap_remove(position))
        } else {
            None
        }
    }
}

#[derive(Debug)]
struct WorkerState {
    task: Task,
    id: String,
    socket: PathBuf,
    pid: Option<u32>,
    /// If we are shutting down this worker explicitly
    /// this flag will be set to prevent the worker from
    /// being re-spawned.
    reap: bool,
    shutdown: mpsc::Sender<WorkerState>,
}

impl PartialEq for WorkerState {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id && self.pid == other.pid
    }
}

impl Eq for WorkerState {}

/// Attempt to restart a worker that died.
async fn restart(worker: WorkerState, mut retry: Retry) {
    info!("Restarting worker {}", worker.id);
    retry.attempts = retry.attempts + 1;

    if retry.attempts >= retry.limit {
        error!(
            "Failed to restart worker {}, exceeded retry limit {}",
            worker.id, retry.limit
        );
    } else {
        if retry.factor > 0 {
            let ms = retry.attempts * retry.factor;
            info!("Delay restart {}ms", ms);
            time::sleep(Duration::from_millis(ms as u64)).await;
        }
        spawn_worker(worker.id, worker.task, worker.socket, retry)
    }
}

/// Generate a random opaque identifier.
pub fn id() -> String {
    let mut rng = rand::thread_rng();
    let mut hasher = DefaultHasher::new();
    hasher.write_usize(rng.gen());
    format!("{:x}", hasher.finish())
}

fn spawn_worker(id: String, task: Task, socket: PathBuf, retry: Retry) {
    tokio::task::spawn(async move {
        // Setup built in environment variables
        let mut envs = task.envs.clone();
        envs.insert(WORKER_ID.to_string(), id.clone());
        if !task.detached {
            envs.insert(
                SOCKET.to_string(),
                socket.to_string_lossy().into_owned(),
            );
        }

        let (shutdown_tx, mut shutdown_rx) = mpsc::channel::<WorkerState>(1);

        info!("Spawn worker {} {}", &task.cmd, task.args.join(" "));

        let mut child = Command::new(task.cmd.clone())
            .args(task.args.clone())
            .envs(envs)
            .spawn()?;

        let pid = child.id();

        if let Some(ref id) = pid {
            info!("Worker pid {}", id);
        }

        {
            let worker = WorkerState {
                id: id.clone(),
                task,
                socket,
                pid,
                reap: false,
                shutdown: shutdown_tx,
            };
            let mut state = supervisor_state().lock().await;
            state.workers.push(worker);
        }

        let mut reaping = false;

        loop {
            tokio::select!(
                res = child.wait() => {
                    match res {
                        Ok(status) => {
                            let pid = pid.unwrap_or(0);
                            if !reaping {
                                if let Some(code) = status.code() {
                                    warn!("Worker process died: {} (code: {})", pid, code);
                                } else {
                                    warn!("Worker process died: {} ({})", pid, status);
                                }
                            }
                            let mut state = supervisor_state().lock().await;
                            let worker = state.remove(&id);
                            drop(state);
                            if let Some(worker) = worker {
                                info!("Removed child worker (id: {}, pid {})", worker.id, pid);
                                if !worker.reap && worker.task.daemon {
                                    restart(worker, retry).await;
                                }
                            } else {
                                if !reaping {
                                    error!("Failed to remove stale worker for pid {}", pid);
                                }
                            }
                            break;
                        }
                        Err(e) => return Err(e),
                    }
                }
                mut worker = shutdown_rx.recv() => {
                    if let Some(mut worker) = worker.take() {
                        reaping = true;
                        info!("Shutdown worker {}", worker.id);
                        worker.reap = true;
                        child.kill().await?;
                    }
                }
            )
        }

        Ok::<(), io::Error>(())
    });
}

async fn listen<P: AsRef<Path>>(
    socket: P,
    tx: Sender<()>,
    handler: Arc<IpcHandler>,
    control_tx: mpsc::Sender<Message>,
) -> Result<()> {
    let path = socket.as_ref();

    // If the socket file exists we must remove to prevent `EADDRINUSE`
    if path.exists() {
        std::fs::remove_file(path)?;
    }

    let listener = UnixListener::bind(socket).unwrap();
    tx.send(()).unwrap();

    loop {
        match listener.accept().await {
            Ok((stream, _addr)) => (handler)(stream, control_tx.clone()),
            Err(e) => {
                warn!("Supervisor failed to accept worker socket {}", e);
            }
        }
    }
}