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:
Alexey Melnichuk 2016-07-26 20:58:37 +03:00 committed by FusionPBX
parent 32ef5c7203
commit d47151ba8d
4 changed files with 53 additions and 27 deletions

View File

@ -7,9 +7,8 @@ local Database = require "resources.functions.database"
local cache = require "resources.functions.cache"
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 shutdown_event = "CUSTOM::fusion::mwi::shutdown"
local vm_message_count do
@ -85,7 +84,7 @@ end
end
local events = EventConsumer.new(sleep, pid_file)
local events = EventConsumer.new(pid_file)
-- FS shutdown
events:bind("SHUTDOWN", function(self, name, event)
@ -93,13 +92,16 @@ events:bind("SHUTDOWN", function(self, name, event)
return self:stop()
end)
-- shutdown command
if shutdown_event then
events:bind(shutdown_event, function(self, name, event)
log.notice("shutdown")
-- Control commands from FusionPBX
events:bind("CUSTOM::fusion::service::" .. service_name, function(self, name, event)
local command = event:getHeader('service-command')
if command == "stop" then
log.notice("get stop command")
return self:stop()
end)
end
end
log.warningf('Unknown service command: %s', command or '<NONE>')
end)
-- MWI SUBSCRIBE
events:bind("MESSAGE_QUERY", function(self, name, event)

View File

@ -27,11 +27,10 @@ end
end
local sleep = 60000
local service_name = "flow"
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
events:bind("SHUTDOWN", function(self, name, event)
@ -39,13 +38,16 @@ events:bind("SHUTDOWN", function(self, name, event)
return self:stop()
end)
-- shutdown command
if shutdown_event then
events:bind(shutdown_event, function(self, name, event)
log.notice("shutdown")
-- Control commands from FusionPBX
events:bind("CUSTOM::fusion::service::" .. service_name, function(self, name, event)
local command = event:getHeader('service-command')
if command == "stop" then
log.notice("get stop command")
return self:stop()
end)
end
end
log.warningf('Unknown service command: %s', command or '<NONE>')
end)
-- FS receive SUBSCRIBE to BLF from device
events:bind("PRESENCE_PROBE", function(self, name, event)

View File

@ -432,10 +432,13 @@ local EventConsumer = class(EventEmitter) do
local default_timeout = 60000
local default_poll_interval = 60000 * 30
function EventConsumer:__init(timeout, pid_file)
function EventConsumer:__init(pid_file, timeout)
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
@ -444,19 +447,14 @@ function EventConsumer:__init(timeout, pid_file)
self._consumer = freeswitch.EventConsumer()
self._timeout = timeout
self._timers = TimeEvents.new()
self._pid = api:execute("create_uuid") or tostring(api:getTime())
self._pid_file = pid_file
if pid_file then
local pid_path = basename(self._pid_file)
mkdir(pid_path)
assert(file.write(self._pid_file, self._pid))
self._pid = api:execute("create_uuid") or tostring(api:getTime())
self._pid_file = pid_file
end
if self._timeout then
self:onInterval(self._timeout, function(self)
if not self:_check_pid_file() then return self:stop() end
self:emit('TIMEOUT')
end)
end
@ -479,6 +477,14 @@ function EventConsumer:_check_pid_file()
return true
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)
if not self._bound[event_name] then
local name, class = split_event(event_name)
@ -502,6 +508,8 @@ function EventConsumer:bind(event_name, cb)
end
function EventConsumer:_run()
self:_reset_pid_file()
self._running = true
-- set some huge default interval

View File

@ -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()