Example #1
0
        public IActionResult Run([FromBody] ThumbnailsTaskRunRequest runRequest)
        {
            var notification = Enqueue(runRequest);

            _pushNotifier.Send(notification);
            return(Ok(notification));
        }
        public void ModuleBackgroundJob(ModuleBackgroundJobOptions options, ModulePushNotification notification)
        {
            try
            {
                notification.Started = DateTime.UtcNow;
                var moduleInfos = _externalModuleCatalog.Modules.OfType <ManifestModuleInfo>()
                                  .Where(x => options.Modules.Any(y => y.Identity.Equals(x.Identity)))
                                  .ToArray();
                var reportProgress = new Progress <ProgressMessage>(m =>
                {
                    lock (_lockObject)
                    {
                        notification.Description = m.Message;
                        notification.ProgressLog.Add(m);
                        _pushNotifier.Send(notification);
                    }
                });

                switch (options.Action)
                {
                case ModuleAction.Install:
                    _moduleInstaller.Install(moduleInfos, reportProgress);
                    break;

                case ModuleAction.Uninstall:
                    _moduleInstaller.Uninstall(moduleInfos, reportProgress);
                    break;
                }
            }
            catch (Exception ex)
            {
                notification.ProgressLog.Add(new ProgressMessage
                {
                    Level   = ProgressMessageLevel.Error,
                    Message = ex.ToString(),
                });
            }
            finally
            {
                _settingsManager.SetValue(PlatformConstants.Settings.Setup.ModulesAutoInstallState.Name, AutoInstallState.Completed);

                notification.Finished    = DateTime.UtcNow;
                notification.Description = "Installation finished.";
                notification.ProgressLog.Add(new ProgressMessage
                {
                    Level   = ProgressMessageLevel.Info,
                    Message = notification.Description,
                });
                _pushNotifier.Send(notification);
            }
        }
        public async Task BackgroundImportAsync(ImportRequest request, ImportNotification notification)
        {
            Action <ExportImportProgressInfo> progressCallback = c =>
            {
                notification.Description = c.Description;
                notification.Errors      = c.Errors;
                notification.ErrorCount  = c.ErrorCount;

                _notifier.Send(notification);
            };

            using (var stream = _blobStorageProvider.OpenRead(request.FileUrl))
            {
                try
                {
                    await _csvCouponImporter.DoImportAsync(stream, request.Delimiter, request.PromotionId, request.ExpirationDate, progressCallback);
                }
                catch (Exception exception)
                {
                    notification.Description = "Import error";
                    notification.ErrorCount++;
                    notification.Errors.Add(exception.ToString());
                }
                finally
                {
                    notification.Finished    = DateTime.UtcNow;
                    notification.Description = "Import finished" + (notification.Errors.Any() ? " with errors" : " successfully");
                    await _notifier.SendAsync(notification);
                }
            }
        }
        public async Task <ActionResult <PlatformExportPushNotification> > RunExport([FromBody] ExportDataRequest request)
        {
            var authorizationResult = await _authorizationService.AuthorizeAsync(User, request.DataQuery, request.ExportTypeName + "ExportDataPolicy");

            if (!authorizationResult.Succeeded)
            {
                return(Unauthorized());
            }

            var typeTitle = request.ExportTypeName.LastIndexOf('.') > 0 ?
                            request.ExportTypeName.Substring(request.ExportTypeName.LastIndexOf('.') + 1) : request.ExportTypeName;

            var notification = new ExportPushNotification(_userNameResolver.GetCurrentUserName())
            {
                NotifyType  = "PlatformExportPushNotification",
                Title       = $"{typeTitle} export",
                Description = "Starting export task..."
            };

            _pushNotificationManager.Send(notification);

            var jobId = BackgroundJob.Enqueue <ExportJob>(x => x.ExportBackgroundAsync(request, notification, JobCancellationToken.Null, null));

            notification.JobId = jobId;

            return(Ok(notification));
        }
        public void ModuleBackgroundJob(ModuleBackgroundJobOptions options, ModulePushNotification notification)
        {
            try
            {
                notification.Started = DateTime.UtcNow;
                var moduleInfos = _moduleCatalog.Modules.OfType <ManifestModuleInfo>()
                                  .Where(x => options.Modules.Any(y => y.Identity.Equals(x.Identity)))
                                  .ToArray();
                var reportProgress = new Progress <ProgressMessage>(m =>
                {
                    lock (_lockObject)
                    {
                        notification.ProgressLog.Add(m);
                        _pushNotifier.Send(notification);
                    }
                });

                switch (options.Action)
                {
                case ModuleAction.Install:
                    _moduleInstaller.Install(moduleInfos, reportProgress);
                    break;

                case ModuleAction.Uninstall:
                    _moduleInstaller.Uninstall(moduleInfos, reportProgress);
                    break;
                }
            }
            catch (Exception ex)
            {
                notification.ProgressLog.Add(new ProgressMessage
                {
                    Level   = ProgressMessageLevel.Error,
                    Message = ex.ToString()
                });
            }
            finally
            {
                notification.Finished = DateTime.UtcNow;
                notification.ProgressLog.Add(new ProgressMessage
                {
                    Level   = ProgressMessageLevel.Info,
                    Message = "Installation finished.",
                });
                _pushNotifier.Send(notification);
            }
        }
