public IHttpActionResult DoExport(CsvExportInfo exportInfo) { var notification = new ExportNotification(CurrentPrincipal.GetCurrentUserName()) { Title = "Catalog export task", Description = "starting export...." }; _notifier.Upsert(notification); BackgroundJob.Enqueue(() => BackgroundExport(exportInfo, notification)); return Ok(notification); }
public IHttpActionResult DoExport(CsvExportInfo exportInfo) { base.CheckCurrentUserHasPermissionForObjects(CatalogPredefinedPermissions.Export, exportInfo); var notification = new ExportNotification(CurrentPrincipal.GetCurrentUserName()) { Title = "Catalog export task", Description = "starting export...." }; _notifier.Upsert(notification); BackgroundJob.Enqueue(() => BackgroundExport(exportInfo, notification)); return Ok(notification); }
// Only public methods can be invoked in the background. (Hangfire) public void BackgroundExport(CsvExportInfo exportInfo, ExportNotification notifyEvent) { var curencySetting = _settingsManager.GetSettingByName("VirtoCommerce.Core.General.Currencies"); var defaultCurrency = EnumUtility.SafeParse<CurrencyCodes>(curencySetting.DefaultValue, CurrencyCodes.USD); exportInfo.Currency = exportInfo.Currency ?? defaultCurrency; 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 = CsvProductMappingConfiguration.GetDefaultConfiguration(); _csvExporter.DoExport(stream, exportInfo, progressCallback); stream.Position = 0; //Upload result csv to blob storage var uploadInfo = new UploadStreamInfo { FileName = "Catalog-" + catalog.Name + "-export.csv", FileByteStream = stream, FolderName = "temp" }; var blobKey = _blobStorageProvider.Upload(uploadInfo); //Get a download url notifyEvent.DownloadUrl = _blobUrlResolver.GetAbsoluteUrl(blobKey); notifyEvent.Description = "Export finished"; } catch (Exception ex) { notifyEvent.Description = "Export failed"; notifyEvent.Errors.Add(ex.ExpandExceptionMessage()); } finally { notifyEvent.Finished = DateTime.UtcNow; _notifier.Upsert(notifyEvent); } } }