Exemple #1
0
        public Task DeleteTaskAsync(ITaskDescriptor task)
        {
            Guard.IsTypeOf <TaskDescriptor>(task);

            _db.TaskDescriptors.Remove((TaskDescriptor)task);
            return(_db.SaveChangesAsync());
        }
Exemple #2
0
        public Task InsertTaskAsync(ITaskDescriptor task)
        {
            Guard.IsTypeOf <TaskDescriptor>(task);

            _db.TaskDescriptors.Add((TaskDescriptor)task);
            return(_db.SaveChangesAsync());
        }
Exemple #3
0
 public Type GetTaskClrType(ITaskDescriptor task)
 {
     try
     {
         return(Type.GetType(task.Type));
     }
     catch
     {
         // TODO: (core) Map old task types to new types.
         return(null);
     }
 }
Exemple #4
0
        public ITaskExecutionInfo CreateExecutionInfo(ITaskDescriptor task)
        {
            Guard.NotNull(task, nameof(task));

            return(new TaskExecutionInfo
            {
                TaskDescriptorId = task.Id,
                IsRunning = true,
                MachineName = _appContext.MachineName.EmptyNull(),
                StartedOnUtc = DateTime.UtcNow
            });
        }
Exemple #5
0
        public Task UpdateTaskAsync(ITaskDescriptor task)
        {
            Guard.IsTypeOf <TaskDescriptor>(task);

            try
            {
                _db.TryUpdate((TaskDescriptor)task);
                return(_db.SaveChangesAsync());
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw;
            }
        }
Exemple #6
0
        public DateTime?GetNextSchedule(ITaskDescriptor task)
        {
            if (task.Enabled)
            {
                try
                {
                    var localTimeZone = _dtHelper.DefaultStoreTimeZone;
                    var baseTime      = TimeZoneInfo.ConvertTime(DateTime.UtcNow, localTimeZone);
                    var next          = CronExpression.GetNextSchedule(task.CronExpression, baseTime);
                    var utcTime       = _dtHelper.ConvertToUtcTime(next, localTimeZone);

                    return(utcTime);
                }
                catch (Exception ex)
                {
                    Logger.Error(ex, "Could not calculate next schedule time for task '{0}'", task.Name);
                }
            }

            return(null);
        }
Exemple #7
0
 public Task ReloadTaskAsync(ITaskDescriptor task)
 {
     Guard.IsTypeOf <TaskDescriptor>(task);
     return(_db.ReloadEntityAsync((TaskDescriptor)task));
 }
Exemple #8
0
 public Task LoadLastExecutionInfoAsync(ITaskDescriptor task, bool force = false)
 {
     throw new NotImplementedException();
 }
        public virtual async Task ExecuteAsync(
            ITaskDescriptor task,
            IDictionary <string, string> taskParameters = null,
            bool throwOnError             = false,
            CancellationToken cancelToken = default)
        {
            Guard.NotNull(task, nameof(task));

            if (_asyncRunner.AppShutdownCancellationToken.IsCancellationRequested)
            {
                return;
            }

            await _taskStore.LoadLastExecutionInfoAsync(task);

            if (task?.LastExecution?.IsRunning == true)
            {
                return;
            }

            bool      faulted   = false;
            bool      canceled  = false;
            string    lastError = null;
            ITask     job       = null;
            string    stateName = null;
            Type      taskType  = null;
            Exception exception = null;

            var executionInfo = _taskStore.CreateExecutionInfo(task);

            try
            {
                taskType = _taskStore.GetTaskClrType(task);
                if (taskType == null)
                {
                    Logger.Debug($"Invalid scheduled task type: {task.Type.NaIfEmpty()}");
                    return;
                }

                if (!_appContext.ModuleCatalog.IsActiveModuleAssembly(taskType.Assembly))
                {
                    return;
                }

                await _taskStore.InsertExecutionInfoAsync(executionInfo);
            }
            catch
            {
                // Get out on any initialization error.
                return;
            }

            try
            {
                // Task history entry has been successfully added, now we execute the task.
                // Create task instance.
                job       = _taskResolver(taskType);
                stateName = task.Id.ToString();

                // Create & set a composite CancellationTokenSource which also contains the global app shoutdown token.
                var cts = CancellationTokenSource.CreateLinkedTokenSource(_asyncRunner.AppShutdownCancellationToken, cancelToken);
                await _asyncState.CreateAsync(task, stateName, false, cts);

                // Run the task
                Logger.Debug("Executing scheduled task: {0}", task.Type);
                var ctx = new TaskExecutionContext(_taskStore, _componentContext, executionInfo, taskParameters);
                await job.Run(ctx, cts.Token);
            }
            catch (Exception ex)
            {
                exception = ex;
                faulted   = true;
                canceled  = ex is OperationCanceledException;
                lastError = ex.ToAllMessages(true);

                if (canceled)
                {
                    Logger.Warn(ex, $"The scheduled task '{task.Name}' has been canceled.");
                }
                else
                {
                    Logger.Error(ex, string.Concat($"Error while running scheduled task '{task.Name}'", ": ", ex.Message));
                }
            }
            finally
            {
                var now        = DateTime.UtcNow;
                var updateTask = false;

                executionInfo.IsRunning       = false;
                executionInfo.ProgressPercent = null;
                executionInfo.ProgressMessage = null;
                executionInfo.Error           = lastError;
                executionInfo.FinishedOnUtc   = now;

                if (faulted)
                {
                    if ((!canceled && task.StopOnError) || task == null)
                    {
                        task.Enabled = false;
                        updateTask   = true;
                    }
                }
                else
                {
                    executionInfo.SucceededOnUtc = now;
                }

                try
                {
                    Logger.Debug("Executed scheduled task: {0}. Elapsed: {1} ms.", task.Type, (now - executionInfo.StartedOnUtc).TotalMilliseconds);

                    // Remove from AsyncState.
                    if (stateName.HasValue())
                    {
                        // We don't just remove the cancellation token, but the whole state (along with the token)
                        // for the case that a state was registered during task execution.
                        await _asyncState.RemoveAsync <ITaskDescriptor>(stateName);
                    }
                }
                catch (Exception ex)
                {
                    Logger.Error(ex);
                }

                if (task.Enabled)
                {
                    task.NextRunUtc = _taskStore.GetNextSchedule(task);
                    updateTask      = true;
                }

                await _taskStore.UpdateExecutionInfoAsync(executionInfo);

                if (updateTask)
                {
                    await _taskStore.UpdateTaskAsync(task);
                }

                await Throttle.CheckAsync(
                    "Delete old schedule task history entries",
                    TimeSpan.FromDays(1),
                    async() => await _taskStore.TrimExecutionInfosAsync() > 0);
            }

            if (throwOnError && exception != null)
            {
                throw exception;
            }
        }