j commited on
Commit
a1d9d40
·
1 Parent(s): 6cd6178

added necessary bits to fetch_json and check_active_job_status to retry

Browse files
reascripts/ReaSpeech/source/ReaSpeechAPI.lua CHANGED
@@ -27,60 +27,101 @@ function ReaSpeechAPI:get_curl_cmd()
27
  return curl
28
  end
29
 
30
- -- Fetch simple JSON responses. Will block until result or curl timeout.
31
- -- For large amounts of data, use fetch_large instead.
32
- function ReaSpeechAPI:fetch_json(url_path, http_method, error_handler, timeout_handler)
33
  http_method = http_method or 'GET'
34
  error_handler = error_handler or function(_msg) end
 
35
  timeout_handler = timeout_handler or function() end
 
 
 
 
36
 
37
  local curl = self:get_curl_cmd()
38
  local api_url = self:get_api_url(url_path)
39
-
40
  local http_method_argument = ""
41
  if http_method ~= 'GET' then
42
- http_method_argument = " -X " .. http_method
43
  end
44
-
45
  local command = table.concat({
46
- curl,
47
- ' "', api_url, '"',
48
- ' -H "accept: application/json"',
49
- http_method_argument,
50
- ' -m ', self.CURL_TIMEOUT_SECONDS,
51
- ' -s',
52
- ' -i',
53
- ' --http1.1',
54
- ' --retry 5',
55
  })
56
-
57
  app:debug('Fetch JSON: ' .. command)
58
 
59
  local exec_result = (ExecProcess.new { command }):wait()
60
-
61
  if exec_result == nil then
62
- local msg = "Unable to run curl"
63
- app:log(msg)
64
- error_handler(msg)
65
- return nil
66
  end
67
 
68
  local status, output = exec_result:match("(%d+)\n(.*)")
69
  status = tonumber(status)
70
 
71
  if status == 28 then
72
- app:debug("Curl timeout reached")
73
- timeout_handler()
74
- return nil
75
  elseif status ~= 0 then
76
- local msg = "Curl failed with status " .. status
77
- app:debug(msg)
78
- error_handler(msg)
79
- return nil
80
  end
81
 
82
  local response_status, response_body = self.http_status_and_body(output)
83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  if response_status >= 400 then
85
  local msg = "Request failed with status " .. response_status
86
  app:log(msg)
 
27
  return curl
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)
reascripts/ReaSpeech/source/ReaSpeechWorker.lua CHANGED
@@ -131,7 +131,7 @@ function ReaSpeechWorker:get_job_status(job_id, retry_count)
131
  self.active_job = nil
132
  end
133
  end, function(response)
134
-
135
  if self:handle_job_status(self.active_job, response) then
136
  self.active_job = nil
137
  end
@@ -250,16 +250,22 @@ end
250
 
251
  function ReaSpeechWorker:check_active_job_status()
252
  if not self.active_job then return end
253
-
254
  local active_job = self.active_job
255
  if not active_job.job.job_id then return end
256
 
257
- local response = self:get_job_status(active_job.job.job_id)
258
- if response then
259
- if self:handle_job_status(active_job, response) then
260
- self.active_job = nil
261
- end
262
- end
 
 
 
 
 
 
 
263
  end
264
 
265
  ReaSpeechWorker.check_sentinel = function(filename)
 
131
  self.active_job = nil
132
  end
133
  end, function(response)
134
+
135
  if self:handle_job_status(self.active_job, response) then
136
  self.active_job = nil
137
  end
 
250
 
251
  function ReaSpeechWorker:check_active_job_status()
252
  if not self.active_job then return end
 
253
  local active_job = self.active_job
254
  if not active_job.job.job_id then return end
255
 
256
+ ReaSpeechAPI:fetch_json("jobs/" .. active_job.job.job_id, 'GET',
257
+ function(error_message)
258
+ -- Error handler
259
+ self:handle_error(active_job, error_message)
260
+ self.active_job = nil
261
+ end,
262
+ function(response)
263
+ -- Success handler
264
+ if self:handle_job_status(active_job, response) then
265
+ self.active_job = nil
266
+ end
267
+ end
268
+ )
269
  end
270
 
271
  ReaSpeechWorker.check_sentinel = function(filename)