Merge pull request #1258 from moteus/fax_queue

Fix. problem when same session can release task multiple times.
This commit is contained in:
FusionPBX 2015-11-26 08:21:07 -08:00
commit a76dee8130
5 changed files with 52 additions and 15 deletions

View File

@ -132,6 +132,12 @@ local function check()
end
local function task()
local session_uuid = session:getVariable('uuid')
session:setVariable('fax_queue_task_session', session_uuid)
log.infof("SESSION UUID: %s", session_uuid)
session:waitForAnswer(session)
while not session:answered() do

View File

@ -28,10 +28,6 @@ local function next_task()
local esl
local ok, err = pcall(function()
for k, v in pairs(task) do
print(string.format(" `%s` => `%s`", tostring(k), tostring(v)))
end
local mode = (task.retry_counter % #FAX_OPTIONS) + 1
local dial_string = '{' ..
task.dial_string .. "api_hangup_hook='lua fax_queue/retry.lua'," ..
@ -42,8 +38,16 @@ local function next_task()
log.notice(originate)
esl = assert(Esl.new())
local ok, err = esl:api(originate)
log.notice(ok or err)
local ok, status, info = esl:api(originate)
if not ok then
Tasks.wait_task(task, false, info)
if task.status ~= 0 then
Tasks.remove_task(task)
end
log.noticef('Can not originate to `%s` cause: %s: %s ', task.uri, tostring(status), tostring(info))
else
log.noticef("originate successfuly: %s", tostring(info))
end
end)
if esl then esl:close() end

View File

@ -10,7 +10,7 @@
local Tasks = require "fax_queue.tasks"
local fax_task_uuid = env:getHeader('fax_task_uuid')
local task = Tasks.select_task(fax_task_uuid)
local task = Tasks.select_task(fax_task_uuid)
if not task then
log.warningf("Can not find fax task: %q", tostring(fax_task_uuid))
return
@ -25,6 +25,7 @@
-- Channel/FusionPBX variables
local uuid = env:getHeader("uuid")
local fax_queue_task_session = env:getHeader('fax_queue_task_session')
local domain_uuid = env:getHeader("domain_uuid") or task.domain_uuid
local domain_name = env:getHeader("domain_name") or task.domain_name
local origination_caller_id_name = env:getHeader("origination_caller_id_name") or '000000000000000'
@ -76,6 +77,7 @@
log.noticef([[<<< CALL RESULT >>>
uuid: = '%s'
task_session_uuid: = '%s'
answered: = '%s'
fax_file: = '%s'
wav_file: = '%s'
@ -90,6 +92,7 @@
fax_options = '%s'
]],
tostring(uuid) ,
tostring(fax_queue_task_session) ,
tostring(answered) ,
tostring(fax_file) ,
tostring(wav_file) ,
@ -338,9 +341,15 @@
end
end
Tasks.wait_task(task, answered, hangup_cause_q850)
if task.status ~= 0 then
Tasks.remove_task(task)
-- if task use group call then retry.lua will be called multiple times
-- here we check eathre that channel which execute `exec.lua`
-- Note that if there no one execute `exec.lua` we do not need call this
-- becase it should deal in `next.lua`
if fax_queue_task_session == uuid then
Tasks.wait_task(task, answered, hangup_cause_q850)
if task.status ~= 0 then
Tasks.remove_task(task)
end
end
end

View File

@ -103,6 +103,14 @@ local remove_finished_tasks_sql = [[
delete from v_fax_tasks where task_status > 3
]]
local function serialize(task, header)
local str = header or ''
for k, v in pairs(task) do
str = str .. ('\n %q = %q'):format(tostring(k), tostring(v))
end
return str
end
local function get_db()
if not db then
db = assert(Database.new('system'))

View File

@ -117,7 +117,11 @@ function EventSocket:api(cmd)
local event, err = self:_request('api ' .. cmd)
if not event then return nil, err end
local body = event:getBody()
if body then return body end
if body then
local ok, status, msg = split_status(body)
if ok == nil then return body end
return ok, status, msg
end
return event:getReply()
end
@ -134,6 +138,13 @@ if freeswitch then
local api
-- [+-][OK|ERR|USAGE|...][Message]
local function split_status(str)
local ok, status, msg = string.match(str, "^%s*([-+])([^%s]+)%s*(.-)%s*$")
if not ok then return nil, str end
return ok == '+', status, msg
end
function EventSocket:__init()
self._api = api or freeswitch.API()
api = self._api
@ -142,10 +153,9 @@ end
function EventSocket:api(cmd)
local result = self._api:executeString(cmd)
if result and result:sub(1, 4) == '-ERR' then
return nil, result:sub(5)
end
return result
local ok, status, msg = split_status(result)
if ok == nil then return result end
return ok, status, msg
end
function EventSocket:close()