Repository navigation
Let export and import be cancelled - #925
Merged
Merged
Conversation
Thread a CancellationToken through IExportManager, ISchemaProperty, IImportManager, IImportDataProvider and IImportDataObject down to the picture preload, the spreadsheet read, the row batch and the picture download. The admin panel passes HttpContext.RequestAborted, so a closed tab or a proxy timeout stops the work at the next row or picture.
A row downloads its pictures before it writes anything, so a cancelled download leaves it untouched and it is reported not processed. The brand, category and collection imports share one picture helper instead of three copies of LoadPicture. A cancelled panel import throws instead of being reported as imported, and the admin export/import actions let an aborted request's cancellation reach the exception handler instead of logging it as an error.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Type: bugfix
Issue
Of the export/import contracts, only
IRowImport<T>.Importtook aCancellationToken.IExportManager<T>.Export,ISchemaProperty<T>.GetProperties,IImportManager<T>.Import,IImportDataProvider.Convert<T>andIImportDataObject<T>.Executedid not, and the panel's export andImportExcelactions never passedHttpContext.RequestAborted. A large spreadsheet import kept running row by row, with picture downloads of up to 30 s each, after the proxy had timed out or the admin had closed the tab. A large export kept loading pictures and building a workbook nobody would download.Solution
CancellationToken cancellationToken = default, and the token is passed all the way down:ExportManager→ISchemaProperty.GetProperties→ExportPictures.Preload, which checks the token before each picture. A cancelled export does not build the workbook.ImportManager→ExcelImportProvider.Convert→PowerExcelMapper.FetchAsync(CopyToAsync) →IImportDataObject.Execute→ImportRows.RunBatch→ the brand, category, collection and product picture download. The country import checks the token before each row.NotProcessedError) or fully written; there is no half-saved entity without its slug or pictures. The brand, category and collection imports share one helper (ImportPictures) instead of three copies ofLoadPicture; the product import compares pictures with it too.DownloadUrl.DownloadImagetakes the token and links it with its 30 s limit. The time limit still returnsnull. A cancelled caller getsOperationCanceledException, so the row is not saved as if the picture were missing.RunBatchtreats only anOperationCanceledExceptionof its own token as a cancel, so a driver or HTTP timeout is still reported as a failure.IImportDataObject.Executelogs the rows it did not reach and then throwsOperationCanceledException, so the panel does not report a cancelled import as imported (the country import already threw).HttpContext.RequestAborted.GetCustomerDataHandlerpasses the handler's token.BaseController.IsRequestAborted(exception): the export/import actions'catch (Exception)leaves out the request's own cancellation, so the exception handler middleware logs the aborted request at debug level instead of the action logging an error and queueing an error notification.Breaking changes
This extends the
ISchemaPropertybreak from #923 in the same release (2.5.0).IExportManager<T>.Export,ISchemaProperty<T>.GetProperties,IImportManager<T>.Import,IImportDataProvider.Convert<T>andIImportDataObject<T>.Executehave a newCancellationToken cancellationToken = defaultparameter. There are no compatibility overloads.ExportManager<T>or a*SchemaPropertyand overridesExport/GetProperties, has to add the parameter.DownloadUrl.DownloadImagehas a new optionalcancellationTokenparameter.PowerExcelMapper.FetchAsynchas a new optional parameter aftervalueParser.MissingMethodExceptionorTypeLoadException.IImportDataObject<T>.ExecutethrowsOperationCanceledExceptionwhen its token is cancelled; rows already saved stay saved.NotProcessedErrorand nothing of it is written. Before, it finished the download first, which took up to 30 s.Testing
dotnet testonGrand.Business.Catalog.Tests,Grand.Business.Common.Tests,Grand.Business.Checkout.Tests,Grand.Business.Customers.Tests,Grand.Web.Admin.TestsandGrand.Web.Tests: all pass. New tests:ProductImportDataObjectTests.Execute_CancelledMidBatch_RemainingRowsLoggedAsNotProcessedAndThrowsProductImportDataObjectTests.Import_CancelledDuringPictureDownload_RowNotWrittenAndNotProcessedBaseBrandControllerTests.ImportFromXlsx_RequestAborted_PropagatesWithoutNotificationBaseBrandControllerTests.ExportXlsx_CancelledWithoutAbortedRequest_ShowsErrorAndRedirectsToListSchemaPropertyTests.Product_CancelledDuringPicturePreload_StopsBeforeNextPictureDownloadUrlTests.DownloadImage_CallerCancelled_ThrowsInsteadOfReturningNull