mirror of
https://github.com/fusionpbx/fusionpbx.git
synced 2026-01-06 11:43:50 +00:00
Add. EventConsumer class. (#1741)
Rewrite MWI and CallFlow subscribe handlers based on EventConsumer class.
Also on my test VirtualBox/Debian system Lua function `os.clock` produce
very strange result(delta ~0.015 for 1 second) so I switch to `os.time`.
Now to to stop this background Lua scripts it possible send CUSTOM event
with subclass `fusion::XXX::shutdown`. Where XXX is `mwi` or `flow`.
Usage of EventConsumer class
```Lua
-- create new object with timeout one minute
local events = EventConsumer.new(60000)
-- bind to some FS event
events:bind("SHUTDOW", function(self, name, event) ... end)
-- bind to another FS event with subclass
events:bind("CUSTOM::fusion::mwi::shutdown", function(self, name, event) ... end)
-- handle timeout event
events:on("TIMEOUT", function(self, name) ... end)
--run event loop
events:run()
```
This commit is contained in:
committed by
FusionPBX
parent
1094a3fe92
commit
a52e9c3626
@@ -2,33 +2,36 @@ require "resources.functions.config"
|
||||
require "resources.functions.split"
|
||||
|
||||
local log = require "resources.functions.log".mwi_subscribe
|
||||
local file = require "resources.functions.file"
|
||||
local EventConsumer = require "resources.functions.event_consumer"
|
||||
local Database = require "resources.functions.database"
|
||||
local ievents = require "resources.functions.ievents"
|
||||
local IntervalTimer = require "resources.functions.interval_timer"
|
||||
local cache = require "resources.functions.cache"
|
||||
local api = require "resources.functions.api"
|
||||
local mwi_notify = require "app.voicemail.resources.functions.mwi_notify"
|
||||
|
||||
local sleep = 60000
|
||||
local pid_file = scripts_dir .. "/run/mwi_subscribe.tmp"
|
||||
local shutdown_event = "CUSTOM::fusion::mwi::shutdown"
|
||||
|
||||
local vm_message_count do
|
||||
|
||||
local vm_to_uuid_sql = [[SELECT v.voicemail_uuid
|
||||
FROM v_voicemails as v inner join v_domains as d on v.domain_uuid = d.domain_uuid
|
||||
WHERE v.voicemail_id = '%s' and d.domain_name = '%s']]
|
||||
|
||||
local vm_messages_sql = [[SELECT
|
||||
( SELECT count(*)
|
||||
FROM v_voicemail_messages
|
||||
WHERE voicemail_uuid = %s
|
||||
AND (message_status is null or message_status = '')
|
||||
FROM v_voicemail_messages
|
||||
WHERE voicemail_uuid = %s
|
||||
AND (message_status is null or message_status = '')
|
||||
) as new_messages,
|
||||
|
||||
( SELECT count(*)
|
||||
FROM v_voicemail_messages
|
||||
WHERE voicemail_uuid = %s
|
||||
AND message_status = 'saved'
|
||||
FROM v_voicemail_messages
|
||||
WHERE voicemail_uuid = %s
|
||||
AND message_status = 'saved'
|
||||
) as saved_messages
|
||||
]]
|
||||
|
||||
local function vm_message_count(account, use_cache)
|
||||
function vm_message_count(account, use_cache)
|
||||
local id, domain_name = split_first(account, '@', true)
|
||||
if not domain_name then return end
|
||||
|
||||
@@ -80,52 +83,48 @@ local function vm_message_count(account, use_cache)
|
||||
return row.new_messages, row.saved_messages
|
||||
end
|
||||
|
||||
local sleep = 60000
|
||||
local pid_file = scripts_dir .. "/run/mwi_subscribe.tmp"
|
||||
local pid = api:execute("create_uuid") or tostring(api:getTime())
|
||||
file.write(pid_file, pid)
|
||||
|
||||
log.notice("start");
|
||||
|
||||
local timer = IntervalTimer.new(sleep):start()
|
||||
|
||||
for event in ievents({"MESSAGE_QUERY", "SHUTDOWN"}, 1, timer:rest()) do
|
||||
if (not event) or (timer:rest() < 1000) then
|
||||
if not file.exists(pid_file) then break end
|
||||
local stored = file.read(pid_file)
|
||||
if stored and stored ~= pid then break end
|
||||
timer:restart()
|
||||
end
|
||||
|
||||
if event then
|
||||
-- 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')
|
||||
if account_header then
|
||||
local proto, account = split_first(account_header, ':', true)
|
||||
|
||||
if (not account) or (proto ~= 'sip' and proto ~= 'sips') then
|
||||
log.warningf("invalid format for voicemail id: %s", account_header)
|
||||
else
|
||||
local new_messages, saved_messages = vm_message_count(account)
|
||||
if not new_messages then
|
||||
log.warningf('can not find voicemail: %s', account)
|
||||
else
|
||||
log.noticef('voicemail %s has %s/%s messages', account, new_messages, saved_messages)
|
||||
mwi_notify(account, new_messages, saved_messages)
|
||||
end
|
||||
end
|
||||
end
|
||||
elseif event_name == 'SHUTDOWN' then
|
||||
log.notice("shutdown")
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if file.read(pid_file) == pid then
|
||||
file.remove(pid_file)
|
||||
local events = EventConsumer.new(sleep, pid_file)
|
||||
|
||||
-- FS shutdown
|
||||
events:bind("SHUTDOWN", function(self, name, event)
|
||||
log.notice("shutdown")
|
||||
return self:stop()
|
||||
end)
|
||||
|
||||
-- shutdown command
|
||||
if shutdown_event then
|
||||
events:bind(shutdown_event, function(self, name, event)
|
||||
log.notice("shutdown")
|
||||
return self:stop()
|
||||
end)
|
||||
end
|
||||
|
||||
-- MWI SUBSCRIBE
|
||||
events:bind("MESSAGE_QUERY", function(self, name, event)
|
||||
local account_header = event:getHeader('Message-Account')
|
||||
if not account_header then
|
||||
return log.warningf("MWI message without `Message-Account` header")
|
||||
end
|
||||
|
||||
local proto, account = split_first(account_header, ':', true)
|
||||
|
||||
if (not account) or (proto ~= 'sip' and proto ~= 'sips') then
|
||||
return log.warningf("invalid format for voicemail id: %s", account_header)
|
||||
end
|
||||
|
||||
local new_messages, saved_messages = vm_message_count(account)
|
||||
if not new_messages then
|
||||
return log.warningf('can not find voicemail: %s', account)
|
||||
end
|
||||
|
||||
log.noticef('voicemail %s has %s/%s message(s)', account, new_messages, saved_messages)
|
||||
mwi_notify(account, new_messages, saved_messages)
|
||||
end)
|
||||
|
||||
log.notice("start")
|
||||
|
||||
events:run()
|
||||
|
||||
log.notice("stop")
|
||||
|
||||
Reference in New Issue
Block a user