fn reload_nginx()

in src/vtok_agent/src/agent/mod.rs [161:228]


    fn reload_nginx(force_start: bool, wait_ms: u64) {
        info!("Reloading NGINX config");
        Command::new("systemctl")
            .args(&["show", "--property=MainPID", "nginx.service"])
            .output()
            .map_err(Error::SystemdExecError)
            .and_then(|output| {
                if !output.status.success() {
                    return Err(Error::SystemdShowPidError(
                        output.status.code(),
                        String::from_utf8_lossy(output.stderr.as_slice()).to_string(),
                    ));
                }
                String::from_utf8(output.stdout)
                    .map_err(Error::Utf8Error)
                    .and_then(|line| {
                        line.as_str()
                            .trim()
                            .rsplit("=")
                            .next()
                            .ok_or(Error::SystemdParsePidError)
                            .and_then(|pid_str| {
                                pid_str
                                    .parse::<i32>()
                                    .map_err(|_| Error::SystemdParsePidError)
                            })
                    })
            })
            .and_then(|pid| match (pid, force_start) {
                (0, true) => {
                    info!("NGINX is not running. Starting it now.");
                    Command::new("systemctl")
                        .args(&["start", "nginx.service"])
                        .status()
                        .map_err(Error::SystemdExecError)
                        .and_then(|status| {
                            if !status.success() {
                                Err(Error::SystemdStartNginxError(status.code()))
                            } else {
                                Ok(())
                            }
                        })
                }
                (0, false) => {
                    warn!(
                        "Unable to reload NGINX: it isn't running and force starting is disabled"
                    );
                    Ok(())
                }
                (pid, _) => {
                    debug!("Sending SIGUSR2 to PID={}", pid);
                    signal::kill(unistd::Pid::from_raw(pid), signal::Signal::SIGUSR2)
                        .map_err(Error::SendSignalError)?;
                    debug!("Sending SIGWINCH to PID={}", pid);
                    signal::kill(unistd::Pid::from_raw(pid), signal::Signal::SIGWINCH)
                        .map_err(Error::SendSignalError)?;
                    debug!("Sleeping to allow NGINX to process live update.");
                    std::thread::sleep(Duration::from_millis(wait_ms));
                    debug!("Sending SIGQUIT to PID={}", pid);
                    signal::kill(unistd::Pid::from_raw(pid), signal::Signal::SIGQUIT)
                        .map_err(Error::SendSignalError)?;
                    Ok(())
                }
            })
            .unwrap_or_else(|err| {
                error!("Unable to reload NGINX: {:?}", err);
            });
    }