// Only public methods can be invoked in the background. (Hangfire) public void BackgroundImport(CsvImportInfo importInfo, ImportNotification notifyEvent) { Action<ExportImportProgressInfo> progressCallback = (x) => { notifyEvent.InjectFrom(x); _notifier.Upsert(notifyEvent); }; using (var stream = _blobStorageProvider.OpenRead(importInfo.FileUrl)) { try { _csvImporter.DoImport(stream, importInfo, progressCallback); } catch (Exception ex) { notifyEvent.Description = "Export error"; notifyEvent.ErrorCount++; notifyEvent.Errors.Add(ex.ToString()); } finally { notifyEvent.Finished = DateTime.UtcNow; notifyEvent.Description = "Import finished" + (notifyEvent.Errors.Any() ? " with errors" : " successfully"); _notifier.Upsert(notifyEvent); } } }
public IHttpActionResult DoImport(CsvImportInfo importInfo) { base.CheckCurrentUserHasPermissionForObjects(CatalogPredefinedPermissions.Import, importInfo); var notification = new ImportNotification(CurrentPrincipal.GetCurrentUserName()) { Title = "Import catalog from CSV", Description = "starting import...." }; _notifier.Upsert(notification); BackgroundJob.Enqueue(() => BackgroundImport(importInfo, notification)); return Ok(notification); }