Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

admin/on_call: Convert Event::send() to async fn #9581

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/admin/on_call.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::{anyhow, Result};
use crates_io_env_vars::required_var;
use reqwest::{blocking::Client, header, StatusCode as Status};
use reqwest::{header, Client, StatusCode as Status};

#[derive(serde::Serialize, Debug)]
#[serde(rename_all = "snake_case", tag = "event_type")]
Expand All @@ -25,7 +25,7 @@
///
/// If the variant is `Trigger`, this will page whoever is on call
/// (potentially waking them up at 3 AM).
pub fn send(self) -> Result<()> {
pub async fn send(self) -> Result<()> {

Check warning on line 28 in src/admin/on_call.rs

View check run for this annotation

Codecov / codecov/patch

src/admin/on_call.rs#L28

Added line #L28 was not covered by tests
let api_token = required_var("PAGERDUTY_API_TOKEN")?;
let service_key = required_var("PAGERDUTY_INTEGRATION_KEY")?;

Expand All @@ -37,12 +37,13 @@
service_key,
event: self,
})
.send()?;
.send()
.await?;

Check warning on line 41 in src/admin/on_call.rs

View check run for this annotation

Codecov / codecov/patch

src/admin/on_call.rs#L40-L41

Added lines #L40 - L41 were not covered by tests

match response.status() {
s if s.is_success() => Ok(()),
Status::BAD_REQUEST => {
let error = response.json::<InvalidEvent>()?;
let error = response.json::<InvalidEvent>().await?;

Check warning on line 46 in src/admin/on_call.rs

View check run for this annotation

Codecov / codecov/patch

src/admin/on_call.rs#L46

Added line #L46 was not covered by tests
Err(anyhow!("pagerduty error: {:?}", error))
}
Status::FORBIDDEN => Err(anyhow!("rate limited by pagerduty")),
Expand Down
34 changes: 15 additions & 19 deletions src/admin/test_pagerduty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use std::str::FromStr;

use crate::admin::on_call;
use crate::tasks::spawn_blocking;

#[derive(Debug, Copy, Clone, clap::ValueEnum)]
pub enum EventType {
Expand Down Expand Up @@ -33,22 +32,19 @@
}

pub async fn run(opts: Opts) -> Result<()> {
spawn_blocking(move || {
let event = match opts.event_type {
EventType::Trigger => on_call::Event::Trigger {
incident_key: Some("test".into()),
description: opts.description.unwrap_or_else(|| "Test event".into()),
},
EventType::Acknowledge => on_call::Event::Acknowledge {
incident_key: "test".into(),
description: opts.description,
},
EventType::Resolve => on_call::Event::Resolve {
incident_key: "test".into(),
description: opts.description,
},
};
event.send()
})
.await
let event = match opts.event_type {
EventType::Trigger => on_call::Event::Trigger {
incident_key: Some("test".into()),
description: opts.description.unwrap_or_else(|| "Test event".into()),
},
EventType::Acknowledge => on_call::Event::Acknowledge {
incident_key: "test".into(),
description: opts.description,
},
EventType::Resolve => on_call::Event::Resolve {
incident_key: "test".into(),
description: opts.description,
},

Check warning on line 47 in src/admin/test_pagerduty.rs

View check run for this annotation

Codecov / codecov/patch

src/admin/test_pagerduty.rs#L35-L47

Added lines #L35 - L47 were not covered by tests
};
event.send().await

Check warning on line 49 in src/admin/test_pagerduty.rs

View check run for this annotation

Codecov / codecov/patch

src/admin/test_pagerduty.rs#L49

Added line #L49 was not covered by tests
}
25 changes: 10 additions & 15 deletions src/bin/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
//! cargo run --bin monitor

use anyhow::Result;
use crates_io::tasks::spawn_blocking;
use crates_io::worker::jobs;
use crates_io::{admin::on_call, db, schema::*};
use crates_io_env_vars::{var, var_parsed};
Expand Down Expand Up @@ -67,7 +66,7 @@
}
};

spawn_blocking(move || log_and_trigger_event(event)).await?;
log_and_trigger_event(event).await?;

Check warning on line 69 in src/bin/monitor.rs

View check run for this annotation

Codecov / codecov/patch

src/bin/monitor.rs#L69

Added line #L69 was not covered by tests

Ok(())
}
Expand All @@ -94,21 +93,17 @@
let minutes = Utc::now().signed_duration_since(start_time).num_minutes();

if minutes > max_job_time {
return spawn_blocking(move || {
log_and_trigger_event(on_call::Event::Trigger {
incident_key: Some(EVENT_KEY.into()),
description: format!("update_downloads job running for {minutes} minutes"),
})
return log_and_trigger_event(on_call::Event::Trigger {
incident_key: Some(EVENT_KEY.into()),
description: format!("update_downloads job running for {minutes} minutes"),

Check warning on line 98 in src/bin/monitor.rs

View check run for this annotation

Codecov / codecov/patch

src/bin/monitor.rs#L96-L98

Added lines #L96 - L98 were not covered by tests
})
.await;
}
};

spawn_blocking(move || {
log_and_trigger_event(on_call::Event::Resolve {
incident_key: EVENT_KEY.into(),
description: Some("No stalled update_downloads job".into()),
})
log_and_trigger_event(on_call::Event::Resolve {
incident_key: EVENT_KEY.into(),
description: Some("No stalled update_downloads job".into()),

Check warning on line 106 in src/bin/monitor.rs

View check run for this annotation

Codecov / codecov/patch

src/bin/monitor.rs#L104-L106

Added lines #L104 - L106 were not covered by tests
})
.await
}
Expand Down Expand Up @@ -152,11 +147,11 @@
}
};

spawn_blocking(move || log_and_trigger_event(event)).await?;
log_and_trigger_event(event).await?;

Check warning on line 150 in src/bin/monitor.rs

View check run for this annotation

Codecov / codecov/patch

src/bin/monitor.rs#L150

Added line #L150 was not covered by tests
Ok(())
}

fn log_and_trigger_event(event: on_call::Event) -> Result<()> {
async fn log_and_trigger_event(event: on_call::Event) -> Result<()> {

Check warning on line 154 in src/bin/monitor.rs

View check run for this annotation

Codecov / codecov/patch

src/bin/monitor.rs#L154

Added line #L154 was not covered by tests
match event {
on_call::Event::Trigger {
ref description, ..
Expand All @@ -167,5 +162,5 @@
} => println!("{description}"),
_ => {} // noop
}
event.send()
event.send().await

Check warning on line 165 in src/bin/monitor.rs

View check run for this annotation

Codecov / codecov/patch

src/bin/monitor.rs#L165

Added line #L165 was not covered by tests
}