forked from vieter-v/vieter
feat(server): use cron schedule for log removal instead
This commit is contained in:
parent
09c61143b0
commit
26796f2228
7 changed files with 75 additions and 21 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue