refactor: some small changes before PR
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/man Pipeline was successful
ci/woodpecker/pr/docker Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
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/man Pipeline was successful
ci/woodpecker/pr/docker Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
This commit is contained in:
parent
b66d1161ed
commit
ab81eebd87
7 changed files with 31 additions and 34 deletions
|
|
@ -43,8 +43,6 @@ pub fn (c &Client) add_build_log(target_id int, start_time time.Time, end_time t
|
|||
}
|
||||
|
||||
// remove_build_log removes the build log with the given id from the server.
|
||||
pub fn (c &Client) remove_build_log(id int) !string {
|
||||
data := c.send_request<string>(.delete, '/api/v1/logs/$id', {})!
|
||||
|
||||
return data.data
|
||||
pub fn (c &Client) remove_build_log(id int) ! {
|
||||
c.send_request<string>(.delete, '/api/v1/logs/$id', {})!
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
module models
|
||||
|
||||
import time
|
||||
import os
|
||||
|
||||
pub struct BuildLog {
|
||||
pub mut:
|
||||
|
|
@ -28,6 +29,13 @@ pub fn (bl &BuildLog) str() string {
|
|||
return str
|
||||
}
|
||||
|
||||
// path returns the path to the log file, relative to the logs directory
|
||||
pub fn (bl &BuildLog) path() string {
|
||||
filename := bl.start_time.custom_format('YYYY-MM-DD_HH-mm-ss')
|
||||
|
||||
return os.join_path(bl.target_id.str(), bl.arch, filename)
|
||||
}
|
||||
|
||||
[params]
|
||||
pub struct BuildLogFilter {
|
||||
pub mut:
|
||||
|
|
|
|||
|
|
@ -86,7 +86,7 @@ fn (mut app App) v1_post_log() web.Result {
|
|||
}
|
||||
|
||||
// Store log in db
|
||||
log := BuildLog{
|
||||
mut log := BuildLog{
|
||||
target_id: target_id
|
||||
start_time: start_time
|
||||
end_time: end_time
|
||||
|
|
@ -95,25 +95,20 @@ fn (mut app App) v1_post_log() web.Result {
|
|||
}
|
||||
|
||||
// id of newly created log
|
||||
log_id := app.db.add_build_log(log)
|
||||
|
||||
repo_logs_dir := os.join_path(app.conf.data_dir, logs_dir_name, target_id.str(), arch)
|
||||
log.id = app.db.add_build_log(log)
|
||||
log_file_path := os.join_path(app.conf.data_dir, logs_dir_name, log.path())
|
||||
|
||||
// Create the logs directory of it doesn't exist
|
||||
if !os.exists(repo_logs_dir) {
|
||||
os.mkdir_all(repo_logs_dir) or {
|
||||
app.lerror("Couldn't create dir '$repo_logs_dir'.")
|
||||
if !os.exists(os.dir(log_file_path)) {
|
||||
os.mkdir_all(os.dir(log_file_path)) or {
|
||||
app.lerror('Error while creating log file: $err.msg()')
|
||||
|
||||
return app.status(.internal_server_error)
|
||||
}
|
||||
}
|
||||
|
||||
// Stream log contents to correct file
|
||||
file_name := start_time.custom_format('YYYY-MM-DD_HH-mm-ss')
|
||||
full_path := os.join_path_single(repo_logs_dir, file_name)
|
||||
|
||||
if length := app.req.header.get(.content_length) {
|
||||
util.reader_to_file(mut app.reader, length.int(), full_path) or {
|
||||
util.reader_to_file(mut app.reader, length.int(), log_file_path) or {
|
||||
app.lerror('An error occured while receiving logs: $err.msg()')
|
||||
|
||||
return app.status(.internal_server_error)
|
||||
|
|
@ -122,16 +117,14 @@ fn (mut app App) v1_post_log() web.Result {
|
|||
return app.status(.length_required)
|
||||
}
|
||||
|
||||
return app.json(.ok, new_data_response(log_id))
|
||||
return app.json(.ok, new_data_response(log.id))
|
||||
}
|
||||
|
||||
// v1_delete_log allows removing a build log from the system.
|
||||
['/api/v1/logs/:id'; auth; delete]
|
||||
fn (mut app App) v1_delete_log(id int) web.Result {
|
||||
log := app.db.get_build_log(id) or { return app.status(.not_found) }
|
||||
file_name := log.start_time.custom_format('YYYY-MM-DD_HH-mm-ss')
|
||||
full_path := os.join_path(app.conf.data_dir, logs_dir_name, log.target_id.str(), log.arch,
|
||||
file_name)
|
||||
full_path := os.join_path(app.conf.data_dir, logs_dir_name, log.path())
|
||||
|
||||
os.rm(full_path) or {
|
||||
app.lerror('Failed to remove log file $full_path: $err.msg()')
|
||||
|
|
|
|||
|
|
@ -28,11 +28,10 @@ fn (mut app App) log_removal_daemon(schedule CronExpression) {
|
|||
logs = app.db.get_build_logs(before: too_old_timestamp, offset: offset, limit: 50)
|
||||
|
||||
for log in logs {
|
||||
file_name := log.start_time.custom_format('YYYY-MM-DD_HH-mm-ss')
|
||||
full_path := os.join_path(app.conf.data_dir, logs_dir_name, log.target_id.str(),
|
||||
log.arch, file_name)
|
||||
os.rm(full_path) or {
|
||||
app.lerror('Failed to remove log file $full_path: $err.msg()')
|
||||
log_file_path := os.join_path(app.conf.data_dir, logs_dir_name, log.path())
|
||||
|
||||
os.rm(log_file_path) or {
|
||||
app.lerror('Failed to remove log file $log_file_path: $err.msg()')
|
||||
failed += 1
|
||||
|
||||
continue
|
||||
|
|
|
|||
Reference in a new issue