private void AddUpgradSchedulerJob()
        {
            Core.Entities.Task upgradeScheduler = DbContext.Task.FirstOrDefault(t => t.Name.StartsWith(UpgradeTaskSchedulerTaskName));
            if (upgradeScheduler == null)
            {
                upgradeScheduler = new Core.Entities.Task
                {
                    ApplicationName  = "DotnetSpider.Hub",
                    Cron             = $"* * * * 2999",
                    IsEnabled        = true,
                    IsDeleted        = true,
                    Developers       = "DotnetSpider.Hub",
                    Owners           = "DotnetSpider.Hub",
                    Arguments        = "",
                    NodeCount        = 1,
                    NodeRunningCount = 0,
                    Name             = UpgradeTaskSchedulerTaskName,
                    Version          = "0001",
                    NodeType         = 1
                };
                DbContext.Task.Add(upgradeScheduler);
                DbContext.SaveChanges();
            }
            var taskId = upgradeScheduler.Id.ToString();
            var job    = new SchedulerJobDto
            {
                Id   = taskId,
                Name = upgradeScheduler.Name,
                Cron = "* * * * 2999",
                Url  = string.Format(Configuration.SchedulerCallback, taskId),
                Data = JsonConvert.SerializeObject(new { TaskId = taskId })
            };

            _schedulerAppService.Create(job);
        }
        private void AddScanRunningTaskJob()
        {
            Core.Entities.Task scanRunningTask = DbContext.Task.FirstOrDefault(t => t.Name == ScanRunningTaskJobName);
            if (scanRunningTask == null)
            {
                scanRunningTask = new Core.Entities.Task
                {
                    ApplicationName  = "DotnetSpider.Hub",
                    Cron             = $"0/15 * * * *",
                    IsEnabled        = true,
                    IsDeleted        = true,
                    Developers       = "DotnetSpider",
                    Owners           = "DotnetSpider",
                    Arguments        = "",
                    NodeCount        = 1,
                    NodeRunningCount = 0,
                    Name             = ScanRunningTaskJobName,
                    Package          = "0001",
                    NodeType         = "HUB"
                };
                DbContext.Task.Add(scanRunningTask);
                DbContext.SaveChanges();
            }
            var taskId = scanRunningTask.Id.ToString();
            var job    = new SchedulerJobDto
            {
                Id   = taskId,
                Name = scanRunningTask.Name,
                Cron = "0/15 * * * *",
                Url  = string.Format(Configuration.SchedulerCallback, taskId),
                Data = JsonConvert.SerializeObject(new { TaskId = taskId })
            };

            _schedulerAppService.Create(job);
        }
        public void Register()
        {
            Domain.Entities.Task scanRunningTask = DbContext.Task.FirstOrDefault(t => t.Name.StartsWith(ScanRunningTaskName));
            if (scanRunningTask == null)
            {
                scanRunningTask = new Domain.Entities.Task
                {
                    ApplicationName  = "DotnetSpider.Enterprise",
                    Cron             = $"0/15 * * * *",
                    IsEnabled        = true,
                    IsDeleted        = true,
                    Developers       = "DotnetSpider",
                    Owners           = "DotnetSpider",
                    Arguments        = "",
                    NodeCount        = 1,
                    NodeRunningCount = 0,
                    Name             = ScanRunningTaskName,
                    Version          = "0001",
                    NodeType         = 1
                };
                DbContext.Task.Add(scanRunningTask);
                DbContext.SaveChanges();
            }
            var taskId = scanRunningTask.Id.ToString();
            var job    = new SchedulerJobDto
            {
                Id   = taskId,
                Name = scanRunningTask.Name,
                Cron = "0/15 * * * *",
                Url  = string.Format(Configuration.SchedulerCallback, taskId),
                Data = JsonConvert.SerializeObject(new { TaskId = taskId })
            };

            _schedulerAppService.Create(job);
        }
Beispiel #4
0
        public void Enable(string taskId)
        {
            var task = DbContext.Task.FirstOrDefault(a => a.Id == taskId);

            if (task == null)
            {
                throw new DotnetSpiderHubException("任务不存在!");
            }
            task.IsEnabled = true;

            var taskIdStr = taskId.ToString();
            var job       = new SchedulerJobDto
            {
                Id      = taskIdStr,
                Name    = task.Name,
                Cron    = task.Cron,
                Url     = string.Format(Configuration.SchedulerCallback, taskId),
                Content = JsonConvert.SerializeObject(new { TaskId = taskIdStr })
            };

            _schedulerAppService.Create(job);
            DbContext.Task.Update(task);
            DbContext.SaveChanges();
            Logger.Information($"Enable task {taskId}.");
        }
