9
9
import logging
10
10
from datetime import datetime
11
11
from typing import Callable , ParamSpec , TypeVar
12
- from urllib .error import HTTPError
12
+ from urllib .error import HTTPError , URLError
13
13
14
14
import requests
15
15
39
39
40
40
logger = logging .getLogger (__name__ )
41
41
42
+ TIMEOUT_IN_SECS = 60
43
+
42
44
43
45
class GithubRunnerNotFoundError (Exception ):
44
46
"""Represents an error when the runner could not be found on GitHub."""
@@ -77,6 +79,7 @@ def wrapper(*args: ParamT.args, **kwargs: ParamT.kwargs) -> ReturnT:
77
79
"""
78
80
try :
79
81
return func (* args , ** kwargs )
82
+ # The ghapi module uses urllib. The HTTPError and URLError are urllib exceptions.
80
83
except HTTPError as exc :
81
84
if exc .code in (401 , 403 ):
82
85
if exc .code == 401 :
@@ -86,9 +89,15 @@ def wrapper(*args: ParamT.args, **kwargs: ParamT.kwargs) -> ReturnT:
86
89
raise TokenError (msg ) from exc
87
90
logger .warning ("Error in GitHub request: %s" , exc )
88
91
raise PlatformApiError from exc
92
+ except URLError as exc :
93
+ logger .warning ("General error in GitHub request: %s" , exc )
94
+ raise PlatformApiError from exc
89
95
except RequestException as exc :
90
96
logger .warning ("Error in GitHub request: %s" , exc )
91
97
raise PlatformApiError from exc
98
+ except TimeoutError as exc :
99
+ logger .warning ("Timeout in GitHub request: %s" , exc )
100
+ raise PlatformApiError from exc
92
101
93
102
return wrapper
94
103
@@ -124,11 +133,11 @@ def get_runner(self, path: GitHubPath, prefix: str, runner_id: int) -> SelfHoste
124
133
try :
125
134
if isinstance (path , GitHubRepo ):
126
135
raw_runner = self ._client .actions .get_self_hosted_runner_for_repo (
127
- path .owner , path .repo , runner_id
136
+ path .owner , path .repo , runner_id , timeout = TIMEOUT_IN_SECS
128
137
)
129
138
else :
130
139
raw_runner = self ._client .actions .get_self_hosted_runner_for_org (
131
- path .org , runner_id
140
+ path .org , runner_id , timeout = TIMEOUT_IN_SECS
132
141
)
133
142
except HTTP404NotFoundError as err :
134
143
raise GithubRunnerNotFoundError from err
@@ -164,6 +173,7 @@ def list_runners(self, path: GitHubPath, prefix: str) -> list[SelfHostedRunner]:
164
173
owner = path .owner ,
165
174
repo = path .repo ,
166
175
per_page = 100 ,
176
+ timeout = TIMEOUT_IN_SECS ,
167
177
)
168
178
for item in page ["runners" ]
169
179
]
@@ -179,6 +189,7 @@ def list_runners(self, path: GitHubPath, prefix: str) -> list[SelfHostedRunner]:
179
189
num_of_pages + 1 ,
180
190
org = path .org ,
181
191
per_page = 100 ,
192
+ timeout = TIMEOUT_IN_SECS ,
182
193
)
183
194
for item in page ["runners" ]
184
195
]
@@ -217,6 +228,7 @@ def get_runner_registration_jittoken(
217
228
name = instance_id .name ,
218
229
runner_group_id = 1 ,
219
230
labels = labels ,
231
+ timeout = TIMEOUT_IN_SECS ,
220
232
)
221
233
elif isinstance (path , GitHubOrg ):
222
234
# We cannot cache it in here, as we are running in a forked process.
@@ -227,6 +239,7 @@ def get_runner_registration_jittoken(
227
239
name = instance_id .name ,
228
240
runner_group_id = runner_group_id ,
229
241
labels = labels ,
242
+ timeout = TIMEOUT_IN_SECS ,
230
243
)
231
244
else :
232
245
assert_never (token )
@@ -249,7 +262,7 @@ def _get_runner_group_id(self, org: GitHubOrg) -> int:
249
262
"Authorization" : f"Bearer { self ._token } " ,
250
263
"X-GitHub-Api-Version" : "2022-11-28" ,
251
264
}
252
- response = requests .get (url , headers = headers , timeout = 30 )
265
+ response = requests .get (url , headers = headers , timeout = TIMEOUT_IN_SECS )
253
266
response .raise_for_status ()
254
267
data = response .json ()
255
268
try :
@@ -282,11 +295,13 @@ def delete_runner(self, path: GitHubPath, runner_id: int) -> None:
282
295
owner = path .owner ,
283
296
repo = path .repo ,
284
297
runner_id = runner_id ,
298
+ timeout = TIMEOUT_IN_SECS ,
285
299
)
286
300
else :
287
301
self ._client .actions .delete_self_hosted_runner_from_org (
288
302
org = path .org ,
289
303
runner_id = runner_id ,
304
+ timeout = TIMEOUT_IN_SECS ,
290
305
)
291
306
# The function delete_self_hosted_runner fails in GitHub if the runner does not exist,
292
307
# so we do not have to worry about that.
@@ -310,7 +325,12 @@ def get_job_info_by_runner_name(
310
325
Returns:
311
326
Job information.
312
327
"""
313
- paged_kwargs = {"owner" : path .owner , "repo" : path .repo , "run_id" : workflow_run_id }
328
+ paged_kwargs = {
329
+ "owner" : path .owner ,
330
+ "repo" : path .repo ,
331
+ "run_id" : workflow_run_id ,
332
+ "timeout" : 60 ,
333
+ }
314
334
try :
315
335
for wf_run_page in paged (
316
336
self ._client .actions .list_jobs_for_workflow_run , ** paged_kwargs
@@ -353,6 +373,7 @@ def get_job_info(self, path: GitHubRepo, job_id: str) -> JobInfo:
353
373
owner = path .owner ,
354
374
repo = path .repo ,
355
375
job_id = job_id ,
376
+ timeout = TIMEOUT_IN_SECS ,
356
377
)
357
378
except HTTPError as exc :
358
379
if exc .code == 404 :
0 commit comments