feat(agent): initial working version

Jef Roosens 2022-12-12 22:58:43 +01:00 committed by Chewing_Bever
parent 6f23d690a7
commit 3611123f45
Signed by untrusted user: Jef Roosens
GPG Key ID: B75D4F293C7052DB
7 changed files with 47 additions and 24 deletions

View File

@ -5,9 +5,9 @@ import conf as vconf
struct Config { struct Config {
pub: pub:
log_level string = 'WARN' log_level string = 'WARN'
// Architecture that the agent represents // Architecture that the agent represents
arch string arch string
api_key string api_key string
address string address string
data_dir string data_dir string

View File

@ -41,6 +41,10 @@ fn agent_init(logger log.Log, conf Config) AgentDaemon {
} }
pub fn (mut d AgentDaemon) run() { pub fn (mut d AgentDaemon) run() {
// This is just so that the very first time the loop is ran, the jobs are
// always polled
mut last_poll_time := time.now().add_seconds(-d.conf.polling_frequency)
for { for {
free_builds := d.update_atomics() free_builds := d.update_atomics()
@ -54,16 +58,37 @@ pub fn (mut d AgentDaemon) run() {
d.images.clean_old_images() d.images.clean_old_images()
// Poll for new jobs // Poll for new jobs
new_configs := d.client.poll_jobs(free_builds) or { if time.now() >= last_poll_time.add_seconds(d.conf.polling_frequency) {
d.lerror('Failed to poll jobs: $err.msg()') new_configs := d.client.poll_jobs(d.conf.arch, free_builds) or {
d.lerror('Failed to poll jobs: $err.msg()')
time.sleep(5 * time.second)
continue
}
last_poll_time = time.now()
// Schedule new jobs
for config in new_configs {
// TODO handle this better than to just skip the config
// Make sure a recent build base image is available for building the config
d.images.refresh_image(config.base_image) or {
d.lerror(err.msg())
continue
}
d.start_build(config)
}
time.sleep(1 * time.second) time.sleep(1 * time.second)
continue
} }
// Builds are running, so check again after one second
// Schedule new jobs else if free_builds < d.conf.max_concurrent_builds {
for config in new_configs { time.sleep(1 * time.second)
d.start_build(config) }
// The agent is not doing anything, so we just wait until the next poll
// time
else {
time_until_next_poll := time.now() - last_poll_time
time.sleep(time_until_next_poll)
} }
} }
} }

View File

@ -104,16 +104,16 @@ pub:
} }
pub fn build_target(address string, api_key string, base_image_id string, target &Target) !BuildResult { pub fn build_target(address string, api_key string, base_image_id string, target &Target) !BuildResult {
config := BuildConfig{ config := BuildConfig{
target_id: target.id target_id: target.id
kind: target.kind kind: target.kind
url: target.url url: target.url
branch: target.branch branch: target.branch
repo: target.repo repo: target.repo
base_image: base_image_id base_image: base_image_id
} }
return build_config(address, api_key, config) return build_config(address, api_key, config)
} }
// build_target builds, packages & publishes a given Arch package based on the // build_target builds, packages & publishes a given Arch package based on the

View File

@ -76,7 +76,6 @@ pub fn (mut q BuildJobQueue) insert(target Target, arch string) ! {
} }
} }
dump(job)
q.queues[arch].insert(job) q.queues[arch].insert(job)
} }
} }

View File

@ -1,7 +1,5 @@
module build module build
import models { Target }
// escape_shell_string escapes any characters that could be interpreted // escape_shell_string escapes any characters that could be interpreted
// incorrectly by a shell. The resulting value should be safe to use inside an // incorrectly by a shell. The resulting value should be safe to use inside an
// echo statement. // echo statement.

View File

@ -2,9 +2,10 @@ module client
import build { BuildConfig } import build { BuildConfig }
pub fn (c &Client) poll_jobs(max int) ![]BuildConfig { pub fn (c &Client) poll_jobs(arch string, max int) ![]BuildConfig {
data := c.send_request<[]BuildConfig>(.get, '/api/v1/jobs/poll', { data := c.send_request<[]BuildConfig>(.get, '/api/v1/jobs/poll', {
'max': max.str() 'arch': arch
'max': max.str()
})! })!
return data.data return data.data

View File

@ -4,6 +4,7 @@ data_dir = "data"
pkg_dir = "data/pkgs" pkg_dir = "data/pkgs"
log_level = "DEBUG" log_level = "DEBUG"
default_arch = "x86_64" default_arch = "x86_64"
arch = "x86_64"
address = "http://localhost:8000" address = "http://localhost:8000"
@ -11,4 +12,3 @@ global_schedule = '* *'
api_update_frequency = 2 api_update_frequency = 2
image_rebuild_frequency = 1 image_rebuild_frequency = 1
max_concurrent_builds = 3 max_concurrent_builds = 3