From 3611123f4549523f2420ac8a1157d146c9064c8d Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Mon, 12 Dec 2022 22:58:43 +0100 Subject: [PATCH] feat(agent): initial working version --- src/agent/cli.v | 4 ++-- src/agent/daemon.v | 39 ++++++++++++++++++++++++++++++++------- src/build/build.v | 18 +++++++++--------- src/build/queue.v | 1 - src/build/shell.v | 2 -- src/client/jobs.v | 5 +++-- vieter.toml | 2 +- 7 files changed, 47 insertions(+), 24 deletions(-) diff --git a/src/agent/cli.v b/src/agent/cli.v index 1badbab..a375f08 100644 --- a/src/agent/cli.v +++ b/src/agent/cli.v @@ -5,9 +5,9 @@ import conf as vconf struct Config { pub: - log_level string = 'WARN' + log_level string = 'WARN' // Architecture that the agent represents - arch string + arch string api_key string address string data_dir string diff --git a/src/agent/daemon.v b/src/agent/daemon.v index aabcb44..f060863 100644 --- a/src/agent/daemon.v +++ b/src/agent/daemon.v @@ -41,6 +41,10 @@ fn agent_init(logger log.Log, conf Config) AgentDaemon { } 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 { free_builds := d.update_atomics() @@ -54,16 +58,37 @@ pub fn (mut d AgentDaemon) run() { d.images.clean_old_images() // Poll for new jobs - new_configs := d.client.poll_jobs(free_builds) or { - d.lerror('Failed to poll jobs: $err.msg()') + if time.now() >= last_poll_time.add_seconds(d.conf.polling_frequency) { + 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) - continue } - - // Schedule new jobs - for config in new_configs { - d.start_build(config) + // Builds are running, so check again after one second + else if free_builds < d.conf.max_concurrent_builds { + time.sleep(1 * time.second) + } + // 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) } } } diff --git a/src/build/build.v b/src/build/build.v index 744ce9c..2d51156 100644 --- a/src/build/build.v +++ b/src/build/build.v @@ -104,16 +104,16 @@ pub: } pub fn build_target(address string, api_key string, base_image_id string, target &Target) !BuildResult { -config := BuildConfig{ - target_id: target.id - kind: target.kind - url: target.url - branch: target.branch - repo: target.repo - base_image: base_image_id - } + config := BuildConfig{ + target_id: target.id + kind: target.kind + url: target.url + branch: target.branch + repo: target.repo + 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 diff --git a/src/build/queue.v b/src/build/queue.v index b704926..29036e4 100644 --- a/src/build/queue.v +++ b/src/build/queue.v @@ -76,7 +76,6 @@ pub fn (mut q BuildJobQueue) insert(target Target, arch string) ! { } } - dump(job) q.queues[arch].insert(job) } } diff --git a/src/build/shell.v b/src/build/shell.v index 42ec3c0..c2d0c9b 100644 --- a/src/build/shell.v +++ b/src/build/shell.v @@ -1,7 +1,5 @@ module build -import models { Target } - // escape_shell_string escapes any characters that could be interpreted // incorrectly by a shell. The resulting value should be safe to use inside an // echo statement. diff --git a/src/client/jobs.v b/src/client/jobs.v index 281d6ce..30f2531 100644 --- a/src/client/jobs.v +++ b/src/client/jobs.v @@ -2,9 +2,10 @@ module client 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', { - 'max': max.str() + 'arch': arch + 'max': max.str() })! return data.data diff --git a/vieter.toml b/vieter.toml index d3922a4..9a68ae3 100644 --- a/vieter.toml +++ b/vieter.toml @@ -4,6 +4,7 @@ data_dir = "data" pkg_dir = "data/pkgs" log_level = "DEBUG" default_arch = "x86_64" +arch = "x86_64" address = "http://localhost:8000" @@ -11,4 +12,3 @@ global_schedule = '* *' api_update_frequency = 2 image_rebuild_frequency = 1 max_concurrent_builds = 3 -