From 63427899217aae4390e01b72238758ac1457856d Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Tue, 13 Dec 2022 13:58:51 +0100 Subject: [PATCH] feat(server): update job queue when adding, removing or updating targets --- src/build/queue.v | 7 +++++++ src/server/api_targets.v | 29 +++++++++++++++++++---------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/build/queue.v b/src/build/queue.v index b5595524..a78e56a2 100644 --- a/src/build/queue.v +++ b/src/build/queue.v @@ -49,6 +49,13 @@ pub fn new_job_queue(default_schedule CronExpression, default_base_image string) } } +// insert_all executes insert for each architecture of the given Target. +pub fn (mut q BuildJobQueue) insert_all(target Target) ! { + for arch in target.arch { + q.insert(target, arch.value)! + } +} + // insert a new target's job into the queue for the given architecture. This // job will then be endlessly rescheduled after being pop'ed, unless removed // explicitely. diff --git a/src/server/api_targets.v b/src/server/api_targets.v index 16db7e9b..dc39d374 100644 --- a/src/server/api_targets.v +++ b/src/server/api_targets.v @@ -12,17 +12,17 @@ fn (mut app App) v1_get_targets() web.Result { filter := models.from_params(app.query) or { return app.json(http.Status.bad_request, new_response('Invalid query parameters.')) } - repos := app.db.get_targets(filter) + targets := app.db.get_targets(filter) - return app.json(.ok, new_data_response(repos)) + return app.json(.ok, new_data_response(targets)) } // v1_get_single_target returns the information for a single target. ['/api/v1/targets/:id'; auth; get] fn (mut app App) v1_get_single_target(id int) web.Result { - repo := app.db.get_target(id) or { return app.not_found() } + target := app.db.get_target(id) or { return app.not_found() } - return app.json(.ok, new_data_response(repo)) + return app.json(.ok, new_data_response(target)) } // v1_post_target creates a new target from the provided query string. @@ -30,22 +30,27 @@ fn (mut app App) v1_get_single_target(id int) web.Result { fn (mut app App) v1_post_target() web.Result { mut params := app.query.clone() - // If a repo is created without specifying the arch, we assume it's meant + // If a target is created without specifying the arch, we assume it's meant // for the default architecture. if 'arch' !in params || params['arch'] == '' { params['arch'] = app.conf.default_arch } - new_repo := models.from_params(params) or { + mut new_target := models.from_params(params) or { return app.json(http.Status.bad_request, new_response(err.msg())) } // Ensure someone doesn't submit an invalid kind - if new_repo.kind !in models.valid_kinds { + if new_target.kind !in models.valid_kinds { return app.json(http.Status.bad_request, new_response('Invalid kind.')) } - id := app.db.add_target(new_repo) + id := app.db.add_target(new_target) + new_target.id = id + + // Add the target to the job queue + // TODO return better error here if it's the cron schedule that's incorrect + app.job_queue.insert_all(new_target) or { return app.status(.internal_server_error) } return app.json(.ok, new_data_response(id)) } @@ -54,6 +59,7 @@ fn (mut app App) v1_post_target() web.Result { ['/api/v1/targets/:id'; auth; delete] fn (mut app App) v1_delete_target(id int) web.Result { app.db.delete_target(id) + app.job_queue.invalidate(id) return app.json(.ok, new_response('')) } @@ -69,7 +75,10 @@ fn (mut app App) v1_patch_target(id int) web.Result { app.db.update_target_archs(id, arch_objs) } - repo := app.db.get_target(id) or { return app.status(.internal_server_error) } + target := app.db.get_target(id) or { return app.status(.internal_server_error) } - return app.json(.ok, new_data_response(repo)) + app.job_queue.invalidate(id) + app.job_queue.insert_all(target) or { return app.status(.internal_server_error) } + + return app.json(.ok, new_data_response(target)) }