2025-07-17 15:40:46 +04:00
|
|
|
local provider_loader = require("providers.provider_loader")
|
2025-02-18 17:39:10 +04:00
|
|
|
local schedule_reload_providers = provider_loader.schedule_reload_providers
|
|
|
|
|
|
2025-06-16 20:43:57 +04:00
|
|
|
-- Initialize auth configuration
|
2025-07-17 15:40:46 +04:00
|
|
|
local auth_config = require("auth.auth_config")
|
2025-06-16 20:43:57 +04:00
|
|
|
auth_config.init()
|
|
|
|
|
|
2025-09-02 13:25:54 +04:00
|
|
|
-- Initialize cache diagnostics
|
|
|
|
|
local cache = require("cache.cache")
|
|
|
|
|
|
|
|
|
|
-- Run cache diagnostics on startup (only in first worker)
|
|
|
|
|
if ngx.worker.id() == 0 then
|
|
|
|
|
pcall(cache.run_diagnostics)
|
|
|
|
|
end
|
|
|
|
|
|
2025-02-18 17:39:10 +04:00
|
|
|
-- Read URL from environment variable
|
|
|
|
|
local url = os.getenv("CONFIG_HEALTH_CHECKER_URL")
|
2025-07-18 16:47:35 +04:00
|
|
|
local fallback = "/app/providers.json"
|
2025-02-18 17:39:10 +04:00
|
|
|
|
|
|
|
|
-- Check worker ID to ensure timers only run in one process
|
|
|
|
|
if ngx.worker.id() == 0 then -- Only in first worker
|
|
|
|
|
ngx.log(ngx.INFO, "Starting reload_providers in worker: ", ngx.worker.id())
|
|
|
|
|
|
|
|
|
|
-- Perform initial provider loading
|
|
|
|
|
schedule_reload_providers(url, fallback)
|
|
|
|
|
|
|
|
|
|
-- Start periodic reload
|
|
|
|
|
local delay = tonumber(os.getenv("RELOAD_INTERVAL")) or 30
|
|
|
|
|
local handler
|
|
|
|
|
handler = function()
|
2025-07-21 19:57:50 +04:00
|
|
|
local ok, err = pcall(schedule_reload_providers, url, fallback)
|
2025-02-18 17:39:10 +04:00
|
|
|
if not ok then
|
2025-07-21 19:57:50 +04:00
|
|
|
ngx.log(ngx.ERR, "Failed to execute schedule_reload_providers: ", err)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Reschedule timer regardless of the result
|
|
|
|
|
local ok_timer, err_timer = ngx.timer.at(delay, handler)
|
|
|
|
|
if not ok_timer then
|
|
|
|
|
ngx.log(ngx.ERR, "Failed to reschedule timer: ", err_timer)
|
2025-02-18 17:39:10 +04:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local ok, err = ngx.timer.at(delay, handler)
|
|
|
|
|
if not ok then
|
|
|
|
|
ngx.log(ngx.ERR, "Failed to create initial timer: ", err)
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
ngx.log(ngx.ERR, "Worker ", ngx.worker.id(), " is not starting reload_providers")
|
|
|
|
|
end
|