vieter/src/agent/daemon.v

65 lines
1.4 KiB
Coq
Raw Normal View History

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
conf Config
// List of last built builder images
builder_images []string
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
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
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()
if free_builds > 0 {
}
2022-12-06 13:50:25 +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
}