Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/crowdsec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,10 @@ function csmod.SetupStream()
ngx.log(ngx.ERR, "Failed to query the stream: " .. err)
end
end
if ngx.worker.exiting() then
ngx.log(ngx.INFO, "worker is exiting, not setting up stream timer")
return
end
local ok, err = ngx.timer.at(runtime.conf["UPDATE_FREQUENCY"], SetupStreamTimer)
if not ok then
error("Failed to create the timer: " .. (err or "unknown"))
Expand Down
69 changes: 69 additions & 0 deletions t/26_stream_timer_worker_exiting.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use Test::Nginx::Socket 'no_plan';

master_on();
workers(1);
run_tests();

__DATA__

=== TEST 26: Stream refresh running when the worker exits

--- main_config
load_module /usr/share/nginx/modules/ndk_http_module.so;
load_module /usr/share/nginx/modules/ngx_http_lua_module.so;

--- http_config

lua_package_path './lib/?.lua;;';
lua_shared_dict crowdsec_cache 50m;
lua_shared_dict lapi_state 1m;

init_by_lua_block
{
cs = require "crowdsec"
local ok, err = cs.init("./t/conf_t/26_stream_timer_worker_exiting_crowdsec_nginx_bouncer.conf", "crowdsec-nginx-bouncer/v1.0.8")
if ok == nil then
ngx.log(ngx.ERR, "[Crowdsec] " .. err)
error()
end
}

init_worker_by_lua_block {
cs = require "crowdsec"
cs.SetupStream()
}

server {
listen 8081;

location = /v1/decisions/stream {
content_by_lua_block {
local args = ngx.req.get_uri_args()
if args.startup ~= "true" then
ngx.shared.lapi_state:set("refreshing", true)
ngx.sleep(1)
end
ngx.say('{"deleted": [], "new": []}')
}
}
}

--- config

location = /t {
content_by_lua_block {
for _ = 1, 100 do
if ngx.shared.lapi_state:get("refreshing") then
break
end
ngx.sleep(0.1)
end
ngx.print(tostring(ngx.shared.lapi_state:get("refreshing")))
}
}

--- request
GET /t
--- response_body: true
--- no_shutdown_error_log
Failed to create the timer
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
ENABLED=true
API_URL=http://127.0.0.1:8081
API_KEY=test_key
CACHE_EXPIRATION=1
BOUNCING_ON_TYPE=all
FALLBACK_REMEDIATION=ban
REQUEST_TIMEOUT=3000
UPDATE_FREQUENCY=1
MODE=stream
EXCLUDE_LOCATION=/v1/decisions/stream
BAN_TEMPLATE_PATH=./ban
REDIRECT_LOCATION=
RET_CODE=
CAPTCHA_PROVIDER=
SECRET_KEY=
SITE_KEY=
CAPTCHA_TEMPLATE_PATH=/var/lib/crowdsec/lua/templates/captcha.html
CAPTCHA_EXPIRATION=3600
Loading