Beispiel #5
0
        public void Update(UpdateTaskInput input)
        {
            if (input == null)
            {
                Logger.Error($"{nameof(input)} should not be null.");
                return;
            }
            var task = DbContext.Task.FirstOrDefault(a => a.Id == input.Id);

            if (task == null)
            {
                throw new Exception("Unfound task.");
            }
            task.Analysts        = input.Analysts?.Trim();
            task.ApplicationName = input.ApplicationName?.Trim();
            task.Arguments       = input.Arguments?.Trim();

            task.Cron        = input.Cron;
            task.Description = input.Description?.Trim();
            task.Developers  = input.Developers?.Trim();

            task.Name             = input.Name?.Trim();
            task.NodeCount        = input.NodeCount;
            task.NodeRunningCount = input.NodeRunningCount;
            task.Os       = input.Os?.Trim();
            task.Owners   = input.Owners?.Trim();
            task.Tags     = input.Tags?.Trim();
            task.NodeType = input.NodeType;
            task.Package  = input.Package?.Trim();
            task.IsSingle = input.IsSingle;

            if (!input.IsEnabled && task.IsEnabled && task.Cron == Configuration.IngoreCron)
            {
                _schedulerAppService.Delete(task.Id.ToString());
            }

            if (input.IsEnabled)
            {
                var taskId = task.Id.ToString();
                var job    = new SchedulerJobDto
                {
                    Id      = taskId,
                    Name    = task.Name,
                    Cron    = task.Cron,
                    Url     = string.Format(Configuration.SchedulerCallback, taskId),
                    Content = JsonConvert.SerializeObject(new { TaskId = taskId })
                };

                if (!task.Name.StartsWith(job.Group))
                {
                    task.Name = $"[{job.Group}]{task.Name}";
                }

                _schedulerAppService.Update(job);
            }

            task.IsEnabled = input.IsEnabled;
            DbContext.Task.Update(task);
            DbContext.SaveChanges();
        }
        public void Create(CreateTaskInput input)
        {
            if (input == null)
            {
                Logger.Error($"{nameof(input)} should not be null.");
                return;
            }
            var task = Mapper.Map <Core.Entities.Task>(input);

            input.ApplicationName = input.ApplicationName.Trim();
            input.Arguments       = input.Arguments?.Trim();
            input.Cron            = input.Cron.Trim();
            input.Package         = input.Package.Trim();
            input.Name            = input.Name.Trim();

            if (input.Cron != Configuration.IngoreCron)
            {
                var job = new SchedulerJobDto
                {
                    Id   = task.Id,
                    Name = task.Name,
                    Cron = input.Cron,
                    Url  = string.Format(Configuration.SchedulerCallback, task.Id),
                    Data = JsonConvert.SerializeObject(new { TaskId = task.Id })
                };
                _schedulerAppService.Create(job);
            }

            DbContext.Task.Add(task);
            DbContext.SaveChanges();
        }
        public void Create(SchedulerJobDto job)
        {
            var content = new StringContent(JsonConvert.SerializeObject(job), Encoding.UTF8, "application/json");

            try
            {
                var result = _retryPolicy.Execute(() =>
                {
                    var response = HttpClientUtil.DefaultClient.PostAsync(Configuration.SchedulerUrl, content).Result;
                    response.EnsureSuccessStatusCode();
                    return(response);
                });

                CheckResult(result);
            }
            catch (Exception e)
            {
                throw new DotnetSpiderException($"Create scheduler failed: {e.Message}.");
            }
        }
Beispiel #8
0
        /// <summary>
        /// 添加一个任务
        /// </summary>
        /// <param name="input"></param>
        public void Create(CreateTaskInput input)
        {
            if (input == null)
            {
                Logger.Error($"{nameof(input)} should not be null.");
                return;
            }
            //映射到任务对象
            var task = Mapper.Map <Core.Entities.Task>(input);

            input.ApplicationName = input.ApplicationName.Trim();
            input.Arguments       = input.Arguments?.Trim();
            input.Cron            = input.Cron.Trim();
            input.Package         = input.Package.Trim();
            input.Name            = input.Name.Trim();

            //表示要创建议计划?
            if (input.Cron != Configuration.IngoreCron)
            {
                //计划工作
                var job = new SchedulerJobDto
                {
                    Id      = task.Id,
                    Name    = task.Name,
                    Cron    = input.Cron,
                    Url     = string.Format(Configuration.SchedulerCallback, task.Id),                 //设置回调
                    Content = JsonConvert.SerializeObject(new { TaskId = task.Id })
                };

                if (!task.Name.StartsWith(job.Group))
                {
                    task.Name = $"[{job.Group}]{task.Name}";
                }

                _schedulerAppService.Create(job); //创建计划
            }

            DbContext.Task.Add(task);             //添加任务
            DbContext.SaveChanges();
        }
        public void Create(CreateTaskInput input)
        {
            if (input == null)
            {
                Logger.LogError($"{nameof(input)} should not be null.");
                return;
            }
            var task = Mapper.Map <Domain.Entities.Task>(input);

            input.ApplicationName = input.ApplicationName.Trim();
            input.Arguments       = input.Arguments?.Trim();
            input.Cron            = input.Cron.Trim();
            input.Version         = input.Version.Trim();
            input.Name            = input.Name.Trim();

            var cron = task.Cron;

            task.Cron = DotnetSpiderConsts.UnTriggerCron;
            DbContext.Task.Add(task);
            DbContext.SaveChanges();

            if (cron != DotnetSpiderConsts.UnTriggerCron)
            {
                var taskId = task.Id.ToString();
                var job    = new SchedulerJobDto
                {
                    Id   = taskId,
                    Name = task.Name,
                    Cron = cron,
                    Url  = string.Format(Configuration.SchedulerCallback, taskId),
                    Data = JsonConvert.SerializeObject(new { TaskId = taskId })
                };
                _schedulerAppService.Create(job);
                task.Cron = cron;
                DbContext.Task.Update(task);
                DbContext.SaveChanges();
            }
        }
 public void UpgradeScheduler()
 {
     foreach (var task in DbContext.Task)
     {
         if (task.Cron == Configuration.IngoreCron)
         {
             _schedulerAppService.Delete(task.Id.ToString());
         }
         else
         {
             var taskId = task.Id.ToString();
             var job    = new SchedulerJobDto
             {
                 Id   = taskId,
                 Name = task.Name,
                 Cron = task.Cron,
                 Url  = string.Format(Configuration.SchedulerCallback, taskId),
                 Data = JsonConvert.SerializeObject(new { TaskId = taskId })
             };
             _schedulerAppService.Create(job);
         }
     }
 }