Add `service` script and make timout optional for EventConsumer ctor (#1775)
* Change. Pass pid file first. Because there really no need pass timeout. Add. `fsc` script to be able shutdown MWI and Call Flow subscribe services. * Change. rename `fsc` to `service`. * Change. Use `stop` command instead of `shutdown`
This commit is contained in:
parent
32ef5c7203
commit
d47151ba8d
|
|
@ -7,9 +7,8 @@ local Database = require "resources.functions.database"
|
||||||
local cache = require "resources.functions.cache"
|
local cache = require "resources.functions.cache"
|
||||||
local mwi_notify = require "app.voicemail.resources.functions.mwi_notify"
|
local mwi_notify = require "app.voicemail.resources.functions.mwi_notify"
|
||||||
|
|
||||||
local sleep = 60000
|
local service_name = "mwi"
|
||||||
local pid_file = scripts_dir .. "/run/mwi_subscribe.tmp"
|
local pid_file = scripts_dir .. "/run/mwi_subscribe.tmp"
|
||||||
local shutdown_event = "CUSTOM::fusion::mwi::shutdown"
|
|
||||||
|
|
||||||
local vm_message_count do
|
local vm_message_count do
|
||||||
|
|
||||||
|
|
@ -85,7 +84,7 @@ end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local events = EventConsumer.new(sleep, pid_file)
|
local events = EventConsumer.new(pid_file)
|
||||||
|
|
||||||
-- FS shutdown
|
-- FS shutdown
|
||||||
events:bind("SHUTDOWN", function(self, name, event)
|
events:bind("SHUTDOWN", function(self, name, event)
|
||||||
|
|
@ -93,13 +92,16 @@ events:bind("SHUTDOWN", function(self, name, event)
|
||||||
return self:stop()
|
return self:stop()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
-- shutdown command
|
-- Control commands from FusionPBX
|
||||||
if shutdown_event then
|
events:bind("CUSTOM::fusion::service::" .. service_name, function(self, name, event)
|
||||||
events:bind(shutdown_event, function(self, name, event)
|
local command = event:getHeader('service-command')
|
||||||
log.notice("shutdown")
|
if command == "stop" then
|
||||||
|
log.notice("get stop command")
|
||||||
return self:stop()
|
return self:stop()
|
||||||
end)
|
end
|
||||||
end
|
|
||||||
|
log.warningf('Unknown service command: %s', command or '<NONE>')
|
||||||
|
end)
|
||||||
|
|
||||||
-- MWI SUBSCRIBE
|
-- MWI SUBSCRIBE
|
||||||
events:bind("MESSAGE_QUERY", function(self, name, event)
|
events:bind("MESSAGE_QUERY", function(self, name, event)
|
||||||
|
|
|
||||||
|
|
@ -27,11 +27,10 @@ end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local sleep = 60000
|
local service_name = "flow"
|
||||||
local pid_file = scripts_dir .. "/run/call_flow_subscribe.tmp"
|
local pid_file = scripts_dir .. "/run/call_flow_subscribe.tmp"
|
||||||
local shutdown_event = "CUSTOM::fusion::flow::shutdown"
|
|
||||||
|
|
||||||
local events = EventConsumer.new(sleep, pid_file)
|
local events = EventConsumer.new(pid_file)
|
||||||
|
|
||||||
-- FS shutdown
|
-- FS shutdown
|
||||||
events:bind("SHUTDOWN", function(self, name, event)
|
events:bind("SHUTDOWN", function(self, name, event)
|
||||||
|
|
@ -39,13 +38,16 @@ events:bind("SHUTDOWN", function(self, name, event)
|
||||||
return self:stop()
|
return self:stop()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
-- shutdown command
|
-- Control commands from FusionPBX
|
||||||
if shutdown_event then
|
events:bind("CUSTOM::fusion::service::" .. service_name, function(self, name, event)
|
||||||
events:bind(shutdown_event, function(self, name, event)
|
local command = event:getHeader('service-command')
|
||||||
log.notice("shutdown")
|
if command == "stop" then
|
||||||
|
log.notice("get stop command")
|
||||||
return self:stop()
|
return self:stop()
|
||||||
end)
|
end
|
||||||
end
|
|
||||||
|
log.warningf('Unknown service command: %s', command or '<NONE>')
|
||||||
|
end)
|
||||||
|
|
||||||
-- FS receive SUBSCRIBE to BLF from device
|
-- FS receive SUBSCRIBE to BLF from device
|
||||||
events:bind("PRESENCE_PROBE", function(self, name, event)
|
events:bind("PRESENCE_PROBE", function(self, name, event)
|
||||||
|
|
|
||||||
|
|
@ -432,10 +432,13 @@ local EventConsumer = class(EventEmitter) do
|
||||||
local default_timeout = 60000
|
local default_timeout = 60000
|
||||||
local default_poll_interval = 60000 * 30
|
local default_poll_interval = 60000 * 30
|
||||||
|
|
||||||
function EventConsumer:__init(timeout, pid_file)
|
function EventConsumer:__init(pid_file, timeout)
|
||||||
self.__base.__init(self)
|
self.__base.__init(self)
|
||||||
|
|
||||||
if pid_file then timeout = timeout or default_timeout end
|
if pid_file then
|
||||||
|
assert(type(pid_file) == 'string')
|
||||||
|
timeout = timeout or default_timeout
|
||||||
|
end
|
||||||
|
|
||||||
if timeout then assert(timeout > 0) end
|
if timeout then assert(timeout > 0) end
|
||||||
|
|
||||||
|
|
@ -444,19 +447,14 @@ function EventConsumer:__init(timeout, pid_file)
|
||||||
self._consumer = freeswitch.EventConsumer()
|
self._consumer = freeswitch.EventConsumer()
|
||||||
self._timeout = timeout
|
self._timeout = timeout
|
||||||
self._timers = TimeEvents.new()
|
self._timers = TimeEvents.new()
|
||||||
self._pid = api:execute("create_uuid") or tostring(api:getTime())
|
|
||||||
self._pid_file = pid_file
|
|
||||||
|
|
||||||
if pid_file then
|
if pid_file then
|
||||||
local pid_path = basename(self._pid_file)
|
self._pid = api:execute("create_uuid") or tostring(api:getTime())
|
||||||
mkdir(pid_path)
|
self._pid_file = pid_file
|
||||||
assert(file.write(self._pid_file, self._pid))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if self._timeout then
|
if self._timeout then
|
||||||
self:onInterval(self._timeout, function(self)
|
self:onInterval(self._timeout, function(self)
|
||||||
if not self:_check_pid_file() then return self:stop() end
|
if not self:_check_pid_file() then return self:stop() end
|
||||||
self:emit('TIMEOUT')
|
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -479,6 +477,14 @@ function EventConsumer:_check_pid_file()
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function EventConsumer:_reset_pid_file()
|
||||||
|
if self._pid_file then
|
||||||
|
local pid_path = basename(self._pid_file)
|
||||||
|
mkdir(pid_path)
|
||||||
|
assert(file.write(self._pid_file, self._pid))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function EventConsumer:bind(event_name, cb)
|
function EventConsumer:bind(event_name, cb)
|
||||||
if not self._bound[event_name] then
|
if not self._bound[event_name] then
|
||||||
local name, class = split_event(event_name)
|
local name, class = split_event(event_name)
|
||||||
|
|
@ -502,6 +508,8 @@ function EventConsumer:bind(event_name, cb)
|
||||||
end
|
end
|
||||||
|
|
||||||
function EventConsumer:_run()
|
function EventConsumer:_run()
|
||||||
|
self:_reset_pid_file()
|
||||||
|
|
||||||
self._running = true
|
self._running = true
|
||||||
|
|
||||||
-- set some huge default interval
|
-- set some huge default interval
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
-- File to conrol FusionPBX Lua services/monitors
|
||||||
|
-- @usage:
|
||||||
|
-- # stop `call_flow_subscribe` monitor
|
||||||
|
-- fs_cli -x "lua service flow stop"
|
||||||
|
-- # stop `mwi_subscribe` monitor
|
||||||
|
-- fs_cli -x "lua service mwi stop"
|
||||||
|
|
||||||
|
local destination = assert(argv[1], "No service name")
|
||||||
|
local command = assert(argv[2], "No command")
|
||||||
|
|
||||||
|
local event = freeswitch.Event("CUSTOM", "fusion::service::" .. destination);
|
||||||
|
event:addHeader('service-command', command)
|
||||||
|
|
||||||
|
event:fire()
|
||||||
Loading…
Reference in New Issue