Ejemplo n.º 1
0
        public virtual async Task ExecuteAsync(
            TaskDescriptor task,
            HttpContext httpContext,
            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;
            }

            ITask     job = null;
            Type      taskType = null;
            Exception exception = null;
            bool      faulted = false, canceled = false;
            string    lastError = null, stateName = 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, httpContext, _componentContext, executionInfo);

                //// TODO: (core) Uncomment job.Run and remove Task.Delay()
                //await job.Run(ctx, cts.Token);
                await Task.Delay(50, 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 <TaskDescriptor>(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);
                }

                if (!canceled)
                {
                    await Throttle.CheckAsync(
                        "Delete old schedule task history entries",
                        TimeSpan.FromHours(4),
                        async() => await _taskStore.TrimExecutionInfosAsync(cancelToken) > 0);
                }
            }

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