diff --git a/resources/install/scripts/app/voicemail/resources/scripts/mwi_subscribe.lua b/resources/install/scripts/app/voicemail/resources/scripts/mwi_subscribe.lua index 953ca152a3..cfff7c5286 100644 --- a/resources/install/scripts/app/voicemail/resources/scripts/mwi_subscribe.lua +++ b/resources/install/scripts/app/voicemail/resources/scripts/mwi_subscribe.lua @@ -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 '') +end) -- MWI SUBSCRIBE events:bind("MESSAGE_QUERY", function(self, name, event) diff --git a/resources/install/scripts/call_flow_subscribe.lua b/resources/install/scripts/call_flow_subscribe.lua index 6db71120b6..f5efd08826 100644 --- a/resources/install/scripts/call_flow_subscribe.lua +++ b/resources/install/scripts/call_flow_subscribe.lua @@ -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 '') +end) -- FS receive SUBSCRIBE to BLF from device events:bind("PRESENCE_PROBE", function(self, name, event) diff --git a/resources/install/scripts/resources/functions/event_consumer.lua b/resources/install/scripts/resources/functions/event_consumer.lua index b09b24b5bb..3ea7c944ad 100644 --- a/resources/install/scripts/resources/functions/event_consumer.lua +++ b/resources/install/scripts/resources/functions/event_consumer.lua @@ -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 diff --git a/resources/install/scripts/service b/resources/install/scripts/service new file mode 100644 index 0000000000..5b84002151 --- /dev/null +++ b/resources/install/scripts/service @@ -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()