Spaces:
Sleeping
Sleeping
j
commited on
Commit
·
e559e5b
1
Parent(s):
843d438
fixed typos
Browse files
reascripts/ReaSpeech/source/ReaSpeechAPI.lua
CHANGED
@@ -28,117 +28,84 @@ function ReaSpeechAPI:get_curl_cmd()
|
|
28 |
end
|
29 |
|
30 |
function ReaSpeechAPI:fetch_json(url_path, http_method, error_handler, success_handler, timeout_handler, retry_count)
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
if not self.active_job then return end
|
110 |
-
local active_job = self.active_job
|
111 |
-
|
112 |
-
if active_job.request_output_file then
|
113 |
-
self:check_active_job_request_output_file()
|
114 |
-
end
|
115 |
-
|
116 |
-
if active_job.transcript_output_file then
|
117 |
-
self:check_active_job_transcript_output_file()
|
118 |
-
else
|
119 |
-
self:check_active_job_status()
|
120 |
-
end
|
121 |
-
end
|
122 |
-
|
123 |
-
local response_status, response_body = self.http_status_and_body(output)
|
124 |
-
|
125 |
-
if response_status >= 400 then
|
126 |
-
local msg = "Request failed with status " .. response_status
|
127 |
-
app:log(msg)
|
128 |
-
error_handler(msg)
|
129 |
-
return nil
|
130 |
-
end
|
131 |
-
|
132 |
-
local response_json = nil
|
133 |
-
if app:trap(function()
|
134 |
-
response_json = json.decode(response_body)
|
135 |
-
end) then
|
136 |
-
return response_json
|
137 |
-
else
|
138 |
-
app:log("JSON parse error")
|
139 |
-
app:log(output)
|
140 |
-
return nil
|
141 |
-
end
|
142 |
end
|
143 |
|
144 |
-- Requests data that may be large or time-consuming.
|
|
|
28 |
end
|
29 |
|
30 |
function ReaSpeechAPI:fetch_json(url_path, http_method, error_handler, success_handler, timeout_handler, retry_count)
|
31 |
+
http_method = http_method or 'GET'
|
32 |
+
error_handler = error_handler or function(_msg) end
|
33 |
+
success_handler = success_handler or function(_response) end
|
34 |
+
timeout_handler = timeout_handler or function() end
|
35 |
+
retry_count = retry_count or 0
|
36 |
+
local max_retries = 5
|
37 |
+
local retry_delay = 1 * (2 ^ retry_count) -- Exponential backoff
|
38 |
+
|
39 |
+
local curl = self:get_curl_cmd()
|
40 |
+
local api_url = self:get_api_url(url_path)
|
41 |
+
|
42 |
+
local http_method_argument = ""
|
43 |
+
if http_method ~= 'GET' then
|
44 |
+
http_method_argument = " -X " .. http_method
|
45 |
+
end
|
46 |
+
|
47 |
+
local command = table.concat({
|
48 |
+
curl,
|
49 |
+
' "', api_url, '"',
|
50 |
+
' -H "accept: application/json"',
|
51 |
+
http_method_argument,
|
52 |
+
' -m ', self.CURL_TIMEOUT_SECONDS,
|
53 |
+
' -s',
|
54 |
+
' -i',
|
55 |
+
' --http1.1',
|
56 |
+
' --retry 5',
|
57 |
+
})
|
58 |
+
|
59 |
+
app:debug('Fetch JSON: ' .. command)
|
60 |
+
|
61 |
+
local exec_result = (ExecProcess.new { command }):wait()
|
62 |
+
|
63 |
+
if exec_result == nil then
|
64 |
+
local msg = "Unable to run curl"
|
65 |
+
app:log(msg)
|
66 |
+
error_handler(msg)
|
67 |
+
return
|
68 |
+
end
|
69 |
+
|
70 |
+
local status, output = exec_result:match("(%d+)\n(.*)")
|
71 |
+
status = tonumber(status)
|
72 |
+
|
73 |
+
if status == 28 then
|
74 |
+
app:debug("Curl timeout reached")
|
75 |
+
timeout_handler()
|
76 |
+
return
|
77 |
+
elseif status ~= 0 then
|
78 |
+
local msg = "Curl failed with status " .. status
|
79 |
+
app:debug(msg)
|
80 |
+
error_handler(msg)
|
81 |
+
return
|
82 |
+
end
|
83 |
+
|
84 |
+
local response_status, response_body = self.http_status_and_body(output)
|
85 |
+
|
86 |
+
if response_status >= 500 and retry_count < max_retries then
|
87 |
+
app:debug("Got 500 error, retrying in " .. retry_delay .. " seconds. Retry " .. (retry_count + 1) .. " of " .. max_retries)
|
88 |
+
reaper.defer(function()
|
89 |
+
self:fetch_json(url_path, http_method, error_handler, success_handler, timeout_handler, retry_count + 1)
|
90 |
+
end)
|
91 |
+
return
|
92 |
+
elseif response_status >= 400 then
|
93 |
+
local msg = "Request failed with status " .. response_status
|
94 |
+
app:log(msg)
|
95 |
+
error_handler(msg)
|
96 |
+
return
|
97 |
+
end
|
98 |
+
|
99 |
+
local response_json = nil
|
100 |
+
if app:trap(function()
|
101 |
+
response_json = json.decode(response_body)
|
102 |
+
end) then
|
103 |
+
success_handler(response_json)
|
104 |
+
else
|
105 |
+
app:log("JSON parse error")
|
106 |
+
app:log(output)
|
107 |
+
error_handler("JSON parse error")
|
108 |
+
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
end
|
110 |
|
111 |
-- Requests data that may be large or time-consuming.
|
reascripts/ReaSpeech/source/ReaSpeechWorker.lua
CHANGED
@@ -321,7 +321,7 @@ function ReaSpeechWorker:handle_response_json(output_file, sentinel_file, succes
|
|
321 |
fail_f("Error parsing response JSON")
|
322 |
return
|
323 |
end
|
324 |
-
--
|
325 |
Tempfile:remove(output_file)
|
326 |
Tempfile:remove(sentinel_file)
|
327 |
end
|
|
|
321 |
fail_f("Error parsing response JSON")
|
322 |
return
|
323 |
end
|
324 |
+
-- remove tempfiles only on success, moved for debugging purposes only!
|
325 |
Tempfile:remove(output_file)
|
326 |
Tempfile:remove(sentinel_file)
|
327 |
end
|