2026-01-21 12:34:59 +01:00
|
|
|
// Copyright (C) 2024-2026 Whiterun LLC
|
2026-01-21 12:23:08 +01:00
|
|
|
//
|
|
|
|
|
// This software is licensed under the GNU Affero General Public License (AGPL), version 3.0 or later.
|
|
|
|
|
// A copy of the license can be found in the LICENSE file or at https://www.gnu.org/licenses/agpl-3.0.html
|
|
|
|
|
|
|
|
|
|
use rocket::fairing::{Fairing, Info, Kind};
|
|
|
|
|
use rocket::{Orbit, Rocket};
|
|
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
|
|
|
|
|
|
static SHUTDOWN_FLAG: AtomicBool = AtomicBool::new(false);
|
|
|
|
|
|
|
|
|
|
/// Returns true if a shutdown has been requested.
|
|
|
|
|
pub fn shutdown_requested() -> bool {
|
|
|
|
|
SHUTDOWN_FLAG.load(Ordering::Relaxed)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Requests a graceful shutdown of all background threads.
|
|
|
|
|
pub fn request_shutdown() {
|
|
|
|
|
SHUTDOWN_FLAG.store(true, Ordering::SeqCst);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Rocket fairing that calls `request_shutdown()` when Rocket shuts down.
|
|
|
|
|
pub struct ShutdownFairing;
|
|
|
|
|
|
|
|
|
|
#[rocket::async_trait]
|
|
|
|
|
impl Fairing for ShutdownFairing {
|
|
|
|
|
fn info(&self) -> Info {
|
|
|
|
|
Info {
|
|
|
|
|
name: "Shutdown Fairing",
|
|
|
|
|
kind: Kind::Shutdown,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn on_shutdown(&self, _rocket: &Rocket<Orbit>) {
|
|
|
|
|
log::info!("Shutdown signal received, requesting graceful shutdown...");
|
|
|
|
|
request_shutdown();
|
|
|
|
|
}
|
|
|
|
|
}
|