Skip to content

Commit 56a0bcf

Browse files
committed
feat: Add sd_notify support
1 parent 4919cf5 commit 56a0bcf

5 files changed

Lines changed: 45 additions & 3 deletions

File tree

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/http/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ rkyv = { version = "0.8.10", features = ["little_endian"] }
4545
form-data = { version = "0.6.0", features = ["sync"], default-features = false }
4646
mime = "0.3.17"
4747
compact_str = "0.9.0"
48+
sd-notify = "0.4.5"
4849

4950
[dev-dependencies]
5051

crates/http/src/management/reload.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ use common::{
1010
ipc::{BroadcastEvent, HousekeeperEvent},
1111
};
1212
use directory::Permission;
13+
use http_proto::*;
1314
use hyper::Method;
15+
use sd_notify;
1416
use serde_json::json;
1517
use std::future::Future;
1618
use utils::url_params::UrlParams;
1719

18-
use http_proto::*;
19-
2020
pub trait ManageReload: Sync + Send {
2121
fn handle_manage_reload(
2222
&self,
@@ -43,7 +43,16 @@ impl ManageReload for Server {
4343
// Validate the access token
4444
access_token.assert_has_permission(Permission::SettingsReload)?;
4545

46-
match (path.get(1).copied(), req.method()) {
46+
let is_systemd = match sd_notify::booted() {
47+
Ok(booted) => booted,
48+
_ => false,
49+
};
50+
51+
if (is_systemd) {
52+
let _ = sd_notify::notify(true, &[sd_notify::NotifyState::Reloading]);
53+
}
54+
55+
let response = match (path.get(1).copied(), req.method()) {
4756
(Some("lookup"), &Method::GET) => {
4857
let result = self.reload_lookups().await?;
4958
// Update core
@@ -117,7 +126,13 @@ impl ManageReload for Server {
117126
.into_http_response())
118127
}
119128
_ => Err(trc::ResourceEvent::NotFound.into_err()),
129+
};
130+
131+
if (is_systemd) {
132+
let _ = sd_notify::notify(true, &[sd_notify::NotifyState::Ready]);
120133
}
134+
135+
response
121136
}
122137

123138
async fn handle_manage_update(

crates/main/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ trc = { path = "../trc" }
3535
utils = { path = "../utils" }
3636
migration = { path = "../migration" }
3737
tokio = { version = "1.47", features = ["full"] }
38+
sd-notify = "0.4.5"
3839

3940
[target.'cfg(not(target_env = "msvc"))'.dependencies]
4041
jemallocator = "0.5.0"

crates/main/src/main.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use http::HttpSessionManager;
1414
use imap::core::ImapSessionManager;
1515
use managesieve::core::ManageSieveSessionManager;
1616
use pop3::Pop3SessionManager;
17+
use sd_notify;
1718
use services::{StartServices, broadcast::subscriber::spawn_broadcast_subscriber};
1819
use smtp::{StartQueueManager, core::SmtpSessionManager};
1920
use std::time::Duration;
@@ -97,9 +98,22 @@ async fn main() -> std::io::Result<()> {
9798
// Start broadcast subscriber
9899
spawn_broadcast_subscriber(init.inner, shutdown_rx);
99100

101+
let is_systemd = match sd_notify::booted() {
102+
Ok(booted) => booted,
103+
_ => false,
104+
};
105+
106+
if (is_systemd) {
107+
let _ = sd_notify::notify(true, &[sd_notify::NotifyState::Ready]);
108+
}
109+
100110
// Wait for shutdown signal
101111
wait_for_shutdown().await;
102112

113+
if (is_systemd) {
114+
let _ = sd_notify::notify(true, &[sd_notify::NotifyState::Stopping]);
115+
}
116+
103117
// Shutdown collector
104118
Collector::shutdown();
105119

0 commit comments

Comments
 (0)