From 1797c0f5606e630d6dc14aeadf5de0b49d10f4a4 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Fri, 16 Dec 2022 21:47:02 +0100 Subject: [PATCH 1/2] fix(agent): correctly calculate sleep time --- src/agent/daemon.v | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/agent/daemon.v b/src/agent/daemon.v index c55d0db..b5a6968 100644 --- a/src/agent/daemon.v +++ b/src/agent/daemon.v @@ -71,6 +71,8 @@ pub fn (mut d AgentDaemon) run() { // clustered together (especially when mostly using the global cron // schedule), so there's a much higher chance jobs are available. if finished > 0 || time.now() >= last_poll_time.add_seconds(d.conf.polling_frequency) { + d.ldebug('Polling for new jobs') + new_configs := d.client.poll_jobs(d.conf.arch, finished + empty) or { d.lerror('Failed to poll jobs: $err.msg()') @@ -78,6 +80,9 @@ pub fn (mut d AgentDaemon) run() { time.sleep(5 * time.second) continue } + + d.ldebug('Received $new_configs.len jobs') + last_poll_time = time.now() for config in new_configs { @@ -105,16 +110,19 @@ pub fn (mut d AgentDaemon) run() { // No new jobs were scheduled and the agent isn't doing anything, // so we just wait until the next polling period. if new_configs.len == 0 && finished + empty == d.conf.max_concurrent_builds { - sleep_time = time.now() - last_poll_time + sleep_time = last_poll_time.add_seconds(d.conf.polling_frequency) - time.now() } } // The agent is not doing anything, so we just wait until the next poll // time else if finished + empty == d.conf.max_concurrent_builds { - sleep_time = time.now() - last_poll_time + sleep_time = last_poll_time.add_seconds(d.conf.polling_frequency) - time.now() } - time.sleep(sleep_time) + if sleep_time > 0 { + d.ldebug('Sleeping for $sleep_time') + time.sleep(sleep_time) + } } } From b067f9c589abcd1885b9bed5c8551c56374dfe11 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Fri, 16 Dec 2022 22:06:26 +0100 Subject: [PATCH 2/2] refactor: streamline agent loop code --- src/agent/daemon.v | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/agent/daemon.v b/src/agent/daemon.v index b5a6968..62f36c2 100644 --- a/src/agent/daemon.v +++ b/src/agent/daemon.v @@ -46,16 +46,22 @@ 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) - mut sleep_time := 1 * time.second - mut finished, mut empty := 0, 0 + mut sleep_time := 0 * time.second + mut finished, mut empty, mut running := 0, 0, 0 for { + if sleep_time > 0 { + d.ldebug('Sleeping for $sleep_time') + time.sleep(sleep_time) + } + finished, empty = d.update_atomics() + running = d.conf.max_concurrent_builds - finished - empty // No new finished builds and no free slots, so there's nothing to be // done if finished + empty == 0 { - time.sleep(1 * time.second) + sleep_time = 1 * time.second continue } @@ -77,7 +83,7 @@ pub fn (mut d AgentDaemon) run() { d.lerror('Failed to poll jobs: $err.msg()') // TODO pick a better delay here - time.sleep(5 * time.second) + sleep_time = 5 * time.second continue } @@ -105,23 +111,16 @@ pub fn (mut d AgentDaemon) run() { // build. d.start_build(config) - } - - // No new jobs were scheduled and the agent isn't doing anything, - // so we just wait until the next polling period. - if new_configs.len == 0 && finished + empty == d.conf.max_concurrent_builds { - sleep_time = last_poll_time.add_seconds(d.conf.polling_frequency) - time.now() + running++ } } + // The agent is not doing anything, so we just wait until the next poll // time - else if finished + empty == d.conf.max_concurrent_builds { + if running == 0 { sleep_time = last_poll_time.add_seconds(d.conf.polling_frequency) - time.now() - } - - if sleep_time > 0 { - d.ldebug('Sleeping for $sleep_time') - time.sleep(sleep_time) + } else { + sleep_time = 1 * time.second } } }