fix reminder tool issues: error on time overflow, optimize scheduler query
This commit is contained in:
parent
bc27113967
commit
24eec69418
4 changed files with 90 additions and 48 deletions
|
|
@ -194,26 +194,48 @@ async fn dispatch(req: &AgentRequest, agent: &str, coord: &Arc<Coordinator>) ->
|
|||
file_path,
|
||||
} => {
|
||||
use hive_sh4re::ReminderTiming;
|
||||
let due_at = match timing {
|
||||
|
||||
// Calculate the due_at timestamp, propagating errors instead of silently
|
||||
// defaulting to epoch 1970 on overflow/conversion failure.
|
||||
let due_at_result: Result<i64> = match timing {
|
||||
ReminderTiming::InSeconds { seconds } => {
|
||||
std::time::SystemTime::now()
|
||||
.checked_add(std::time::Duration::from_secs(*seconds))
|
||||
.and_then(|t| {
|
||||
t.duration_since(std::time::UNIX_EPOCH)
|
||||
.ok()
|
||||
.and_then(|d| i64::try_from(d.as_secs()).ok())
|
||||
})
|
||||
.unwrap_or(0)
|
||||
let now = std::time::SystemTime::now();
|
||||
let future = match now.checked_add(std::time::Duration::from_secs(*seconds)) {
|
||||
Some(t) => t,
|
||||
None => return AgentResponse::Err {
|
||||
message: format!("InSeconds overflow: {seconds}s exceeds system time range"),
|
||||
},
|
||||
};
|
||||
let duration = match future.duration_since(std::time::UNIX_EPOCH) {
|
||||
Ok(d) => d,
|
||||
Err(e) => return AgentResponse::Err {
|
||||
message: format!("system time before UNIX_EPOCH: {e}"),
|
||||
},
|
||||
};
|
||||
match i64::try_from(duration.as_secs()) {
|
||||
Ok(ts) => Ok(ts),
|
||||
Err(e) => return AgentResponse::Err {
|
||||
message: format!("unix timestamp exceeds i64 range: {e}"),
|
||||
},
|
||||
}
|
||||
}
|
||||
ReminderTiming::At { unix_timestamp } => *unix_timestamp,
|
||||
ReminderTiming::At { unix_timestamp } => Ok(*unix_timestamp),
|
||||
};
|
||||
match broker.store_reminder(agent, message, file_path.as_deref(), due_at) {
|
||||
Ok(id) => {
|
||||
tracing::info!(%id, %agent, %due_at, "reminder scheduled");
|
||||
AgentResponse::Ok
|
||||
|
||||
match due_at_result {
|
||||
Ok(due_at) => {
|
||||
match broker.store_reminder(agent, message, file_path.as_deref(), due_at) {
|
||||
Ok(id) => {
|
||||
tracing::info!(%id, %agent, %due_at, "reminder scheduled");
|
||||
AgentResponse::Ok
|
||||
}
|
||||
Err(e) => AgentResponse::Err {
|
||||
message: format!("failed to store reminder: {e:#}"),
|
||||
},
|
||||
}
|
||||
}
|
||||
Err(e) => AgentResponse::Err {
|
||||
message: format!("failed to store reminder: {e:#}"),
|
||||
message: format!("invalid reminder timing: {e:#}"),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue