Skip to content

Commit a71697e

Browse files
committed
Cancel job when bot is removed as a reviewer on a PR
1 parent 5c17ee1 commit a71697e

6 files changed

Lines changed: 103 additions & 3 deletions

File tree

Cargo.lock

Lines changed: 75 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ pretty_env_logger = "0.4"
1414
serde_json = "1.0"
1515
uuid = { version = "0.8", features = ["v4"] }
1616
nix = "0.17"
17-
lazy_static = "1.4"
17+
lazy_static = "1.4"
18+
futures = "0.3"

src/lib/cmd.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::process::{Command, Stdio};
33
use std::sync::Arc;
44
use tokio::sync::RwLock;
55
use uuid::Uuid;
6-
use nix::sys::signal::{self, Signal};
76

87
use crate::lib::job;
98

src/lib/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::env;
22
use lazy_static::lazy_static;
33

44
pub const REVIEW_REQUESTED: &'static str = "review_requested";
5+
pub const REVIEW_REQUEST_REMOVED: &'static str = "review_request_removed";
56
pub const REVIEWER: &'static str = "sexxi-bot";
67
// TODO(azhng): const REPO: &'static str = "rust";
78

@@ -17,6 +18,7 @@ pub const SEXXI_TEST_PROJECT: &'static str = "sexxi-webhook-test";
1718

1819
pub const SEXXI_REMOTE_HOST: &'static str = "sorbitol";
1920
pub const SEXXI_LOG_FILE_DIR: &'static str = "www/build-logs";
21+
pub const SEXXI_TIMEOUT_JOB_IN_SEC: u64 = 60 * 60 * 2;
2022

2123
lazy_static! {
2224
pub static ref MACHINE_USER: String = env::var("USER").unwrap();

src/lib/handler.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,28 @@ async fn parse_and_handle(
145145
return Ok::<_, hyper::Error>(gen_response(500));
146146
};
147147
},
148+
config::REVIEW_REQUEST_REMOVED => {
149+
let head_ref = json["pull_request"]["head"]["ref"].as_str().unwrap();
150+
let mut job_registry = job_registry.write().await;
151+
152+
let c_job: Arc<RwLock<job::JobDesc>>;
153+
let c_job_id: Uuid;
154+
if let Some(c_id) = job_registry.running_jobs.get(head_ref.clone()) {
155+
c_job_id = c_id.clone();
156+
if let Some(j) = job_registry.jobs.get(&c_job_id) {
157+
c_job = j.clone();
158+
let curr_job = c_job.read().await;
159+
if head_ref == curr_job.head_ref {
160+
info!("Request to run job on {} has been removed. Cancelling running job.",head_ref);
161+
// [To-Do] Fix, could potentially kill a process which is already dead
162+
signal::kill(Pid::from_raw(curr_job.pid as i32), Signal::SIGINT);
163+
164+
// Remove cancelled job
165+
job_registry.running_jobs.remove(head_ref);
166+
}
167+
}
168+
}
169+
},
148170
_ => {
149171
warn!("Action: {} not handled", action);
150172
}

src/lib/job.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ pub async fn process_job(job_id: &Uuid, job_registry: Arc<RwLock<JobRegistry>>)
118118
in removing a running job not the canceled job.
119119
*/
120120
if job.status != JobStatus::Canceled {
121+
info!("Removing job from job_registry!");
121122
{
122123
let mut job_registry = job_registry.write().await;
123124
job_registry.running_jobs.remove(&job.head_ref.clone());
@@ -218,11 +219,11 @@ async fn run_and_build(job: &JobDesc, job_registry: &Arc<RwLock<JobRegistry>>) -
218219
Ok(())
219220
}
220221

221-
222222
async fn start_build_job(job: &JobDesc, job_registry: &Arc<RwLock<JobRegistry>>) -> Result<(), String> {
223223
let comment = format!("{}, job id: {}", config::COMMENT_JOB_START, &job.id);
224224
if let Err(e) = api::post_comment(&comment, job.pr_num).await {
225225
return Err(format!("failed to post comment to pr {}: {}", &job.pr_num, e));
226226
}
227+
227228
run_and_build(job, job_registry).await
228229
}

0 commit comments

Comments
 (0)