feat(server): update job queue when adding, removing or updating targets

Jef Roosens 2022-12-13 13:58:51 +01:00
parent e742d3de6d
commit 6342789921
Signed by untrusted user: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 26 additions and 10 deletions

View File

@ -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 // 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 // job will then be endlessly rescheduled after being pop'ed, unless removed
// explicitely. // explicitely.

View File

@ -12,17 +12,17 @@ fn (mut app App) v1_get_targets() web.Result {
filter := models.from_params<TargetFilter>(app.query) or { filter := models.from_params<TargetFilter>(app.query) or {
return app.json(http.Status.bad_request, new_response('Invalid query parameters.')) 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. // v1_get_single_target returns the information for a single target.
['/api/v1/targets/:id'; auth; get] ['/api/v1/targets/:id'; auth; get]
fn (mut app App) v1_get_single_target(id int) web.Result { 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. // 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 { fn (mut app App) v1_post_target() web.Result {
mut params := app.query.clone() 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. // for the default architecture.
if 'arch' !in params || params['arch'] == '' { if 'arch' !in params || params['arch'] == '' {
params['arch'] = app.conf.default_arch params['arch'] = app.conf.default_arch
} }
new_repo := models.from_params<Target>(params) or { mut new_target := models.from_params<Target>(params) or {
return app.json(http.Status.bad_request, new_response(err.msg())) return app.json(http.Status.bad_request, new_response(err.msg()))
} }
// Ensure someone doesn't submit an invalid kind // 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.')) 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)) 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] ['/api/v1/targets/:id'; auth; delete]
fn (mut app App) v1_delete_target(id int) web.Result { fn (mut app App) v1_delete_target(id int) web.Result {
app.db.delete_target(id) app.db.delete_target(id)
app.job_queue.invalidate(id)
return app.json(.ok, new_response('')) 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) 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))
} }