diff --git a/src/cron/daemon/build.v b/src/cron/daemon/build.v new file mode 100644 index 00000000..e7e5ac34 --- /dev/null +++ b/src/cron/daemon/build.v @@ -0,0 +1,45 @@ +module daemon + +import git +import time +import sync.stdatomic + +// update_builds starts as many builds as possible. +fn (mut d Daemon) update_builds() ? { + now := time.now() + + for d.queue.len() > 0 { + if d.queue.peek() ?.timestamp < now { + sb := d.queue.pop() ? + + // If this build couldn't be scheduled, no more will be possible. + if !d.start_build(sb.repo_id)? { + break + } + } else { + break + } + } +} + +// start_build starts a build for the given repo_id. +fn (mut d Daemon) start_build(repo_id string) ?bool { + for i in 0..d.atomics.len { + if stdatomic.load_u64(&d.atomics[i]) == 0 { + stdatomic.store_u64(&d.atomics[i], 1) + + go d.run_build(i, d.repos_map[repo_id]) + + return true + } + } + + return false +} + +fn (mut d Daemon) run_build(build_index int, repo git.GitRepo) ? { + time.sleep(10 * time.second) + + stdatomic.store_u64(&d.atomics[build_index], 2) +} + diff --git a/src/cron/daemon/daemon.v b/src/cron/daemon/daemon.v index eadd04c1..fc917e49 100644 --- a/src/cron/daemon/daemon.v +++ b/src/cron/daemon/daemon.v @@ -61,8 +61,13 @@ pub fn init_daemon(logger log.Log, address string, api_key string, base_image st // run starts the actual daemon process. It runs builds when possible & // periodically refreshes the list of repositories to ensure we stay in sync. pub fn (mut d Daemon) run() ? { - println(d.queue) - println('i am running') + for { + d.update_builds() ? + println(d.queue) + println(d.atomics) + + time.sleep(60 * time.second) + } } fn (mut d Daemon) renew_repos() ? { diff --git a/vieter.toml b/vieter.toml index e646739f..452500fe 100644 --- a/vieter.toml +++ b/vieter.toml @@ -9,5 +9,5 @@ default_arch = "x86_64" address = "http://localhost:8000" -global_schedule = '0 3' +global_schedule = '* *'