Skip to content

Commit e32ad30

Browse files
authored
admin/on_call: Convert Event::send() to async fn (#9581)
1 parent 429e1be commit e32ad30

File tree

3 files changed

+30
-38
lines changed

3 files changed

+30
-38
lines changed

src/admin/on_call.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use anyhow::{anyhow, Result};
22
use crates_io_env_vars::required_var;
3-
use reqwest::{blocking::Client, header, StatusCode as Status};
3+
use reqwest::{header, Client, StatusCode as Status};
44

55
#[derive(serde::Serialize, Debug)]
66
#[serde(rename_all = "snake_case", tag = "event_type")]
@@ -25,7 +25,7 @@ impl Event {
2525
///
2626
/// If the variant is `Trigger`, this will page whoever is on call
2727
/// (potentially waking them up at 3 AM).
28-
pub fn send(self) -> Result<()> {
28+
pub async fn send(self) -> Result<()> {
2929
let api_token = required_var("PAGERDUTY_API_TOKEN")?;
3030
let service_key = required_var("PAGERDUTY_INTEGRATION_KEY")?;
3131

@@ -37,12 +37,13 @@ impl Event {
3737
service_key,
3838
event: self,
3939
})
40-
.send()?;
40+
.send()
41+
.await?;
4142

4243
match response.status() {
4344
s if s.is_success() => Ok(()),
4445
Status::BAD_REQUEST => {
45-
let error = response.json::<InvalidEvent>()?;
46+
let error = response.json::<InvalidEvent>().await?;
4647
Err(anyhow!("pagerduty error: {:?}", error))
4748
}
4849
Status::FORBIDDEN => Err(anyhow!("rate limited by pagerduty")),

src/admin/test_pagerduty.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use anyhow::Result;
22
use std::str::FromStr;
33

44
use crate::admin::on_call;
5-
use crate::tasks::spawn_blocking;
65

76
#[derive(Debug, Copy, Clone, clap::ValueEnum)]
87
pub enum EventType {
@@ -33,22 +32,19 @@ pub struct Opts {
3332
}
3433

3534
pub async fn run(opts: Opts) -> Result<()> {
36-
spawn_blocking(move || {
37-
let event = match opts.event_type {
38-
EventType::Trigger => on_call::Event::Trigger {
39-
incident_key: Some("test".into()),
40-
description: opts.description.unwrap_or_else(|| "Test event".into()),
41-
},
42-
EventType::Acknowledge => on_call::Event::Acknowledge {
43-
incident_key: "test".into(),
44-
description: opts.description,
45-
},
46-
EventType::Resolve => on_call::Event::Resolve {
47-
incident_key: "test".into(),
48-
description: opts.description,
49-
},
50-
};
51-
event.send()
52-
})
53-
.await
35+
let event = match opts.event_type {
36+
EventType::Trigger => on_call::Event::Trigger {
37+
incident_key: Some("test".into()),
38+
description: opts.description.unwrap_or_else(|| "Test event".into()),
39+
},
40+
EventType::Acknowledge => on_call::Event::Acknowledge {
41+
incident_key: "test".into(),
42+
description: opts.description,
43+
},
44+
EventType::Resolve => on_call::Event::Resolve {
45+
incident_key: "test".into(),
46+
description: opts.description,
47+
},
48+
};
49+
event.send().await
5450
}

src/bin/monitor.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
//! cargo run --bin monitor
66
77
use anyhow::Result;
8-
use crates_io::tasks::spawn_blocking;
98
use crates_io::worker::jobs;
109
use crates_io::{admin::on_call, db, schema::*};
1110
use crates_io_env_vars::{var, var_parsed};
@@ -67,7 +66,7 @@ async fn check_failing_background_jobs(conn: &mut AsyncPgConnection) -> Result<(
6766
}
6867
};
6968

70-
spawn_blocking(move || log_and_trigger_event(event)).await?;
69+
log_and_trigger_event(event).await?;
7170

7271
Ok(())
7372
}
@@ -94,21 +93,17 @@ async fn check_stalled_update_downloads(conn: &mut AsyncPgConnection) -> Result<
9493
let minutes = Utc::now().signed_duration_since(start_time).num_minutes();
9594

9695
if minutes > max_job_time {
97-
return spawn_blocking(move || {
98-
log_and_trigger_event(on_call::Event::Trigger {
99-
incident_key: Some(EVENT_KEY.into()),
100-
description: format!("update_downloads job running for {minutes} minutes"),
101-
})
96+
return log_and_trigger_event(on_call::Event::Trigger {
97+
incident_key: Some(EVENT_KEY.into()),
98+
description: format!("update_downloads job running for {minutes} minutes"),
10299
})
103100
.await;
104101
}
105102
};
106103

107-
spawn_blocking(move || {
108-
log_and_trigger_event(on_call::Event::Resolve {
109-
incident_key: EVENT_KEY.into(),
110-
description: Some("No stalled update_downloads job".into()),
111-
})
104+
log_and_trigger_event(on_call::Event::Resolve {
105+
incident_key: EVENT_KEY.into(),
106+
description: Some("No stalled update_downloads job".into()),
112107
})
113108
.await
114109
}
@@ -152,11 +147,11 @@ async fn check_spam_attack(conn: &mut AsyncPgConnection) -> Result<()> {
152147
}
153148
};
154149

155-
spawn_blocking(move || log_and_trigger_event(event)).await?;
150+
log_and_trigger_event(event).await?;
156151
Ok(())
157152
}
158153

159-
fn log_and_trigger_event(event: on_call::Event) -> Result<()> {
154+
async fn log_and_trigger_event(event: on_call::Event) -> Result<()> {
160155
match event {
161156
on_call::Event::Trigger {
162157
ref description, ..
@@ -167,5 +162,5 @@ fn log_and_trigger_event(event: on_call::Event) -> Result<()> {
167162
} => println!("{description}"),
168163
_ => {} // noop
169164
}
170-
event.send()
165+
event.send().await
171166
}

0 commit comments

Comments
 (0)