feat(agent): begin reforming for new api

Jef Roosens 2022-12-12 21:21:58 +01:00 committed by Chewing_Bever
parent 0a5c4295e0
commit 5bab1f77f0
Signed by untrusted user: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 88 additions and 3 deletions

View File

@ -14,6 +14,8 @@ const (
struct AgentDaemon {
logger shared log.Log
conf Config
// List of last built builder images
builder_images []string
// Which builds are currently running; length is same as
// conf.max_concurrent_builds
builds []BuildConfig
@ -44,9 +46,8 @@ pub fn (mut d AgentDaemon) run() {
}
}
// clean_finished_builds 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.
// 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.
fn (mut d AgentDaemon) update_atomics() int {
mut count := 0

49
src/agent/images.v 100644
View File

@ -0,0 +1,49 @@
module agent
import time
import docker
struct ImageManager {
images map[string]string
timestamps map[string]time.Time
}
// clean_old_base_images tries to remove any old but still present builder
// images.
fn (mut d AgentDaemon) clean_old_base_images() {
mut i := 0
mut dd := docker.new_conn() or {
d.lerror('Failed to connect to Docker socket.')
return
}
defer {
dd.close() or {}
}
for i < d.builder_images.len - 1 {
// For each builder image, we try to remove it by calling the Docker
// API. If the function returns an error or false, that means the image
// wasn't deleted. Therefore, we move the index over. If the function
// returns true, the array's length has decreased by one so we don't
// move the index.
dd.remove_image(d.builder_images[i]) or { i += 1 }
}
}
// rebuild_base_image builds a builder image from the given base image.
/* fn (mut d AgentDaemon) build_base_image(base_image string) bool { */
/* d.linfo('Rebuilding builder image....') */
/* d.builder_images << build.create_build_image(d.base_image) or { */
/* d.lerror('Failed to rebuild base image. Retrying in ${daemon.rebuild_base_image_retry_timout}s...') */
/* d.image_build_timestamp = time.now().add_seconds(daemon.rebuild_base_image_retry_timout) */
/* return false */
/* } */
/* d.image_build_timestamp = time.now().add_seconds(60 * d.image_rebuild_frequency) */
/* return true */
/* } */

35
src/agent/log.v 100644
View File

@ -0,0 +1,35 @@
module agent
import log
// log reate a log message with the given level
pub fn (mut d AgentDaemon) log(msg string, level log.Level) {
lock d.logger {
d.logger.send_output(msg, level)
}
}
// lfatal create a log message with the fatal level
pub fn (mut d AgentDaemon) lfatal(msg string) {
d.log(msg, log.Level.fatal)
}
// lerror create a log message with the error level
pub fn (mut d AgentDaemon) lerror(msg string) {
d.log(msg, log.Level.error)
}
// lwarn create a log message with the warn level
pub fn (mut d AgentDaemon) lwarn(msg string) {
d.log(msg, log.Level.warn)
}
// linfo create a log message with the info level
pub fn (mut d AgentDaemon) linfo(msg string) {
d.log(msg, log.Level.info)
}
// ldebug create a log message with the debug level
pub fn (mut d AgentDaemon) ldebug(msg string) {
d.log(msg, log.Level.debug)
}