2022-12-06 13:50:25 +01:00
|
|
|
module agent
|
|
|
|
|
|
|
|
import log
|
|
|
|
import sync.stdatomic
|
|
|
|
import build { BuildConfig }
|
|
|
|
import client
|
|
|
|
|
|
|
|
const (
|
|
|
|
build_empty = 0
|
|
|
|
build_running = 1
|
|
|
|
build_done = 2
|
|
|
|
)
|
|
|
|
|
|
|
|
struct AgentDaemon {
|
|
|
|
logger shared log.Log
|
2022-12-06 14:11:17 +01:00
|
|
|
conf Config
|
2022-12-12 21:50:34 +01:00
|
|
|
images ImageManager
|
2022-12-06 13:50:25 +01:00
|
|
|
// Which builds are currently running; length is same as
|
|
|
|
// conf.max_concurrent_builds
|
|
|
|
builds []BuildConfig
|
|
|
|
// Atomic variables used to detect when a build has finished; length is the
|
|
|
|
// same as conf.max_concurrent_builds
|
2022-12-06 14:11:17 +01:00
|
|
|
client client.Client
|
2022-12-06 13:50:25 +01:00
|
|
|
atomics []u64
|
|
|
|
}
|
|
|
|
|
|
|
|
fn agent_init(logger log.Log, conf Config) AgentDaemon {
|
|
|
|
mut d := AgentDaemon{
|
|
|
|
logger: logger
|
|
|
|
client: client.new(conf.address, conf.api_key)
|
|
|
|
conf: conf
|
2022-12-12 21:50:34 +01:00
|
|
|
images: new_image_manager(conf.image_rebuild_frequency)
|
2022-12-06 13:50:25 +01:00
|
|
|
builds: []BuildConfig{len: conf.max_concurrent_builds}
|
|
|
|
atomics: []u64{len: conf.max_concurrent_builds}
|
|
|
|
}
|
|
|
|
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut d AgentDaemon) run() {
|
|
|
|
for {
|
|
|
|
free_builds := d.update_atomics()
|
|
|
|
|
2022-12-06 14:11:17 +01:00
|
|
|
if free_builds > 0 {
|
|
|
|
}
|
2022-12-06 13:50:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-12 21:21:58 +01:00
|
|
|
// update_atomics checks for each build whether it's completed, and sets it to
|
|
|
|
// free again if so. The return value is how many fields are now set to free.
|
2022-12-06 13:50:25 +01:00
|
|
|
fn (mut d AgentDaemon) update_atomics() int {
|
|
|
|
mut count := 0
|
|
|
|
|
|
|
|
for i in 0 .. d.atomics.len {
|
|
|
|
if stdatomic.load_u64(&d.atomics[i]) == agent.build_done {
|
|
|
|
stdatomic.store_u64(&d.atomics[i], agent.build_empty)
|
|
|
|
count++
|
|
|
|
} else if stdatomic.load_u64(&d.atomics[i]) == agent.build_empty {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return count
|
|
|
|
}
|