Skip to content

Commit

Permalink
Cancel job when bot is removed as a reviewer on a PR
Browse files Browse the repository at this point in the history
  • Loading branch information
roxelo committed Jun 25, 2020
1 parent 47a0b13 commit fda4a9e
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 7 deletions.
75 changes: 75 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ pretty_env_logger = "0.4"
serde_json = "1.0"
uuid = { version = "0.8", features = ["v4"] }
nix = "0.17"
lazy_static = "1.4"
lazy_static = "1.4"
futures = "0.3"
4 changes: 2 additions & 2 deletions src/lib/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use lazy_static::lazy_static;
use std::env;

pub const REVIEW_REQUESTED: &'static str = "review_requested";
pub const REVIEW_REQUESTED_REMOVED: &'static str = "review_requested_removed";

pub const REVIEW_REQUEST_REMOVED: &'static str = "review_request_removed";
pub const REVIEWER: &'static str = "sexxi-bot";
// TODO(azhng): const REPO: &'static str = "rust";

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

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

lazy_static! {
pub static ref MACHINE_USER: String = env::var("USER").unwrap();
Expand Down
22 changes: 22 additions & 0 deletions src/lib/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,28 @@ async fn parse_and_handle(
return Ok::<_, hyper::Error>(gen_response(500));
};
},
config::REVIEW_REQUEST_REMOVED => {
let head_ref = json["pull_request"]["head"]["ref"].as_str().unwrap();
let mut job_registry = job_registry.write().await;

let c_job: Arc<RwLock<job::JobDesc>>;
let c_job_id: Uuid;
if let Some(c_id) = job_registry.running_jobs.get(head_ref.clone()) {
c_job_id = c_id.clone();
if let Some(j) = job_registry.jobs.get(&c_job_id) {
c_job = j.clone();
let curr_job = c_job.read().await;
if head_ref == curr_job.head_ref {
info!("Request to run job on {} has been removed. Cancelling running job.",head_ref);
// [To-Do] Fix, could potentially kill a process which is already dead
signal::kill(Pid::from_raw(curr_job.pid as i32), Signal::SIGINT);

// Remove cancelled job
job_registry.running_jobs.remove(head_ref);
}
}
}
},
_ => {
warn!("Action: {} not handled", action);
}
Expand Down
9 changes: 5 additions & 4 deletions src/lib/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ pub async fn process_job(job_id: &Uuid, job_registry: Arc<RwLock<JobRegistry>>)
error!("job {} failed due to: {}", &job_id, e);
}

if job.read().await.status == JobStatus::Canceled {
job_registry.write().await.running_jobs.remove(&job.read().await.head_ref.clone());
}
// We should be removing the job from the running_jobs list when the job is no more running
info!("Job is no longer running, remove the job from the running_jobs list");
job_registry.write().await.running_jobs.remove(&job.read().await.head_ref.clone());

}

async fn job_failure_handler<T: std::fmt::Display>(
Expand Down Expand Up @@ -203,7 +204,6 @@ async fn run_and_build(job: &Arc<RwLock<JobDesc>>) -> Result<(), String> {
Ok(())
}


async fn start_build_job(job: &Arc<RwLock<JobDesc>>) -> Result<(), String> {
{
let job = &*job.read().await;
Expand All @@ -212,5 +212,6 @@ async fn start_build_job(job: &Arc<RwLock<JobDesc>>) -> Result<(), String> {
return Err(format!("failed to post comment to pr {}: {}", &job.pr_num, e));
}
}

run_and_build(job).await
}

0 comments on commit fda4a9e

Please sign in to comment.