Skip to content

Commit

Permalink
feat: add worker_job_update api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jiegec committed Mar 10, 2024
1 parent b33cd58 commit c1920a4
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 4 deletions.
7 changes: 7 additions & 0 deletions DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ Job result:
1. List of successful builds
2. Failed package and link to build log (on buildit.aosc.io)

Job status:

1. created: can be assigned to worker
2. assigned: assigned to worker
3. error: unexpected error
4. finished: finished, successful or failed

## Authentication

Authentication:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
ALTER TABLE jobs DROP COLUMN error_message;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Your SQL goes here
ALTER TABLE jobs ADD COLUMN error_message TEXT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
ALTER TABLE jobs DROP COLUMN elapsed_secs;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Your SQL goes here
ALTER TABLE jobs ADD COLUMN elapsed_secs BIGINT;
2 changes: 2 additions & 0 deletions server/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub struct Job {
pub skipped_packages: Option<String>,
pub log_url: Option<String>,
pub finish_time: Option<chrono::DateTime<chrono::Utc>>,
pub error_message: Option<String>,
pub elapsed_secs: Option<i64>,
}

#[derive(Insertable)]
Expand Down
33 changes: 30 additions & 3 deletions server/src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,17 @@ pub enum JobResult {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JobOk {
/// Is the build successful?
pub success: bool,
pub build_success: bool,
/// List of packages successfully built
pub successful_packages: Vec<String>,
/// List of packages failed to build
pub failed_package: Option<String>,
/// List of packages skipped
pub skipped_packages: Vec<String>,
/// URL to build log
pub log: Option<String>,
pub log_url: Option<String>,
/// Elapsed time of the job
pub elapsed: Duration,
pub elapsed_secs: i64,
/// If pushpkg succeeded
pub pushpkg_success: bool,
}
Expand All @@ -216,5 +216,32 @@ pub async fn worker_job_update(
State(pool): State<DbPool>,
Json(payload): Json<WorkerJobUpdateRequest>,
) -> Result<(), AnyhowError> {
let mut conn = pool
.get()
.context("Failed to get db connection from pool")?;

use crate::schema::jobs::dsl::*;
match payload.result {
JobResult::Ok(res) => {
diesel::update(jobs.filter(id.eq(payload.job_id)))
.set((
status.eq("finished"),
build_success.eq(res.build_success),
pushpkg_success.eq(res.pushpkg_success),
successful_packages.eq(res.successful_packages.join(",")),
failed_package.eq(res.failed_package),
skipped_packages.eq(res.skipped_packages.join(",")),
log_url.eq(res.log_url),
finish_time.eq(chrono::Utc::now()),
elapsed_secs.eq(res.elapsed_secs),
))
.execute(&mut conn)?;
}
JobResult::Error(err) => {
diesel::update(jobs.filter(id.eq(payload.job_id)))
.set((status.eq("error"), error_message.eq(err)))
.execute(&mut conn)?;
}
}
Ok(())
}
8 changes: 7 additions & 1 deletion server/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ diesel::table! {
skipped_packages -> Nullable<Text>,
log_url -> Nullable<Text>,
finish_time -> Nullable<Timestamptz>,
error_message -> Nullable<Text>,
elapsed_secs -> Nullable<Int8>,
}
}

Expand Down Expand Up @@ -47,4 +49,8 @@ diesel::table! {

diesel::joinable!(jobs -> pipelines (pipeline_id));

diesel::allow_tables_to_appear_in_same_query!(jobs, pipelines, workers,);
diesel::allow_tables_to_appear_in_same_query!(
jobs,
pipelines,
workers,
);

0 comments on commit c1920a4

Please sign in to comment.