// Only public methods can be invoked in the background. (Hangfire) public void BackgroundExport(Data.Model.CsvExportInfo exportInfo, ExportNotification notifyEvent) { var currencies = _commerceService.GetAllCurrencies(); var defaultCurrency = currencies.First(x => x.IsPrimary); exportInfo.Currency = exportInfo.Currency ?? defaultCurrency.Code; var catalog = _catalogService.GetById(exportInfo.CatalogId); if (catalog == null) { throw new NullReferenceException("catalog"); } Action <ExportImportProgressInfo> progressCallback = x => { notifyEvent.InjectFrom(x); _notifier.Upsert(notifyEvent); }; using (var stream = new MemoryStream()) { try { exportInfo.Configuration = Data.Model.CsvProductMappingConfiguration.GetDefaultConfiguration(); _csvExporter.DoExport(stream, exportInfo, progressCallback); stream.Position = 0; var fileNameTemplate = _settingsManager.GetValue("CsvCatalogImport.ExportFileNameTemplate", string.Empty); var fileName = string.Format(fileNameTemplate, DateTime.UtcNow); fileName = Path.ChangeExtension(fileName, ".csv"); var blobRelativeUrl = Path.Combine("temp", fileName); //Upload result csv to blob storage using (var blobStream = _blobStorageProvider.OpenWrite(blobRelativeUrl)) { stream.CopyTo(blobStream); } //Get a download url notifyEvent.DownloadUrl = _blobUrlResolver.GetAbsoluteUrl(blobRelativeUrl); notifyEvent.Description = "Export finished"; } catch (Exception ex) { notifyEvent.Description = "Export failed"; notifyEvent.Errors.Add(ex.ExpandExceptionMessage()); } finally { notifyEvent.Finished = DateTime.UtcNow; _notifier.Upsert(notifyEvent); } } }
public IHttpActionResult DoExport(Data.Model.CsvExportInfo exportInfo) { CheckCurrentUserHasPermissionForObjects(CatalogPredefinedPermissions.Export, exportInfo); var notification = new ExportNotification(_userNameResolver.GetCurrentUserName()) { Title = "Catalog export task", Description = "starting export...." }; _notifier.Upsert(notification); BackgroundJob.Enqueue(() => BackgroundExport(exportInfo, notification)); return(Ok(notification)); }