Пример #1
0
        /// <summary>
        /// 执行的后台任务方法。
        /// </summary>
        /// <returns>返回任务实例。</returns>
        public async Task RunAsync()
        {
            var tasks = (await _taskManager.LoadTasksAsync()).ToList();

            if (!tasks.Any())
            {
                tasks = new List <TaskDescriptor>();
            }
            foreach (var service in _services)
            {
                var task = tasks.SingleOrDefault(t => t.Type.Equals(service.GetType().DisplayName()));
                if (task == null)
                {
                    task                    = new TaskDescriptor();
                    task.Type               = service.GetType().DisplayName();
                    task.Name               = service.Name;
                    task.Description        = service.Description;
                    task.DependenceArgument = service.DependenceArgument;
                    task.TaskInterval       = service.Interval;
                    task.NextExecuting      = DateTime.Now.AddDays(-1);
                    task.ExtensionName      = service.ExtensionName;
                    await _taskManager.SaveAsync(task);

                    tasks.Add(task);
                }
                task.Service = service.ExecuteAsync;
            }
            await ExecuteAsync(tasks);
        }
Пример #2
0
        private async Task ExecuteArgumentAsync(TaskDescriptor task)
        {
            var argument = await _taskManager.GetArgumentAsync(task.Id);

            if (argument != null)
            {
                try
                {
                    //在服务运行后可以更改当前参数值
                    if (argument.Args != null)
                    {
                        argument.Args.SetArgumentAsync = args => _taskManager.SetArgumentAsync(argument.Id, args);
                    }
                    await task.Service(argument.Args);

                    await _taskManager.SetArgumentAsync(argument.Id);
                }
                catch (Exception ex)
                {
                    await
                    _taskManager.SetArgumentAsync(argument.Id, argument.TryTimes + 1,
                                                  ex.Message);

                    _taskManager.LogError(task.Name, ex);
                }
            }
            await Task.Delay(TimeSpan.FromSeconds(1));
        }
Пример #3
0
        private async Task ExecuteArgumentAsync(TaskDescriptor task)
        {
            //for (int i = 0; i < (int)ArgumentStatus.Failured; i++)
            //{
            var argument = await _taskManager.GetArgumentAsync(task.Id);

            if (argument != null)
            {
                try
                {
                    await task.Service(argument.Args);

                    await _taskManager.SetArgumentAsync(argument.Id);
                }
                catch (Exception ex)
                {
                    await
                    _taskManager.SetArgumentAsync(argument.Id, argument.TryTimes + 1,
                                                  ex.Message);

                    _taskManager.LogError(task.Name, ex);
                }
            }
            await Task.Delay(TimeSpan.FromSeconds(1));

            //}
        }
Пример #4
0
 /// <summary>
 /// 保存后台服务实例对象。
 /// </summary>
 /// <param name="task">后台服务实例对象。</param>
 public async Task SaveAsync(TaskDescriptor task)
 {
     await _repository.CreateAsync(task);
 }