//! `faunosteo-worker run`: the keyed parent's loop (STORAGE.md §3.4, §3.5) and //! `run --check`, the start-up self-description without any secret value. use std::io::Write; use std::time::{Instant, SystemTime, UNIX_EPOCH}; use crate::backend::Backend; use crate::config::{self, Config, ConfigError}; use crate::decode::libwebp_version; use crate::decoder::{ChildDecoder, Decoder}; use crate::http::HttpBackend; use crate::job::{JobEnd, process_job}; use crate::limits::MIN_LIBWEBP_VERSION; use crate::sweep::{SweepConfig, run_sweep}; use crate::watchdog::{Watchdog, deadlines}; use crate::{exit_codes, log}; const THIRTY_DAYS: i64 = 30 * 24 * 3600; /// Entry of the `run` subcommand; returns the process exit code. pub fn main(args: &[String]) -> i32 { match args.first().map(String::as_str) { Some("--check") => check(), Some(other) => { eprintln!("run: unknown argument {other} (use --check to print the configuration)"); exit_codes::USAGE } None => start(), } } fn start() -> i32 { let config = match Config::from_env() { Ok(config) => config, Err(error) => { eprintln!("run: {error}"); return exit_codes::USAGE; } }; if !libwebp_ok() { eprintln!("run: linked libwebp below the minimum, refusing to start"); return exit_codes::INTERNAL; } log::set_worker_id(&config.worker_id); log_start(&config); let decoder = match ChildDecoder::from_current_exe() { Ok(decoder) => decoder, Err(error) => { log::error("start_failed") .detail(&format!("current_exe: {error}")) .emit(); return exit_codes::INTERNAL; } }; let backend = HttpBackend::new(&config); let watchdog = Watchdog::new(); watchdog.spawn_monitor(); run_loop(&config, &backend, &decoder, &watchdog) } /// Sweep at start and every `sweep` interval; claim one job per pass; idle on `poll`. pub fn run_loop( config: &Config, backend: &dyn Backend, decoder: &dyn Decoder, watchdog: &Watchdog, ) -> i32 { let sweep_config = SweepConfig { pending_max_age: config.pending_max_age, quarantine_grace: config.quarantine_grace, }; let mut next_sweep = Instant::now(); loop { if Instant::now() >= next_sweep { run_sweep(backend, watchdog, sweep_config); next_sweep = Instant::now() + config.sweep; } let worked = match claim_pass(backend, decoder, watchdog) { Ok(count) => count > 0, Err(code) => return code, }; if config.once { return exit_codes::OK; } if !worked { std::thread::sleep(config.poll); } } } /// One claim and the jobs it returned; `Err` carries the exit code of a stop. fn claim_pass( backend: &dyn Backend, decoder: &dyn Decoder, watchdog: &Watchdog, ) -> Result { let jobs = { let _stage = watchdog.enter("claim", deadlines::CLAIM); match backend.claim_jobs(1) { Ok(jobs) => jobs, Err(error) => { let line = log::warn("claim_failed").code(error.kind()); match error { crate::backend::BackendError::Status(status) => line.http_status(status).emit(), _ => line.emit(), } return Ok(0); } } }; for job in &jobs { if process_job(backend, decoder, watchdog, job) == JobEnd::Stop { return Err(exit_codes::CHILD_BROKEN); } } Ok(jobs.len()) } fn log_start(config: &Config) { let (major, minor, rev) = libwebp_version(); log::info("worker_start") .detail(&format!( "libwebp {major}.{minor}.{rev}; poll {}s sweep {}s grace {}s pending {}s once {}", config.poll.as_secs(), config.sweep.as_secs(), config.quarantine_grace.as_secs(), config.pending_max_age.as_secs(), config.once )) .emit(); match token_expiry(config) { TokenExpiry::NotJwt => log::info("token_no_expiry").emit(), TokenExpiry::Valid(days) => log::info("token_expiry").count(days).emit(), TokenExpiry::Soon(days) => log::warn("token_expires_soon").count(days).emit(), TokenExpiry::Expired => log::error("token_expired").emit(), } } enum TokenExpiry { NotJwt, Valid(usize), Soon(usize), Expired, } fn token_expiry(config: &Config) -> TokenExpiry { let Some(exp) = config::jwt_exp(config.service_key.expose()) else { return TokenExpiry::NotJwt; }; let now = SystemTime::now() .duration_since(UNIX_EPOCH) .map(|d| i64::try_from(d.as_secs()).unwrap_or(i64::MAX)) .unwrap_or(0); let left = exp - now; let days = usize::try_from(left / 86_400).unwrap_or(0); if left <= 0 { TokenExpiry::Expired } else if left < THIRTY_DAYS { TokenExpiry::Soon(days) } else { TokenExpiry::Valid(days) } } fn libwebp_ok() -> bool { libwebp_version() >= MIN_LIBWEBP_VERSION } /// Prints the resolved configuration without any value of a secret. Exit 0 when the /// worker could start with this environment, or when no worker variable is set at all /// (an unconfigured image is a valid artifact check); a partial or invalid configuration /// and a too-old libwebp exit non-zero and name the variable. fn check() -> i32 { let (major, minor, rev) = libwebp_version(); let libwebp_ok = libwebp_ok(); println!( "libwebp: {major}.{minor}.{rev} ({})", if libwebp_ok { "accepted" } else { "below the minimum, refusing to start" } ); let mut any_set = false; for name in config::VARIABLES { let set = std::env::var_os(name).is_some(); any_set |= set; println!("{name}: {}", if set { "set" } else { "unset" }); } let configuration = if any_set { Some(Config::from_env()) } else { None }; print_configuration(configuration.as_ref()); let _ = std::io::stdout().flush(); match (libwebp_ok, configuration) { (false, _) => exit_codes::INTERNAL, (true, None | Some(Ok(_))) => exit_codes::OK, (true, Some(Err(_))) => exit_codes::USAGE, } } fn print_configuration(configuration: Option<&Result>) { match configuration { None => println!("configuration: none (no worker variable set; the loop would not start)"), Some(Err(error)) => println!("configuration: invalid ({error})"), Some(Ok(config)) => { println!( "configuration: ok (url {}, worker_id {}, poll {}s, sweep {}s, quarantine grace {}s, pending max age {}s, once {})", config.url, config.worker_id, config.poll.as_secs(), config.sweep.as_secs(), config.quarantine_grace.as_secs(), config.pending_max_age.as_secs(), config.once ); match token_expiry(config) { TokenExpiry::NotJwt => println!("token: not a JWT (no expiry to track)"), TokenExpiry::Valid(days) => println!("token: JWT expiring in {days} days"), TokenExpiry::Soon(days) => { println!("token: JWT expiring in {days} days (WARNING: under 30)") } TokenExpiry::Expired => println!("token: JWT EXPIRED"), } } } }