in kong/kong/init.lua [605:763]
function Kong.init_worker()
local ctx = ngx.ctx
ctx.KONG_PHASE = PHASES.init_worker
math.randomseed()
kong.timer = _G.timerng
_G.timerng = nil
kong.timer:set_debug(kong.configuration.log_level == "debug")
kong.timer:start()
local ok, err = kong.db:init_worker()
if not ok then
stash_init_worker_error("failed to instantiate 'kong.db' module: " .. err)
return
end
if ngx.worker.id() == 0 then
if schema_state.missing_migrations then
ngx_log(ngx_WARN, "missing migrations: ",
list_migrations(schema_state.missing_migrations))
end
if schema_state.pending_migrations then
ngx_log(ngx_INFO, "starting with pending migrations: ",
list_migrations(schema_state.pending_migrations))
end
end
local worker_events, err = kong_global.init_worker_events()
if not worker_events then
stash_init_worker_error("failed to instantiate 'kong.worker_events' " ..
"module: " .. err)
return
end
kong.worker_events = worker_events
local cluster_events, err = kong_global.init_cluster_events(kong.configuration, kong.db)
if not cluster_events then
stash_init_worker_error("failed to instantiate 'kong.cluster_events' " ..
"module: " .. err)
return
end
kong.cluster_events = cluster_events
local cache, err = kong_global.init_cache(kong.configuration, cluster_events, worker_events)
if not cache then
stash_init_worker_error("failed to instantiate 'kong.cache' module: " ..
err)
return
end
kong.cache = cache
local core_cache, err = kong_global.init_core_cache(kong.configuration, cluster_events, worker_events)
if not core_cache then
stash_init_worker_error("failed to instantiate 'kong.core_cache' module: " ..
err)
return
end
kong.core_cache = core_cache
kong.db:set_events_handler(worker_events)
if kong.configuration.database == "off" then
local t = lmdb_txn.begin(1)
t:db_open(true)
ok, err = t:commit()
if not ok then
stash_init_worker_error("failed to create and open LMDB database: " .. err)
return
end
if not has_declarative_config(kong.configuration) and
declarative.get_current_hash() ~= nil then
ngx_log(ngx_INFO, "found persisted lmdb config, loading...")
local ok, err = declarative_init_build()
if not ok then
stash_init_worker_error("failed to initialize declarative config: " .. err)
return
end
elseif declarative_entities then
ok, err = load_declarative_config(kong.configuration,
declarative_entities,
declarative_meta)
if not ok then
stash_init_worker_error("failed to load declarative config file: " .. err)
return
end
else
local ok, err = declarative_init_build()
if not ok then
stash_init_worker_error("failed to initialize declarative config: " .. err)
return
end
end
end
local is_not_control_plane = kong.configuration.role ~= "control_plane"
if is_not_control_plane then
ok, err = execute_cache_warmup(kong.configuration)
if not ok then
ngx_log(ngx_ERR, "failed to warm up the DB cache: " .. err)
end
end
ok, err = runloop.update_plugins_iterator()
if not ok then
stash_init_worker_error("failed to build the plugins iterator: " .. err)
return
end
if is_not_control_plane then
ok, err = runloop.update_router()
if not ok then
stash_init_worker_error("failed to build the router: " .. err)
return
end
end
runloop.init_worker.before()
local plugins_iterator = runloop.get_plugins_iterator()
local errors = execute_init_worker_plugins_iterator(plugins_iterator, ctx)
if errors then
for _, e in ipairs(errors) do
local err = "failed to execute the \"init_worker\" " ..
"handler for plugin \"" .. e.plugin .."\": " .. e.err
stash_init_worker_error(err)
end
end
runloop.init_worker.after()
if is_not_control_plane and ngx.worker.id() == 0 then
plugin_servers.start()
end
if kong.clustering then
kong.clustering:init_worker()
end
end