Example #6
0
        public async Task Process(ThumbnailsTaskRunRequest generateRequest, ThumbnailProcessNotification notifyEvent, IJobCancellationToken cancellationToken, PerformContext context)
        {
            try
            {
                Action <ThumbnailTaskProgress> progressCallback = x =>
                {
                    notifyEvent.Description    = x.Message;
                    notifyEvent.Errors         = x.Errors;
                    notifyEvent.ErrorCount     = notifyEvent.Errors.Count;
                    notifyEvent.TotalCount     = x.TotalCount ?? 0;
                    notifyEvent.ProcessedCount = x.ProcessedCount ?? 0;
                    notifyEvent.JobId          = context.BackgroundJob.Id;

                    _pushNotifier.Send(notifyEvent);
                };

                //wrap token
                var tasks = await _taskService.GetByIdsAsync(generateRequest.TaskIds);

                var cancellationTokenWrapper = new JobCancellationTokenWrapper(cancellationToken);
                await _thumbnailProcessor.ProcessTasksAsync(tasks, generateRequest.Regenerate, progressCallback, cancellationTokenWrapper);

                //update tasks in case of successful generation
                foreach (var task in tasks)
                {
                    task.LastRun = DateTime.UtcNow;
                }

                await _taskService.SaveChangesAsync(tasks);
            }
            catch (JobAbortedException)
            {
                //do nothing
            }
            catch (Exception ex)
            {
                notifyEvent.Description = "Error";
                notifyEvent.ErrorCount++;
                notifyEvent.Errors.Add(ex.ToString());
            }
            finally
            {
                notifyEvent.Finished    = DateTime.UtcNow;
                notifyEvent.Description = "Process finished" + (notifyEvent.Errors.Any() ? " with errors" : " successfully");
                _pushNotifier.Send(notifyEvent);
            }
        }
