EXAMPLES: add basic event_hdl lua example script

For now: basic server event handling from lua, events are printed to
STDOUT.

The idea behind this is to provide simple and easy-to-use examples
that serve as a basic introduction for lua event handler feature.
This commit is contained in:
Aurelien DARRAGON 2023-03-17 19:50:35 +01:00 committed by Christopher Faulet
parent 223770ddca
commit 620382af2f
2 changed files with 35 additions and 0 deletions

7
examples/lua/README Normal file
View File

@ -0,0 +1,7 @@
These files are example lua scripts that can be customized
if necessary.
They can be loaded with the 'lua-load' keyword like this:
lua-load /path/to/lua_script.lua

View File

@ -0,0 +1,28 @@
-- haproxy event-handling from Lua
--
-- This file serves as a demo to show you the various events that
-- can be handled directly from custom lua functions.
-- Events captured from lua will be printed directly to STDOUT
-- It may not be exhaustive, please refer to the lua documentation
-- in doc/lua-api/index.rst for up-to-date content and further explanations
-- subscribe to every SERVER family events, this is the equivalent of doing:
-- core.event_sub({"SERVER_ADD", "SERVER_DEL", "SERVER_UP", "SERVER_DOWN"}, ...)
core.event_sub({"SERVER"}, function(event, data)
-- This function will be called when:
-- - new server is added from the CLI (SERVER_ADD)
-- - existing server is removed from the CLI (SERVER_DEL)
-- - existing server state changes from UP to DOWN (SERVER_DOWN)
-- - existing server state changes from DOWN to UP (SERVER_UP)
-- If the server still exists at the time the function is called, data["reference"]
-- contains a valid reference to the lua server object related to the event
--
sv_status = data["reference"] ~= nil and data["reference"]:get_stats().status or "DELETED"
print("[DEBUG - FROM LUA]", "EventType." .. event .. ": " ..
"server " .. data["proxy_name"] .. "/" .. data["name"] .. " " ..
"is " .. sv_status)
end)
-- Please note that you may also use Server.event_sub() method to subscribe to events
-- relative to a specific server only. See the lua documentation for more information.
-- New event families will be added over time...