feat(server): use cron schedule for log removal instead
All checks were successful
ci/woodpecker/pr/docs Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/docker Pipeline was successful
ci/woodpecker/pr/man Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful

This commit is contained in:
Jef Roosens 2022-12-19 09:47:53 +01:00
parent 09c61143b0
commit 26796f2228
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
7 changed files with 75 additions and 21 deletions

View file

@ -5,15 +5,16 @@ import conf as vconf
struct Config {
pub:
log_level string = 'WARN'
pkg_dir string
data_dir string
api_key string
default_arch string
global_schedule string = '0 3'
port int = 8000
base_image string = 'archlinux:base-devel'
max_log_age int = -1
port int = 8000
log_level string = 'WARN'
pkg_dir string
data_dir string
api_key string
default_arch string
global_schedule string = '0 3'
base_image string = 'archlinux:base-devel'
max_log_age int = -1
log_removal_schedule string = '0 0'
}
// cmd returns the cli submodule that handles starting the server

View file

@ -3,11 +3,12 @@ module server
import time
import models { BuildLog }
import os
import cron.expression { CronExpression }
const log_removal_frequency = 24 * time.hour
const fallback_log_removal_frequency = 24 * time.hour
// log_removal_daemon removes old build logs every `log_removal_frequency`.
fn (mut app App) log_removal_daemon() {
fn (mut app App) log_removal_daemon(schedule CronExpression) {
mut start_time := time.Time{}
for {
@ -51,6 +52,12 @@ fn (mut app App) log_removal_daemon() {
app.linfo('Cleaned $counter logs ($failed failed)')
// Sleep until the next cycle
time.sleep(start_time.add_days(1) - time.now())
next_time := schedule.next_from_now() or {
app.lerror("Log removal daemon couldn't calculate next time: $err.msg(); fallback to $server.fallback_log_removal_frequency")
start_time.add(server.fallback_log_removal_frequency)
}
time.sleep(next_time - time.now())
}
}

View file

@ -55,6 +55,10 @@ pub fn server(conf Config) ! {
util.exit_with_message(1, 'Invalid global cron expression: $err.msg()')
}
log_removal_ce := expression.parse_expression(conf.log_removal_schedule) or {
util.exit_with_message(1, 'Invalid log removal cron expression: $err.msg()')
}
// Configure logger
log_level := log.level_from_tag(conf.log_level) or {
util.exit_with_message(1, 'Invalid log level. The allowed values are FATAL, ERROR, WARN, INFO & DEBUG.')
@ -109,7 +113,7 @@ pub fn server(conf Config) ! {
}
if conf.max_log_age > 0 {
go app.log_removal_daemon()
go app.log_removal_daemon(log_removal_ce)
}
web.run(app, conf.port)