Example #7
0
        public IActionResult IndexDocuments([FromBody] IndexingOptions[] options)
        {
            var currentUserName = _userNameResolver.GetCurrentUserName();
            var notification    = IndexingJobs.Enqueue(currentUserName, options);

            _pushNotifier.Send(notification);
            return(Ok(notification));
        }
        public IActionResult ImportSampleData([FromQuery] string url = null)
        {
            lock (_lockObject)
            {
                var sampleDataState = EnumUtility.SafeParse(_settingsManager.GetValue(PlatformConstants.Settings.Setup.SampleDataState.Name, SampleDataState.Undefined.ToString()), SampleDataState.Undefined);
                if (sampleDataState == SampleDataState.Undefined)
                {
                    //Sample data initialization
                    if (Uri.IsWellFormedUriString(url, UriKind.Absolute))
                    {
                        _settingsManager.SetValue(PlatformConstants.Settings.Setup.SampleDataState.Name, SampleDataState.Processing);
                        var pushNotification = new SampleDataImportPushNotification(User.Identity.Name);
                        _pushNotifier.Send(pushNotification);
                        var jobId = BackgroundJob.Enqueue(() => SampleDataImportBackgroundAsync(new Uri(url), _hostEnv.MapPath(_platformOptions.LocalUploadFolderPath), pushNotification, JobCancellationToken.Null, null));
                        pushNotification.JobId = jobId;

                        return(Ok(pushNotification));
                    }
                }
            }

            return(NoContent());
        }
        public async Task ExportBackgroundAsync(ExportDataRequest request, ExportPushNotification notification, IJobCancellationToken cancellationToken, PerformContext context)
        {
            void progressCallback(ExportProgressInfo x)
            {
                notification.Patch(x);
                notification.JobId = context.BackgroundJob.Id;
                _pushNotificationManager.Send(notification);
            }

            try
            {
                if (string.IsNullOrEmpty(_platformOptions.DefaultExportFolder))
                {
                    throw new PlatformException($"{nameof(_platformOptions.DefaultExportFolder)} should be set.");
                }

                var fileName = string.Format(FileNameTemplate, DateTime.UtcNow);

                // Do not like provider creation here to get file extension, maybe need to pass created provider to Exporter.
                // Create stream inside Exporter is not good as it is not Exporter resposibility to decide where to write.
                var provider = _exportProviderFactory.CreateProvider(request);

                if (!string.IsNullOrEmpty(provider.ExportedFileExtension))
                {
                    fileName = Path.ChangeExtension(fileName, provider.ExportedFileExtension);
                }

                var url = UrlHelperExtensions.Combine(_platformOptions.DefaultExportFolder, fileName);
                using (var blobStream = _blobStorageProvider.OpenWrite(url))
                {
                    _dataExporter.Export(blobStream, request, progressCallback, new JobCancellationTokenWrapper(cancellationToken));
                }

                notification.DownloadUrl = _blobUrlResolver.GetAbsoluteUrl(url);
            }
            catch (JobAbortedException)
            {
                //do nothing
            }
            catch (Exception ex)
            {
                notification.Errors.Add(ex.ExpandExceptionMessage());
            }
            finally
            {
                notification.Description = "Export finished";
                notification.Finished    = DateTime.UtcNow;
                await _pushNotificationManager.SendAsync(notification);
            }
        }
        public async Task RunManually(string[] orderIds, OrdersSynchronizationPushNotification notification,
                                      IJobCancellationToken cancellationToken, PerformContext context)
        {
            var ordersFeed = new InMemoryIndexDocumentChangeFeed(orderIds, IndexDocumentChangeType.Modified, BatchSize);

            void ProgressCallback(AvaTaxOrdersSynchronizationProgress x)
            {
                notification.Description    = x.Message;
                notification.Errors         = x.Errors;
                notification.ErrorCount     = notification.Errors.Count;
                notification.TotalCount     = x.TotalCount ?? 0;
                notification.ProcessedCount = x.ProcessedCount ?? 0;
                notification.JobId          = context.BackgroundJob.Id;

                _pushNotificationManager.Send(notification);
            }

            try
            {
                await PerformOrderSynchronization(ordersFeed, ProgressCallback, cancellationToken);
            }
            catch (JobAbortedException)
            {
                //do nothing
            }
            catch (Exception ex)
            {
                notification.ErrorCount++;
                notification.Errors.Add(ex.ToString());
            }
            finally
            {
                notification.Finished    = DateTime.UtcNow;
                notification.Description = "Process finished " + (notification.Errors.Any() ? "with errors" : "successfully");
                _pushNotificationManager.Send(notification);
            }
        }
Example #11
0
        public async Task ExecuteAsync(
            BulkActionContext bulkActionContext,
            BulkActionPushNotification notification,
            IJobCancellationToken cancellationToken,
            PerformContext performContext)
        {
            Validate(bulkActionContext);
            Validate(performContext);

            try
            {
                var tokenWrapper = new JobCancellationTokenWrapper(cancellationToken);
                await _bulkActionExecutor.ExecuteAsync(
                    bulkActionContext,
                    context =>
                {
                    notification.Patch(context);
                    notification.JobId = performContext.BackgroundJob.Id;
                    _pushNotificationManager.Send(notification);
                },
                    tokenWrapper);
            }
            catch (JobAbortedException)
            {
                // idle
            }
            catch (Exception exception)
            {
                notification.Errors.Add(exception.ExpandExceptionMessage());
            }
            finally
            {
                notification.Description = "Job finished";
                notification.Finished    = DateTime.UtcNow;
                _pushNotificationManager.Send(notification);
            }
        }
