Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: call custom ev_callback if configured #14

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions lib/resty/healthcheck.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1457,6 +1457,9 @@ function _M.new(opts)
end

self.ev_callback = function(data, event)
if opts.ev_callback then
opts.ev_callback(self.name, event, data)
end
-- just a wrapper to be able to access `self` as a closure
return self:event_handler(event, data.ip, data.port, data.hostname)
end
Expand Down
196 changes: 196 additions & 0 deletions t/ev_callback.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
use Test::Nginx::Socket::Lua 'no_plan';
use Cwd qw(cwd);

workers(1);

my $pwd = cwd();

our $HttpConfig = qq{
lua_package_path "$pwd/lib/?.lua;;";
lua_shared_dict test_shm 8m;
lua_shared_dict my_worker_events 8m;
};

no_shuffle();
run_tests();

__DATA__



=== TEST 1: healthy
--- http_config eval
qq{
$::HttpConfig

server {
listen 2116;
location = /status {
return 200;
}
}
}
--- config
location = /t {
content_by_lua_block {
local we = require "resty.worker.events"
assert(we.configure{ shm = "my_worker_events", interval = 0.1 })
local healthcheck = require("resty.healthcheck")
local checker = healthcheck.new({
ev_callback = function(name, event, data)
ngx.log(ngx.WARN, "name=", name)
ngx.log(ngx.WARN, "event=", event)
ngx.log(ngx.WARN, "ip=", data.ip)
ngx.log(ngx.WARN, "port=", data.port)
ngx.log(ngx.WARN, "hostname=", data.hostname)
end,
name = "testing",
shm_name = "test_shm",
type = "http",
checks = {
active = {
http_path = "/status",
healthy = {
interval = 0.1, -- we don't want active checks
successes = 1,
},
unhealthy = {
interval = 0.1, -- we don't want active checks
tcp_failures = 3,
http_failures = 3,
}
}
}
})
checker:add_target("127.0.0.1", 2116, nil, false)
ngx.sleep(3)
}
}
--- request
GET /t
--- error_log
name=testing
event=healthy
ip=127.0.0.1
port=2116
hostname=nil



=== TEST 2: unhealthy
--- http_config eval
qq{
$::HttpConfig

server {
listen 2116;
location = /status {
return 500;
}
}
}
--- config
location = /t {
content_by_lua_block {
local we = require "resty.worker.events"
assert(we.configure{ shm = "my_worker_events", interval = 0.1 })
local healthcheck = require("resty.healthcheck")
local checker = healthcheck.new({
ev_callback = function(name, event, data)
ngx.log(ngx.WARN, "name=", name)
ngx.log(ngx.WARN, "event=", event)
ngx.log(ngx.WARN, "ip=", data.ip)
ngx.log(ngx.WARN, "port=", data.port)
ngx.log(ngx.WARN, "hostname=", data.hostname)
end,
name = "testing",
shm_name = "test_shm",
type = "http",
checks = {
active = {
http_path = "/status",
healthy = {
interval = 0.1, -- we don't want active checks
successes = 1,
},
unhealthy = {
interval = 0.1, -- we don't want active checks
tcp_failures = 3,
http_failures = 1,
http_statuses = {500,400},
}
}
}
})
checker:add_target("127.0.0.1", 2116, nil, true)
ngx.sleep(3)
}
}
--- request
GET /t
--- error_log
name=testing
event=unhealthy
ip=127.0.0.1
port=2116
hostname=nil



=== TEST 3: clear
--- http_config eval
qq{
$::HttpConfig

server {
listen 2116;
location = /status {
return 500;
}
}
}
--- config
location = /t {
content_by_lua_block {
local we = require "resty.worker.events"
assert(we.configure{ shm = "my_worker_events", interval = 0.1 })
local healthcheck = require("resty.healthcheck")
local checker = healthcheck.new({
ev_callback = function(name, event, data)
ngx.log(ngx.WARN, "name=", name)
ngx.log(ngx.WARN, "event=", event)
ngx.log(ngx.WARN, "ip=", data.ip)
ngx.log(ngx.WARN, "port=", data.port)
ngx.log(ngx.WARN, "hostname=", data.hostname)
end,
name = "testing",
shm_name = "test_shm",
type = "http",
checks = {
active = {
http_path = "/status",
healthy = {
interval = 0.1, -- we don't want active checks
successes = 1,
},
unhealthy = {
interval = 0.1, -- we don't want active checks
tcp_failures = 3,
http_failures = 1,
http_statuses = {500,400},
}
}
}
})
checker:add_target("127.0.0.1", 2116, nil, true)
ngx.sleep(1)
checker:stop()
checker:clear()
ngx.sleep(1)
}
}
--- request
GET /t
--- error_log
name=testing
event=clear