Skip to content

Commit d7eacdc

Browse files
aster-voidclaude
andcommitted
scheduler: extract magic numbers and deduplicate output logic
- Add SCHEDULE_TOLERANCE_MS constant for cron matching tolerance - Add AUTO_JITTER_RATIO constant for default jitter calculation - Extract print_output_lines helper to reduce code duplication 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent 67ecbe6 commit d7eacdc

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

src/scheduler/backoff.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ use std::time::Duration;
33

44
use crate::config::RetryConfig;
55

6+
/// Default jitter ratio when not explicitly configured (25% of base delay)
7+
const AUTO_JITTER_RATIO: u32 = 25;
8+
69
pub fn calculate_backoff(retry: &RetryConfig, attempt: u32) -> Duration {
710
let base_delay = retry.delay.saturating_mul(2u32.saturating_pow(attempt));
8-
let jitter_max = retry.jitter.unwrap_or_else(|| retry.delay.saturating_mul(25) / 100);
11+
let jitter_max =
12+
retry.jitter.unwrap_or_else(|| retry.delay.saturating_mul(AUTO_JITTER_RATIO) / 100);
913
base_delay.saturating_add(generate_jitter(jitter_max))
1014
}
1115

src/scheduler/executor.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@ enum CommandResult {
8181
Timeout,
8282
}
8383

84+
fn print_output_lines(tag: &str, output: &str, use_stderr: bool) {
85+
if output.trim().is_empty() {
86+
return;
87+
}
88+
for line in output.lines() {
89+
if use_stderr {
90+
eprintln!("{} | {}", tag, line);
91+
} else {
92+
println!("{} | {}", tag, line);
93+
}
94+
}
95+
}
96+
8497
fn handle_result(tag: &str, job: &Job, result: &CommandResult) -> bool {
8598
match result {
8699
CommandResult::Completed(output) => {
@@ -89,24 +102,12 @@ fn handle_result(tag: &str, job: &Job, result: &CommandResult) -> bool {
89102

90103
if output.status.success() {
91104
println!("{} ✓ Completed", tag);
92-
if !stdout.trim().is_empty() {
93-
for line in stdout.lines() {
94-
println!("{} | {}", tag, line);
95-
}
96-
}
105+
print_output_lines(tag, &stdout, false);
97106
true
98107
} else {
99108
eprintln!("{} ✗ Failed (exit code: {:?})", tag, output.status.code());
100-
if !stderr.trim().is_empty() {
101-
for line in stderr.lines() {
102-
eprintln!("{} | {}", tag, line);
103-
}
104-
}
105-
if !stdout.trim().is_empty() {
106-
for line in stdout.lines() {
107-
eprintln!("{} | {}", tag, line);
108-
}
109-
}
109+
print_output_lines(tag, &stderr, true);
110+
print_output_lines(tag, &stdout, true);
110111
false
111112
}
112113
}

src/scheduler/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,17 @@ impl Scheduler {
218218

219219
// === Helper Functions ===
220220

221+
/// Tolerance for schedule matching (accounts for 1-second tick interval)
222+
const SCHEDULE_TOLERANCE_MS: i64 = 1000;
223+
221224
fn is_job_due<Z: TimeZone>(schedule: &cron::Schedule, tz: Z) -> bool
222225
where
223226
Z::Offset: std::fmt::Display,
224227
{
225228
let now = Utc::now().with_timezone(&tz);
226229
if let Some(next) = schedule.upcoming(tz).next() {
227230
let until_next = (next - now).num_milliseconds();
228-
until_next <= 1000 && until_next >= 0
231+
until_next <= SCHEDULE_TOLERANCE_MS && until_next >= 0
229232
} else {
230233
false
231234
}

0 commit comments

Comments
 (0)