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
- 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
-
37
- local max_retries = 5
38
- local retry_delay = 1 * (2 ^ retry_count) -- Exponential backoff
39
-
40
- local curl = self:get_curl_cmd()
41
- local api_url = self:get_api_url(url_path)
42
- local http_method_argument = ""
43
- if http_method ~= 'GET' then
44
- http_method_argument = " -X " .. http_method
45
- end
46
- local command = table.concat({
47
- curl,
48
- ' "', api_url, '"',
49
- ' -H "accept: application/json"',
50
- http_method_argument,
51
- ' -m ', self.CURL_TIMEOUT_SECONDS,
52
- ' -s',
53
- ' -i',
54
- ' --http1.1',
55
- ' --retry 5',
56
- })
57
- app:debug('Fetch JSON: ' .. command)
58
-
59
- local exec_result = (ExecProcess.new { command }):wait()
60
- if exec_result == nil then
61
- local msg = "Unable to run curl"
62
- app:log(msg)
63
- error_handler(msg)
64
- return
65
- end
66
-
67
- local status, output = exec_result:match("(%d+)\n(.*)")
68
- status = tonumber(status)
69
-
70
- if status == 28 then
71
- app:debug("Curl timeout reached")
72
- timeout_handler()
73
- return
74
- elseif status ~= 0 then
75
- local msg = "Curl failed with status " .. status
76
- app:debug(msg)
77
- error_handler(msg)
78
- return
79
- end
80
-
81
- local response_status, response_body = self.http_status_and_body(output)
82
-
83
- if response_status >= 500 and retry_count < max_retries then
84
- app:debug("Got 500 error, retrying in " .. retry_delay .. " seconds. Retry " .. (retry_count + 1) .. " of " .. max_retries)
85
- reaper.defer(function()
86
- self:fetch_json(url_path, http_method, error_handler, success_handler, timeout_handler, retry_count + 1)
87
- end)
88
- return
89
- elseif response_status >= 400 then
90
- local msg = "Request failed with status " .. response_status
91
- app:log(msg)
92
- error_handler(msg)
93
- return
94
- end
95
-
96
- local response_json = nil
97
- if app:trap(function()
98
- response_json = json.decode(response_body)
99
- end) then
100
- success_handler(response_json)
101
- else
102
- app:log("JSON parse error")
103
- app:log(output)
104
- error_handler("JSON parse error")
105
- end
106
- end
107
-
108
- function ReaSpeechWorker:check_active_job()
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
- -- moved tempfile removal to only successful responses, for debugging purposes only!
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