Skip to content

Commit 09831fa

Browse files
authored
feat(admin): fail error in looped jobs, notify gc'ed community projects (#2063)
1 parent 133ef80 commit 09831fa

File tree

3 files changed

+70
-13
lines changed

3 files changed

+70
-13
lines changed

admin/src/args.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,16 @@ pub enum Command {
7979
flag: String,
8080
},
8181

82-
/// Garbage collect free tier projects
83-
Gc {
82+
/// Garbage collect Community tier projects
83+
GcCommunity {
8484
/// days since last deployment to filter by
8585
days: u32,
8686
/// loop and stop the returned projects instead of printing them
8787
#[arg(long)]
8888
stop_deployments: bool,
89+
/// send emails to owners of projects
90+
#[arg(long)]
91+
send_emails: bool,
8992
/// limit how many projects to stop
9093
#[arg(long, default_value_t = 100)]
9194
limit: u32,

admin/src/client.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ impl Client {
136136
self.inner.get_json(&path).await
137137
}
138138

139+
pub async fn stop_gc_inactive_project(&self, project_id: &str) -> Result<String> {
140+
let path = format!("/admin/gc/stop-inactive-project/{project_id}");
141+
self.inner.put_json(&path, Option::<()>::None).await
142+
}
143+
139144
pub async fn get_user(&self, user_id: &str) -> Result<UserResponse> {
140145
self.inner.get_json(format!("/admin/users/{user_id}")).await
141146
}

admin/src/lib.rs

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,34 @@ pub async fn run(args: Args) {
6464
team_user_id,
6565
user_id,
6666
} => {
67-
client
67+
let res = client
6868
.add_team_member(&team_user_id, user_id)
6969
.await
7070
.unwrap();
71-
println!("added");
71+
println!("{res}");
7272
}
7373
Command::RenewCerts => {
7474
let certs = client.get_old_certificates().await.unwrap();
7575
eprintln!("Starting renewals of {} certs in 5 seconds...", certs.len());
7676
tokio::time::sleep(tokio::time::Duration::from_millis(5000)).await;
77+
let mut any_error = false;
7778
for (cert_id, subject, acm) in certs {
7879
println!(
79-
"--> {cert_id} {subject} {}",
80+
"{cert_id} {subject} {}",
8081
if acm.is_some() { "(ACM)" } else { "" }
8182
);
82-
println!("{:?}", client.renew_certificate(&cert_id).await);
83+
println!(
84+
" {:?}",
85+
client.renew_certificate(&cert_id).await.inspect_err(|_| {
86+
any_error = true;
87+
})
88+
);
8389
// prevent api rate limiting
8490
tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await;
8591
}
92+
if any_error {
93+
panic!("Error in loop occured");
94+
}
8695
}
8796
Command::UpdateProjectConfig { project_id, json } => {
8897
let res = client
@@ -120,21 +129,22 @@ pub async fn run(args: Args) {
120129
client.feature_flag(&entity, &flag, false).await.unwrap();
121130
println!("Removed flag {flag} for {entity}");
122131
}
123-
Command::Gc {
132+
Command::GcCommunity {
124133
days,
125134
stop_deployments,
135+
send_emails,
126136
limit,
127137
} => {
128138
let project_ids = client.gc_free_tier(days).await.unwrap();
129-
gc(client, project_ids, stop_deployments, limit).await;
139+
gc(client, project_ids, stop_deployments, send_emails, limit).await;
130140
}
131141
Command::GcShuttlings {
132142
minutes,
133143
stop_deployments,
134144
limit,
135145
} => {
136146
let project_ids = client.gc_shuttlings(minutes).await.unwrap();
137-
gc(client, project_ids, stop_deployments, limit).await;
147+
gc(client, project_ids, stop_deployments, false, limit).await;
138148
}
139149
Command::DeleteUser { user_id } => {
140150
eprintln!("Deleting user {} in 3 seconds...", user_id);
@@ -157,27 +167,51 @@ pub async fn run(args: Args) {
157167
users.len()
158168
);
159169
tokio::time::sleep(tokio::time::Duration::from_millis(5000)).await;
170+
let mut any_error = false;
160171
for user_id in users {
161172
println!("{user_id}");
162-
println!(" {:?}", client.downgrade_protrial(&user_id).await);
173+
println!(
174+
" {:?}",
175+
client.downgrade_protrial(&user_id).await.inspect_err(|_| {
176+
any_error = true;
177+
})
178+
);
163179
// prevent api rate limiting
164180
tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await;
165181
}
182+
if any_error {
183+
panic!("Error in loop occured");
184+
}
166185
}
167186
Command::FixRetentionPolicies => {
168187
let projects = client.get_projects_for_retention_policy().await.unwrap();
169188
eprintln!("Starting fix of {} log retention policies", projects.len());
189+
let mut any_error = false;
170190
for pid in projects {
171191
println!("{pid}");
172-
println!(" {:?}", client.fix_retention_policy(&pid).await);
192+
println!(
193+
" {:?}",
194+
client.fix_retention_policy(&pid).await.inspect_err(|_| {
195+
any_error = true;
196+
})
197+
);
173198
// prevent api rate limiting
174199
tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await;
175200
}
201+
if any_error {
202+
panic!("Error in loop occured");
203+
}
176204
}
177205
};
178206
}
179207

180-
async fn gc(client: Client, mut project_ids: Vec<String>, stop_deployments: bool, limit: u32) {
208+
async fn gc(
209+
client: Client,
210+
mut project_ids: Vec<String>,
211+
stop_deployments: bool,
212+
send_email: bool,
213+
limit: u32,
214+
) {
181215
if !stop_deployments {
182216
for pid in &project_ids {
183217
println!("{pid}");
@@ -192,9 +226,24 @@ async fn gc(client: Client, mut project_ids: Vec<String>, stop_deployments: bool
192226
project_ids.len()
193227
);
194228
tokio::time::sleep(tokio::time::Duration::from_millis(5000)).await;
229+
let mut any_error = false;
195230
for pid in project_ids {
196-
println!("{}", client.inner.stop_service(&pid).await.unwrap());
231+
println!("{pid}");
232+
let call = if send_email {
233+
client.stop_gc_inactive_project(&pid).await
234+
} else {
235+
client.inner.stop_service(&pid).await
236+
};
237+
println!(
238+
" {:?}",
239+
call.inspect_err(|_| {
240+
any_error = true;
241+
})
242+
);
197243
// prevent api rate limiting
198244
tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await;
199245
}
246+
if any_error {
247+
panic!("Error in loop occured");
248+
}
200249
}

0 commit comments

Comments
 (0)