From 486004de0247c3cb5964125822da5c689023884b Mon Sep 17 00:00:00 2001 From: Alexey Melnichuk Date: Thu, 26 Nov 2015 17:23:33 +0300 Subject: [PATCH 1/4] Fix. problem when same session can release task multiple times. It can be when originate has group dial-string. So it call `api_hangup_hook` for each channel. Now we release task in `retry.lua` only if originate success and this is same channel which execute `exec.lua`. If originate fail we release task `next.lua` --- resources/install/scripts/fax_queue/exec.lua | 6 ++++++ resources/install/scripts/fax_queue/next.lua | 13 ++++++------ resources/install/scripts/fax_queue/retry.lua | 15 ++++++++++---- resources/install/scripts/fax_queue/tasks.lua | 8 ++++++++ .../scripts/resources/functions/esl.lua | 20 ++++++++++++++----- 5 files changed, 47 insertions(+), 15 deletions(-) diff --git a/resources/install/scripts/fax_queue/exec.lua b/resources/install/scripts/fax_queue/exec.lua index 7fbb58d826..98de09adb7 100644 --- a/resources/install/scripts/fax_queue/exec.lua +++ b/resources/install/scripts/fax_queue/exec.lua @@ -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 diff --git a/resources/install/scripts/fax_queue/next.lua b/resources/install/scripts/fax_queue/next.lua index 0067d0e9b4..9e7ac0081f 100644 --- a/resources/install/scripts/fax_queue/next.lua +++ b/resources/install/scripts/fax_queue/next.lua @@ -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,13 @@ 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) + 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 diff --git a/resources/install/scripts/fax_queue/retry.lua b/resources/install/scripts/fax_queue/retry.lua index ef099546b7..d22f6df7ed 100644 --- a/resources/install/scripts/fax_queue/retry.lua +++ b/resources/install/scripts/fax_queue/retry.lua @@ -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' @@ -338,9 +339,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 diff --git a/resources/install/scripts/fax_queue/tasks.lua b/resources/install/scripts/fax_queue/tasks.lua index 920f1e9087..96f6d612bf 100644 --- a/resources/install/scripts/fax_queue/tasks.lua +++ b/resources/install/scripts/fax_queue/tasks.lua @@ -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')) diff --git a/resources/install/scripts/resources/functions/esl.lua b/resources/install/scripts/resources/functions/esl.lua index fcd4898754..f334be191c 100644 --- a/resources/install/scripts/resources/functions/esl.lua +++ b/resources/install/scripts/resources/functions/esl.lua @@ -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() From a485be3883729fc5fce55b99c23567942ff6aaab Mon Sep 17 00:00:00 2001 From: Alexey Melnichuk Date: Thu, 26 Nov 2015 17:25:15 +0300 Subject: [PATCH 2/4] Fix. Remove task in next.lua --- resources/install/scripts/fax_queue/next.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/resources/install/scripts/fax_queue/next.lua b/resources/install/scripts/fax_queue/next.lua index 9e7ac0081f..eaa8c0c790 100644 --- a/resources/install/scripts/fax_queue/next.lua +++ b/resources/install/scripts/fax_queue/next.lua @@ -51,6 +51,9 @@ local function next_task() if not ok then Tasks.release_task(task) + if task.status ~= 0 then + Tasks.remove_task(task) + end log.noticef("Error execute task: %s", tostring(err)) end From 5bc286d5e2caccda767048972d062ca4a2f366dc Mon Sep 17 00:00:00 2001 From: Alexey Melnichuk Date: Thu, 26 Nov 2015 17:30:56 +0300 Subject: [PATCH 3/4] Fix. Remove task in next.lua (Fix preview commit) --- resources/install/scripts/fax_queue/next.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/resources/install/scripts/fax_queue/next.lua b/resources/install/scripts/fax_queue/next.lua index eaa8c0c790..9d5c0adfff 100644 --- a/resources/install/scripts/fax_queue/next.lua +++ b/resources/install/scripts/fax_queue/next.lua @@ -41,6 +41,9 @@ local function next_task() 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)) @@ -51,9 +54,6 @@ local function next_task() if not ok then Tasks.release_task(task) - if task.status ~= 0 then - Tasks.remove_task(task) - end log.noticef("Error execute task: %s", tostring(err)) end From 990dd7b9ed44d85a44ecef5261d9f39f21c94189 Mon Sep 17 00:00:00 2001 From: Alexey Melnichuk Date: Thu, 26 Nov 2015 17:41:48 +0300 Subject: [PATCH 4/4] Add. log session uuid --- resources/install/scripts/fax_queue/retry.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/resources/install/scripts/fax_queue/retry.lua b/resources/install/scripts/fax_queue/retry.lua index d22f6df7ed..018cec372e 100644 --- a/resources/install/scripts/fax_queue/retry.lua +++ b/resources/install/scripts/fax_queue/retry.lua @@ -77,6 +77,7 @@ log.noticef([[<<< CALL RESULT >>> uuid: = '%s' + task_session_uuid: = '%s' answered: = '%s' fax_file: = '%s' wav_file: = '%s' @@ -91,6 +92,7 @@ fax_options = '%s' ]], tostring(uuid) , + tostring(fax_queue_task_session) , tostring(answered) , tostring(fax_file) , tostring(wav_file) ,