Skip to content

Commit

Permalink
Handle calendar API timeouts with retries
Browse files Browse the repository at this point in the history
  • Loading branch information
k1o0 committed Jul 31, 2023
1 parent bd80f8b commit 010a0e1
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion +ww/getNumDays.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
params = ww.Params;
nDays = params.get('nDaysInFuture');
assert(nDays < 6, 'Cannot post water for more than 5 days in the future')
maxTries = 3;
fail = false;
if isempty(params.get('CAL_API_KEY')) || isempty(params.get('CAL_API_URL'))
disp('Calendar API not configured; skipping')
Expand All @@ -21,7 +22,20 @@
fullEndpoint = sprintf('%s?country=%s&year=%d&region=%s&api_key=%s', ...
params.get('CAL_API_URL'), params.get('CAL_Country'), V(1), ...
params.get('CAL_Region'), params.get('CAL_API_KEY'));
data = webread(fullEndpoint, options);

% We sometimes get a timeout so let's try a few times
for i = 1:maxTries
try
data = webread(fullEndpoint, options);
catch ex
if strcmp(ex.identifier, 'MATLAB:webservices:Timeout') && i < maxTries
warning(ex.identifier, 'Calendar API response timeout, retrying...')
else
rethrow(ex)
end
end
end

if params.get('Mode') > 0 % Print URL and response code
fprintf('GET %d %s %s %s', ...
data.meta.code, ...
Expand Down

0 comments on commit 010a0e1

Please sign in to comment.