@@ -136,6 +136,23 @@ cpp::result<bool, std::string> EngineService::InstallEngine(
136136 }
137137}
138138
139+ cpp::result<bool , std::string> EngineService::InstallEngineAsync (
140+ const std::string& engine, const std::string& version,
141+ const std::string& src) {
142+ // Although this function is called async, only download tasks are performed async
143+ // TODO(sang) better handler for unzip and download scenarios
144+ auto ne = NormalizeEngine (engine);
145+ if (!src.empty ()) {
146+ return UnzipEngine (ne, version, src);
147+ } else {
148+ auto result = DownloadEngine (ne, version, true /* async*/ );
149+ if (result.has_error ()) {
150+ return result;
151+ }
152+ return DownloadCuda (ne, true /* async*/ );
153+ }
154+ }
155+
139156cpp::result<bool , std::string> EngineService::UnzipEngine (
140157 const std::string& engine, const std::string& version,
141158 const std::string& path) {
@@ -222,7 +239,7 @@ cpp::result<bool, std::string> EngineService::UninstallEngine(
222239}
223240
224241cpp::result<bool , std::string> EngineService::DownloadEngine (
225- const std::string& engine, const std::string& version) {
242+ const std::string& engine, const std::string& version, bool async ) {
226243
227244 // Check if GITHUB_TOKEN env exist
228245 const char * github_token = std::getenv (" GITHUB_TOKEN" );
@@ -320,27 +337,34 @@ cpp::result<bool, std::string> EngineService::DownloadEngine(
320337 .localPath = local_path,
321338 }}}};
322339
323- return download_service_->AddDownloadTask (
324- downloadTask, [](const DownloadTask& finishedTask) {
325- // try to unzip the downloaded file
326- CTL_INF (" Engine zip path: "
327- << finishedTask.items [0 ].localPath .string ());
328-
329- std::filesystem::path extract_path =
330- finishedTask.items [0 ].localPath .parent_path ().parent_path ();
331-
332- archive_utils::ExtractArchive (
333- finishedTask.items [0 ].localPath .string (),
334- extract_path.string ());
335-
336- // remove the downloaded file
337- try {
338- std::filesystem::remove (finishedTask.items [0 ].localPath );
339- } catch (const std::exception& e) {
340- CTL_WRN (" Could not delete file: " << e.what ());
341- }
342- CTL_INF (" Finished!" );
343- });
340+ auto on_finished = [](const DownloadTask& finishedTask) {
341+ // try to unzip the downloaded file
342+ CTL_INF (
343+ " Engine zip path: " << finishedTask.items [0 ].localPath .string ());
344+
345+ std::filesystem::path extract_path =
346+ finishedTask.items [0 ].localPath .parent_path ().parent_path ();
347+
348+ archive_utils::ExtractArchive (
349+ finishedTask.items [0 ].localPath .string (), extract_path.string ());
350+
351+ // remove the downloaded file
352+ try {
353+ std::filesystem::remove (finishedTask.items [0 ].localPath );
354+ } catch (const std::exception& e) {
355+ CTL_WRN (" Could not delete file: " << e.what ());
356+ }
357+ CTL_INF (" Finished!" );
358+ };
359+ if (async) {
360+ auto res = download_service_->AddTask (downloadTask, on_finished);
361+ if (res.has_error ()) {
362+ return cpp::fail (res.error ());
363+ }
364+ return true ;
365+ } else {
366+ return download_service_->AddDownloadTask (downloadTask, on_finished);
367+ }
344368 }
345369 }
346370 return true ;
@@ -350,7 +374,7 @@ cpp::result<bool, std::string> EngineService::DownloadEngine(
350374}
351375
352376cpp::result<bool , std::string> EngineService::DownloadCuda (
353- const std::string& engine) {
377+ const std::string& engine, bool async ) {
354378 if (hw_inf_.sys_inf ->os == " mac" || engine == kOnnxRepo ||
355379 engine == kOnnxEngine ) {
356380 // mac and onnx engine does not require cuda toolkit
@@ -403,19 +427,27 @@ cpp::result<bool, std::string> EngineService::DownloadCuda(
403427 .localPath = cuda_toolkit_local_path}},
404428 }};
405429
406- return download_service_->AddDownloadTask (
407- downloadCudaToolkitTask, [&](const DownloadTask& finishedTask) {
408- auto engine_path =
409- file_manager_utils::GetEnginesContainerPath () / engine;
410- archive_utils::ExtractArchive (finishedTask.items [0 ].localPath .string (),
411- engine_path.string ());
412-
413- try {
414- std::filesystem::remove (finishedTask.items [0 ].localPath );
415- } catch (std::exception& e) {
416- CTL_ERR (" Error removing downloaded file: " << e.what ());
417- }
418- });
430+ auto on_finished = [engine](const DownloadTask& finishedTask) {
431+ auto engine_path = file_manager_utils::GetEnginesContainerPath () / engine;
432+ archive_utils::ExtractArchive (finishedTask.items [0 ].localPath .string (),
433+ engine_path.string ());
434+
435+ try {
436+ std::filesystem::remove (finishedTask.items [0 ].localPath );
437+ } catch (std::exception& e) {
438+ CTL_ERR (" Error removing downloaded file: " << e.what ());
439+ }
440+ };
441+ if (async) {
442+ auto res = download_service_->AddTask (downloadCudaToolkitTask, on_finished);
443+ if (res.has_error ()) {
444+ return cpp::fail (res.error ());
445+ }
446+ return true ;
447+ } else {
448+ return download_service_->AddDownloadTask (downloadCudaToolkitTask,
449+ on_finished);
450+ }
419451}
420452
421453std::string EngineService::GetMatchedVariant (
0 commit comments