feat(build): start of server-side job queue

This commit is contained in:
Jef Roosens 2022-12-06 14:11:17 +01:00
parent 6281ef7607
commit 9a49d96e20
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
9 changed files with 174 additions and 28 deletions

39
src/server/api_builds.v Normal file
View file

@ -0,0 +1,39 @@
module server
/* import web */
/* import web.response { new_data_response, new_response } */
/* import time */
/* import build { BuildConfig } */
/* // import os */
/* // import util */
/* // import models { BuildLog, BuildLogFilter } */
/* ['/api/v1/builds/poll'; auth; get] */
/* fn (mut app App) v1_poll_build_queue() web.Result { */
/* arch := app.query['arch'] or { */
/* return app.json(.bad_request, new_response('Missing arch query arg.')) */
/* } */
/* max_str := app.query['max'] or { */
/* return app.json(.bad_request, new_response('Missing max query arg.')) */
/* } */
/* max := max_str.int() */
/* mut out := []BuildConfig{} */
/* now := time.now() */
/* lock app.build_queues { */
/* mut queue := app.build_queues[arch] or { return app.json(.ok, new_data_response(out)) } */
/* for queue.len() > 0 && out.len < max { */
/* next := queue.peek() or { return app.status(.internal_server_error) } */
/* if next.timestamp < now { */
/* out << queue.pop() or { return app.status(.internal_server_error) }.config */
/* } */
/* } */
/* } */
/* return app.json(.ok, new_data_response(out)) */
/* } */

View file

@ -5,12 +5,14 @@ import conf as vconf
struct Config {
pub:
log_level string = 'WARN'
pkg_dir string
data_dir string
api_key string
default_arch string
port int = 8000
log_level string = 'WARN'
pkg_dir string
data_dir string
api_key string
default_arch string
global_schedule string = '0 3'
port int = 8000
base_image string = 'archlinux:base-devel'
}
// cmd returns the cli submodule that handles starting the server

View file

@ -6,6 +6,8 @@ import log
import repo
import util
import db
import build { BuildJobQueue }
import cron.expression
const (
log_file_name = 'vieter.log'
@ -20,9 +22,37 @@ pub:
conf Config [required; web_global]
pub mut:
repo repo.RepoGroupManager [required; web_global]
db db.VieterDb
// Keys are the various architectures for packages
job_queue BuildJobQueue [required; web_global]
db db.VieterDb
}
// fn (mut app App) init_build_queues() {
// // Initialize build queues
// mut i := 0
// mut targets := app.db.get_targets(limit: 25)
// default_ce := expression.parse_expression(conf.global_schedule) or { return }
// for targets.len > 0 {
// for t in targets {
// ce := parse_expression(t.schedule) or { default_ce }
// for arch in t.arch {
// if arch !in app.build_queues {
// app.build_queues[arch] = Minheap<ScheduledBuild>{}
// }
// build_config := BuildConfig{}
// app.build_queues[arch].push(ScheduledBuild{
// timestamp: ce.next()
// config: build_config
// })
// }
// }
// }
//}
// server starts the web server & starts listening for requests
pub fn server(conf Config) ! {
// Prevent using 'any' as the default arch
@ -30,6 +60,10 @@ pub fn server(conf Config) ! {
util.exit_with_message(1, "'any' is not allowed as the value for default_arch.")
}
global_ce := expression.parse_expression(conf.global_schedule) or {
util.exit_with_message(1, 'Invalid global cron expression: $err.msg()')
}
// Configure logger
log_level := log.level_from_tag(conf.log_level) or {
util.exit_with_message(1, 'Invalid log level. The allowed values are FATAL, ERROR, WARN, INFO & DEBUG.')
@ -71,11 +105,14 @@ pub fn server(conf Config) ! {
util.exit_with_message(1, 'Failed to initialize database: $err.msg()')
}
mut queue := build.new_job_queue(global_ce, conf.base_image)
web.run(&App{
logger: logger
api_key: conf.api_key
conf: conf
repo: repo
db: db
job_queue: queue
}, conf.port)
}