Skip to content

Commit

Permalink
refactor(webhooks): pass trigger to other webhook events
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-online committed Feb 7, 2025
1 parent f546f9b commit 36f2270
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 21 deletions.
44 changes: 31 additions & 13 deletions crates/service/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,29 @@ impl PulseRunner {
sify(&processed)
);

self.webhooks
.add_event(EventType::Processed, None, &processed)
.await;
for ev in &processed {
self.webhooks
.add_event(
EventType::Processed,
Some(ev.event_source.clone()),
&[ev.file_path.clone()],
)
.await;
}
}

if !retrying.is_empty() {
warn!("retrying {} file{}", retrying.len(), sify(&retrying));

self.webhooks
.add_event(EventType::Retrying, None, &retrying)
.await;
for ev in &retrying {
self.webhooks
.add_event(
EventType::Retrying,
Some(ev.event_source.clone()),
&[ev.file_path.clone()],
)
.await;
}
}

if !failed.is_empty() {
Expand All @@ -155,9 +167,15 @@ impl PulseRunner {
sify(&failed)
);

self.webhooks
.add_event(EventType::Error, None, &failed)
.await;
for ev in &failed {
self.webhooks
.add_event(
EventType::Failed,
Some(ev.event_source.clone()),
&[ev.file_path.clone()],
)
.await;
}
}

Ok(())
Expand All @@ -166,7 +184,7 @@ impl PulseRunner {
async fn process_events(
&self,
evs: &mut [ScanEvent],
) -> anyhow::Result<(Vec<String>, Vec<String>, Vec<String>)> {
) -> anyhow::Result<(Vec<ScanEvent>, Vec<ScanEvent>, Vec<ScanEvent>)> {
let mut failed_ids = vec![];

let trigger_settings = &self.settings.triggers;
Expand Down Expand Up @@ -225,20 +243,20 @@ impl PulseRunner {
if ev.failed_times >= self.settings.opts.max_retries {
ev.process_status = ProcessStatus::Failed.into();
ev.next_retry_at = None;
failed.push(conn.save_changes(ev)?.file_path.clone());
failed.push(conn.save_changes(ev)?);
} else {
let next_retry = chrono::Utc::now().naive_utc()
+ chrono::Duration::seconds(2_i64.pow(ev.failed_times as u32 + 1));

ev.process_status = ProcessStatus::Retry.into();
ev.next_retry_at = Some(next_retry);

retrying.push(conn.save_changes(ev)?.file_path.clone());
retrying.push(conn.save_changes(ev)?);
}
} else {
ev.process_status = ProcessStatus::Complete.into();
ev.processed_at = Some(chrono::Utc::now().naive_utc());
succeeded.push(conn.save_changes(ev)?.file_path.clone());
succeeded.push(conn.save_changes(ev)?);
}
}

Expand Down
8 changes: 4 additions & 4 deletions crates/service/src/settings/webhooks/discord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ impl DiscordWebhook {

for (event, trigger, files) in batch {
let color = match event {
EventType::New => 6_061_450, // grey
EventType::Found => 52084, // green
EventType::Error => 16_711_680, // red
EventType::Processed => 39129, // blue
EventType::New => 6_061_450, // grey
EventType::Found => 52084, // green
EventType::Failed => 16_711_680, // red
EventType::Processed => 39129, // blue
EventType::Retrying | EventType::HashMismatch => 16_776_960,
};

Expand Down
8 changes: 4 additions & 4 deletions crates/service/src/settings/webhooks/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ pub enum EventType {
Retrying = 3,
/// Processed event
Processed = 4,
/// Error event
Error = 5,
/// Failed event
Failed = 5,
}

impl Display for EventType {
Expand All @@ -29,7 +29,7 @@ impl Display for EventType {
Self::New => "NEW",
Self::Retrying => "RETRY",
Self::Found => "FOUND",
Self::Error => "ERROR",
Self::Failed => "FAILED",
Self::Processed => "PROCESSED",
Self::HashMismatch => "HASH MISMATCH",
};
Expand All @@ -44,7 +44,7 @@ impl EventType {
Self::New => "added",
Self::Found => "found",
Self::Retrying => "retrying",
Self::Error => "failed",
Self::Failed => "failed",
Self::Processed => "processed",
Self::HashMismatch => "mismatched",
}
Expand Down

0 comments on commit 36f2270

Please sign in to comment.