Skip to content

Commit

Permalink
feat(healthcheck) support setting the events module
Browse files Browse the repository at this point in the history
this is wip
  • Loading branch information
locao committed Jun 1, 2022
1 parent fb5150a commit 62413b0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
36 changes: 33 additions & 3 deletions lib/resty/healthcheck.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ local ipairs = ipairs
local cjson = require("cjson.safe").new()
local table_insert = table.insert
local table_remove = table.remove
local worker_events = require("resty.worker.events")
local resty_lock = require ("resty.lock")
local re_find = ngx.re.find
local bit = require("bit")
Expand All @@ -43,6 +42,11 @@ local ngx_worker_pid = ngx.worker.pid
local ssl = require("ngx.ssl")
local resty_timer = require "resty.timer"


local RESTY_EVENTS_VER = "0.1.0"
local RESTY_WORKER_EVENTS_VER = "0.3.3"


local new_tab
local nkeys
local is_array
Expand Down Expand Up @@ -83,6 +87,32 @@ do
end
end


local worker_events
--- This function loads the worker events module received as arg. It will throw
-- error() if it is not possible to load the module.
local function load_events_module(module_name)
if module_name == "resty.worker.events" then
worker_events = require("resty.worker.events")
assert(worker_events, "could not load lua-resty-worker-events")
assert(worker_events._VERSION == RESTY_WORKER_EVENTS_VER,
"unsupported lua-resty-worker-events version")
assert(worker_events.configured(), "please configure the " ..
"'lua-resty-worker-events' module before using 'lua-resty-healthcheck'")

elseif module_name == "resty.events" then
worker_events = require("resty.events.compat")
assert(worker_events, "could not load lua-resty-events")
assert(worker_events._VERSION == RESTY_EVENTS_VER,
"unsupported lua-resty-events version")

else
error("unknown events module")
end

end


-- constants
local EVENT_SOURCE_PREFIX = "lua-resty-healthcheck"
local LOG_PREFIX = "[healthcheck] "
Expand Down Expand Up @@ -1334,6 +1364,7 @@ local defaults = {
name = NO_DEFAULT,
shm_name = NO_DEFAULT,
type = NO_DEFAULT,
events_module = "resty.worker.events",
checks = {
active = {
type = "http",
Expand Down Expand Up @@ -1437,8 +1468,7 @@ end
-- @return checker object, or `nil + error`
function _M.new(opts)

assert(worker_events.configured(), "please configure the " ..
"'lua-resty-worker-events' module before using 'lua-resty-healthcheck'")
load_events_module(opts.events_module)

local active_type = (((opts or EMPTY).checks or EMPTY).active or EMPTY).type
local passive_type = (((opts or EMPTY).checks or EMPTY).passive or EMPTY).type
Expand Down
1 change: 0 additions & 1 deletion lua-resty-healthcheck-scm-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ description = {
homepage = "https://github.com/Kong/lua-resty-healthcheck"
}
dependencies = {
"lua-resty-worker-events == 1.0.0",
"penlight >= 1.9.2",
"lua-resty-timer ~> 1",
}
Expand Down

0 comments on commit 62413b0

Please sign in to comment.