Add. Support multiple events. (#1736)

Now subscriber stops and remove pid files when FS shutdown.
So if you use Status->Services you will see correct status.
This commit is contained in:
Alexey Melnichuk 2016-07-06 21:37:16 +03:00 committed by FusionPBX
parent e6567d6d8d
commit 3d1f5b6866
3 changed files with 62 additions and 29 deletions

View File

@ -89,7 +89,7 @@ log.notice("start");
local timer = IntervalTimer.new(sleep):start() local timer = IntervalTimer.new(sleep):start()
for event in ievents("MESSAGE_QUERY", 1, timer:rest()) do for event in ievents({"MESSAGE_QUERY", "SHUTDOWN"}, 1, timer:rest()) do
if (not event) or (timer:rest() < 1000) then if (not event) or (timer:rest() < 1000) then
if not file.exists(pid_file) then break end if not file.exists(pid_file) then break end
local stored = file.read(pid_file) local stored = file.read(pid_file)
@ -99,6 +99,8 @@ for event in ievents("MESSAGE_QUERY", 1, timer:rest()) do
if event then if event then
-- log.notice("event:" .. event:serialize("xml")); -- log.notice("event:" .. event:serialize("xml"));
local event_name = event:getHeader('Event-Name')
if event_name == 'MESSAGE_QUERY' then
local account_header = event:getHeader('Message-Account') local account_header = event:getHeader('Message-Account')
if account_header then if account_header then
local proto, account = split_first(account_header, ':', true) local proto, account = split_first(account_header, ':', true)
@ -115,7 +117,15 @@ for event in ievents("MESSAGE_QUERY", 1, timer:rest()) do
end end
end end
end end
elseif event_name == 'SHUTDOWN' then
log.notice("shutdown")
break
end
end end
end end
if file.read(pid_file) == pid then
file.remove(pid_file)
end
log.notice("stop") log.notice("stop")

View File

@ -33,11 +33,11 @@ local pid = api:execute("create_uuid") or tostring(api:getTime())
file.write(pid_file, pid) file.write(pid_file, pid)
log.notice("start call_flow_subscribe"); log.notice("start");
local timer = IntervalTimer.new(sleep):start() local timer = IntervalTimer.new(sleep):start()
for event in ievents("PRESENCE_PROBE", 1, timer:rest()) do for event in ievents({"PRESENCE_PROBE", "SHUTDOWN"}, 1, timer:rest()) do
if (not event) or (timer:rest() < 1000) then if (not event) or (timer:rest() < 1000) then
if not file.exists(pid_file) then break end if not file.exists(pid_file) then break end
local stored = file.read(pid_file) local stored = file.read(pid_file)
@ -47,6 +47,8 @@ for event in ievents("PRESENCE_PROBE", 1, timer:rest()) do
if event then if event then
-- log.notice("event:" .. event:serialize("xml")); -- log.notice("event:" .. event:serialize("xml"));
local event_name = event:getHeader('Event-Name')
if event_name == 'PRESENCE_PROBE' then
if event:getHeader('proto') == 'flow' and if event:getHeader('proto') == 'flow' and
event:getHeader('Event-Calling-Function') == 'sofia_presence_handle_sip_i_subscribe' event:getHeader('Event-Calling-Function') == 'sofia_presence_handle_sip_i_subscribe'
then then
@ -64,7 +66,15 @@ for event in ievents("PRESENCE_PROBE", 1, timer:rest()) do
log.noticef("%s UNSUBSCRIBE from %s", from, to) log.noticef("%s UNSUBSCRIBE from %s", from, to)
end end
end end
elseif event_name == 'SHUTDOWN' then
log.notice("shutdown")
break
end
end end
end end
log.notice("stop call_flow_subscribe") if file.read(pid_file) == pid then
file.remove(pid_file)
end
log.notice("stop")

View File

@ -1,10 +1,23 @@
local ievents = function(events, ...) local ievents = function(events, ...)
if type(events) == 'string' then if type(events) == 'string' then
events = freeswitch.EventConsumer(events) events = freeswitch.EventConsumer(events)
elseif type(events) == 'table' then
local array = events
events = freeswitch.EventConsumer()
for _, event in ipairs(array) do
local base, sub
if type(event) == 'table' then
base, sub = event[1], event[2]
else
base = event
end
if not sub then events:bind(base)
else events:bind(base, sub) end
end
end end
local block, timeout = ... local block, timeout = ...
if timeout == 0 then block, timeout = 0, 0 end if timeout and (timeout == 0) then block, timeout = 0, 0 end
timeout = timeout or 0 timeout = timeout or 0
return function() return function()