Hi!
I wanted a way to run a tailon server in my docker images only when needed. That's what superservers are for.
当社で基本的にdockerを使ってます。必要のときだけにtailonのサーバーを起動したかった。そのためにスパーサーバーって言うツールの種類があります。
inetd was painful to use so I made a simple one with HAProxy support for lua scripts.
普通のinetdがつい買いにくくてluaで自分のやつを作りました。
Haproxy conf sample
global
lua-load '/servers/haproxy/superserver.lua'
frontend tailon-frontend
bind :9090
tcp-request content set-var(txn.service_dir) str("/s6-supervised-servers/tailon")
tcp-request content lua.extend-life
default_backend tailon-backend
backend tailon-backend
server tailon-server localhost:9200
Superserver code
superserver = {}
superserver.version = "0.0.1"
--
-- Configuration
--
superserver.lifetime = 60
superserver.healthcheck_period = 3
Servers stay alive for 60s.
We check every 3s. So 20 ticks before dying!
--
-- State
--
superserver.expiracy_map = {}
For each server we store the number of ticks until death here.
--
-- ~Constant~
--
superserver.ticks_by_lifetime = superserver.lifetime / superserver.healthcheck_period
--
-- Server killing loop
--
superserver.shinigami_task = function()
while true do
for service_dir, ticks_to_live in pairs(superserver.expiracy_map) do
if ticks_to_live == 0 then
-- do nothing
elseif ticks_to_live == 1 then
core.log(core.info, "killing service " .. service_dir)
os.execute('s6-svc -d ' .. service_dir)
superserver.expiracy_map[service_dir] = 0
else
superserver.expiracy_map[service_dir] = ticks_to_live - 1
end
end
core.sleep(superserver.healthcheck_period)
end
end
We use the wonderful s6 tool to start and stop our servers!
--
-- Server launching and life extension
--
superserver.extend_life_action = function(txn)
-- core.log(core.info, "extend_life txn " .. txn)
local service_dir = txn.get_var(txn, 'txn.service_dir')
if service_dir then
core.log(core.info, "extending life for service_dir " .. service_dir)
local ticks_to_live = superserver.expiracy_map[service_dir] or 0
if ticks_to_live == 0 then
core.log(core.info, "starting service " .. service_dir)
os.execute('s6-svc -u ' .. service_dir)
end
superserver.expiracy_map[service_dir] = superserver.ticks_by_lifetime
else
core.log(core.warning, "failed to extend life, service_dir var not set.")
end
end
--
-- Register code in haproxy!
--
core.register_task(superserver.shinigami_task)
core.register_action('extend-life', {'tcp-req', 'tcp-res',
'http-req', 'http-res'},
superserver.extend_life_action)