Skip to content

Commit

Permalink
refactor: use hardcore wait time other than config
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyDluffy6017 committed Sep 8, 2022
1 parent 1502185 commit 2c521ae
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
12 changes: 4 additions & 8 deletions apisix/plugins/log-rotate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ local INTERVAL = 60 * 60 -- rotate interval (unit: second)
local MAX_KEPT = 24 * 7 -- max number of log files will be kept
local MAX_SIZE = -1 -- max size of file will be rotated
local COMPRESSION_FILE_SUFFIX = ".tar.gz" -- compression file suffix
local WAIT_TIME_BEFORE_COMPRESS = 0.5 -- wait time before compress
local rotate_time
local default_logs
local enable_compression = false
Expand Down Expand Up @@ -217,7 +216,7 @@ local function file_size(file)
end


local function rotate_file(files, now_time, max_kept, wait_time)
local function rotate_file(files, now_time, max_kept)
if core.table.isempty(files) then
return
end
Expand Down Expand Up @@ -245,7 +244,7 @@ local function rotate_file(files, now_time, max_kept, wait_time)
if enable_compression then
-- Waiting for nginx reopen files
-- to avoid losing logs during compression
ngx_sleep(wait_time)
ngx_sleep(0.5)

for _, new_file in ipairs(new_files) do
compression_file(new_file)
Expand All @@ -270,14 +269,11 @@ local function rotate()
local interval = INTERVAL
local max_kept = MAX_KEPT
local max_size = MAX_SIZE
-- for test
local wait_time = WAIT_TIME_BEFORE_COMPRESS
local attr = plugin.plugin_attr(plugin_name)
if attr then
interval = attr.interval or interval
max_kept = attr.max_kept or max_kept
max_size = attr.max_size or max_size
wait_time = attr.wait_time or wait_time
enable_compression = attr.enable_compression or enable_compression
end

Expand All @@ -303,7 +299,7 @@ local function rotate()

if now_time >= rotate_time then
local files = {DEFAULT_ACCESS_LOG_FILENAME, DEFAULT_ERROR_LOG_FILENAME}
rotate_file(files, now_time, max_kept, wait_time)
rotate_file(files, now_time, max_kept)

-- reset rotate time
rotate_time = rotate_time + interval
Expand All @@ -321,7 +317,7 @@ local function rotate()
core.table.insert(files, DEFAULT_ERROR_LOG_FILENAME)
end

rotate_file(files, now_time, max_kept, wait_time)
rotate_file(files, now_time, max_kept)
end
end

Expand Down
5 changes: 2 additions & 3 deletions t/plugin/log-rotate2.t
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ plugin_attr:
interval: 1
max_kept: 3
enable_compression: true
wait_time: 0
_EOC_

$block->set_value("yaml_config", $yaml_config);
Expand All @@ -62,7 +61,7 @@ __DATA__
location /t {
content_by_lua_block {
ngx.log(ngx.ERR, "start xxxxxx")
ngx.sleep(2.5)
ngx.sleep(3.5)
local has_split_access_file = false
local has_split_error_file = false
local lfs = require("lfs")
Expand Down Expand Up @@ -106,7 +105,7 @@ start xxxxxx
--- config
location /t {
content_by_lua_block {
ngx.sleep(2)
ngx.sleep(3)
local default_logs = {}
for file_name in lfs.dir(ngx.config.prefix() .. "/logs/") do
Expand Down

0 comments on commit 2c521ae

Please sign in to comment.