fix(agent): correctly calculate sleep time

Jef Roosens 2022-12-16 21:47:02 +01:00
parent 946d9acd59
commit 1797c0f560
1 changed files with 11 additions and 3 deletions

View File

@ -71,6 +71,8 @@ pub fn (mut d AgentDaemon) run() {
// clustered together (especially when mostly using the global cron // clustered together (especially when mostly using the global cron
// schedule), so there's a much higher chance jobs are available. // 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) { 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 { new_configs := d.client.poll_jobs(d.conf.arch, finished + empty) or {
d.lerror('Failed to poll jobs: $err.msg()') d.lerror('Failed to poll jobs: $err.msg()')
@ -78,6 +80,9 @@ pub fn (mut d AgentDaemon) run() {
time.sleep(5 * time.second) time.sleep(5 * time.second)
continue continue
} }
d.ldebug('Received $new_configs.len jobs')
last_poll_time = time.now() last_poll_time = time.now()
for config in new_configs { for config in new_configs {
@ -105,18 +110,21 @@ pub fn (mut d AgentDaemon) run() {
// No new jobs were scheduled and the agent isn't doing anything, // No new jobs were scheduled and the agent isn't doing anything,
// so we just wait until the next polling period. // so we just wait until the next polling period.
if new_configs.len == 0 && finished + empty == d.conf.max_concurrent_builds { 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 // The agent is not doing anything, so we just wait until the next poll
// time // time
else if finished + empty == d.conf.max_concurrent_builds { 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()
} }
if sleep_time > 0 {
d.ldebug('Sleeping for $sleep_time')
time.sleep(sleep_time) time.sleep(sleep_time)
} }
} }
}
// update_atomics checks for each build whether it's completed, and sets it to // update_atomics checks for each build whether it's completed, and sets it to
// empty again if so. The return value is a tuple `(finished, empty)` where // empty again if so. The return value is a tuple `(finished, empty)` where