You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add background option to update/install APIs (#6134)
* Add background option to update/install APIs
* Refactor to use common background_task utility in backups too
* Use a validation_complete event rather then looking for bus events
Copy file name to clipboardExpand all lines: supervisor/api/utils.py
+48-1Lines changed: 48 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,9 @@
1
1
"""Init file for Supervisor util for RESTful API."""
2
2
3
+
importasyncio
4
+
fromcollections.abcimportCallable
3
5
importjson
4
-
fromtypingimportAny
6
+
fromtypingimportAny, cast
5
7
6
8
fromaiohttpimportweb
7
9
fromaiohttp.hdrsimportAUTHORIZATION
@@ -23,6 +25,7 @@
23
25
)
24
26
from ..coresysimportCoreSys, CoreSysAttributes
25
27
from ..exceptionsimportAPIError, BackupFileNotFoundError, DockerAPIError, HassioError
28
+
from ..jobsimportJobSchedulerOptions, SupervisorJob
26
29
from ..utilsimportcheck_exception_chain, get_message_from_exception_chain
27
30
from ..utils.jsonimportjson_dumps, json_loadsasjson_loads_util
28
31
from ..utils.log_formatimportformat_message
@@ -198,3 +201,47 @@ async def api_validate(
198
201
data_validated[origin_value] =data[origin_value]
199
202
200
203
returndata_validated
204
+
205
+
206
+
asyncdefbackground_task(
207
+
coresys_obj: CoreSysAttributes,
208
+
task_method: Callable,
209
+
*args,
210
+
**kwargs,
211
+
) ->tuple[asyncio.Task, str]:
212
+
"""Start task in background and return task and job ID.
213
+
214
+
Args:
215
+
coresys_obj: Instance that accesses coresys data using CoreSysAttributes
216
+
task_method: The method to execute in the background. Must include a keyword arg 'validation_complete' of type asyncio.Event. Should set it after any initial validation has completed
217
+
*args: Arguments to pass to task_method
218
+
**kwargs: Keyword arguments to pass to task_method
219
+
220
+
Returns:
221
+
Tuple of (task, job_id)
222
+
223
+
"""
224
+
event=asyncio.Event()
225
+
job, task=cast(
226
+
tuple[SupervisorJob, asyncio.Task],
227
+
coresys_obj.sys_jobs.schedule_job(
228
+
task_method,
229
+
JobSchedulerOptions(),
230
+
*args,
231
+
validation_complete=event,
232
+
**kwargs,
233
+
),
234
+
)
235
+
236
+
# Wait for provided event before returning
237
+
# If the task fails validation it should raise before getting there
0 commit comments