public IHttpActionResult Run(ThumbnailsTaskRunRequest runRequest)
        {
            var notification = Enqueue(runRequest);

            _pushNotifier.Upsert(notification);
            return(Ok(notification));
        }
Пример #2
0
        public IActionResult Run([FromBody] ThumbnailsTaskRunRequest runRequest)
        {
            var notification = Enqueue(runRequest);

            _pushNotifier.Send(notification);
            return(Ok(notification));
        }
        private ThumbnailProcessNotification Enqueue(ThumbnailsTaskRunRequest runRequest)
        {
            var notification = new ThumbnailProcessNotification(_userNameResolver.GetCurrentUserName())
            {
                Title       = "Process images",
                Description = "starting process...."
            };

            _pushNotifier.Upsert(notification);

            var jobId = BackgroundJob.Enqueue <ThumbnailProcessJob>(x => x.Process(runRequest, notification, JobCancellationToken.Null, null));

            notification.JobId = jobId;

            return(notification);
        }
        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.Upsert(notifyEvent);
                };

                //wrap token
                var tasks = _taskService.GetByIds(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;
                }

                _taskService.SaveOrUpdate(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.Upsert(notifyEvent);
            }
        }