Example #12
0
        public void Progress(IndexingProgress progress)
        {
            _log.LogTrace(progress.Description);

            _totalCountMap[progress.DocumentType]     = progress.TotalCount ?? 0;
            _processedCountMap[progress.DocumentType] = progress.ProcessedCount ?? 0;

            _notification.DocumentType = progress.DocumentType;
            _notification.Description  = progress.Description;

            if (!progress.Errors.IsNullOrEmpty())
            {
                _notification.Errors.AddRange(progress.Errors);
                _notification.ErrorCount = _notification.Errors.Count;
            }

            _notification.TotalCount     = progress.TotalCount ?? 0;
            _notification.ProcessedCount = progress.ProcessedCount ?? 0;

            if (!_suppressInsignificantNotifications || progress.TotalCount > 0 || progress.ProcessedCount > 0)
            {
                _pushNotificationManager.Send(_notification);
            }
        }
 private void ProgressCallback(ExportProgressInfo x, ExportPushNotification pushNotification, PerformContext context)
 {
     pushNotification.Patch(x);
     pushNotification.JobId = context.BackgroundJob.Id;
     _pushNotificationManager.Send(pushNotification);
 }
Example #14
0
        public async Task BackgroundDownload(string storeId, string baseUrl, SitemapDownloadNotification notification)
        {
            void SendNotificationWithProgressInfo(ExportImportProgressInfo c)
            {
                // TODO: is there a better way to copy ExportImportProgressInfo properties to SitemapDownloadNotification without using ValueInjecter?
                notification.Description    = c.Description;
                notification.ProcessedCount = c.ProcessedCount;
                notification.TotalCount     = c.TotalCount;
                notification.Errors         = c.Errors?.ToList() ?? new List <string>();

                _notifier.Send(notification);
            }

            try
            {
                var relativeUrl    = $"tmp/sitemap-{storeId}.zip";
                var localTmpFolder = MapPath(_hostingEnvironment, "~/App_Data/Uploads/tmp");
                var localTmpPath   = Path.Combine(localTmpFolder, $"sitemap-{storeId}.zip");
                if (!Directory.Exists(localTmpFolder))
                {
                    Directory.CreateDirectory(localTmpFolder);
                }

                //Import first to local tmp folder because Azure blob storage doesn't support some special file access mode
                using (var stream = SystemFile.Open(localTmpPath, FileMode.OpenOrCreate))
                {
                    using (var zipPackage = ZipPackage.Open(stream, FileMode.Create))
                    {
                        await CreateSitemapPartAsync(zipPackage, storeId, baseUrl, "sitemap.xml", SendNotificationWithProgressInfo);

                        var sitemapUrls = await _sitemapXmlGenerator.GetSitemapUrlsAsync(storeId);

                        foreach (var sitemapUrl in sitemapUrls)
                        {
                            if (!string.IsNullOrEmpty(sitemapUrl))
                            {
                                await CreateSitemapPartAsync(zipPackage, storeId, baseUrl, sitemapUrl, SendNotificationWithProgressInfo);
                            }
                        }
                    }
                }
                //Copy export data to blob provider for get public download url
                using (var localStream = SystemFile.Open(localTmpPath, FileMode.Open))
                    using (var blobStream = _blobStorageProvider.OpenWrite(relativeUrl))
                    {
                        localStream.CopyTo(blobStream);
                        notification.DownloadUrl = _blobUrlResolver.GetAbsoluteUrl(relativeUrl);
                        notification.Description = "Sitemap download finished";
                    }
            }
            catch (Exception exception)
            {
                notification.Description = "Sitemap download failed";
                notification.Errors.Add(exception.ExpandExceptionMessage());
            }
            finally
            {
                notification.Finished = DateTime.UtcNow;
                await _notifier.SendAsync(notification);
            }
        }