Пример #1
0
        public async Task <IActionResult> CreateAsync(CreateSpiderViewModel viewModel)
        {
            // POST 过来的镜像仓库是选择出来的,如果不正确表明可能是其它 HACK 过来的数据, 直接抛 404即可
            var dockerRepository = await _dbContext.DockerRepositories.FirstOrDefaultAsync(x =>
                                                                                           x.Registry == viewModel.Registry && x.Repository == viewModel.Repository);

            if (dockerRepository == null)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                var exists = await _dbContext.Spiders.AnyAsync(x =>
                                                               x.Name == viewModel.Name);

                if (exists)
                {
                    ModelState.AddModelError("Name", "名称已经存在");
                }

                try
                {
                    TriggerBuilder.Create().WithCronSchedule(viewModel.Cron).Build();
                }
                catch
                {
                    ModelState.AddModelError("Cron", "Cron 表达式不正确");
                }

                if (ModelState.IsValid)
                {
                    var transaction = await _dbContext.Database.BeginTransactionAsync();

                    try
                    {
                        var spider = new Portal.Entity.Spider
                        {
                            Name         = viewModel.Name,
                            Cron         = viewModel.Cron,
                            Type         = viewModel.Type,
                            Registry     = dockerRepository.Registry,
                            Repository   = viewModel.Repository,
                            Environment  = viewModel.Environment,
                            Tag          = viewModel.Tag,
                            CreationTime = DateTime.Now
                        };
                        await _dbContext.Spiders.AddAsync(spider);

                        await _dbContext.SaveChangesAsync();

                        var id = spider.Id.ToString();
                        await ScheduleJobAsync(viewModel.Cron, id, spider.Name);

                        transaction.Commit();

                        return(Redirect("/spider"));
                    }
                    catch (Exception e)
                    {
                        _logger.LogError($"添加任务失败: {e}");
                        try
                        {
                            transaction.Rollback();
                        }
                        catch (Exception re)
                        {
                            _logger.LogError($"回滚添加任务失败: {re}");
                        }
                    }
                }
            }

            if (!string.IsNullOrWhiteSpace(viewModel.Registry))
            {
                viewModel.Tags = await GetRepositoryTagsAsync(dockerRepository.Schema, dockerRepository.Registry,
                                                              dockerRepository.Repository,
                                                              dockerRepository.UserName, dockerRepository.Password);
            }

            return(View("Create", viewModel));
        }
Пример #2
0
        public async Task <IActionResult> Add(AddSpiderViewModel dto)
        {
            if (!ModelState.IsValid)
            {
                return(await Add(dto.Repository));
            }

            var exists = await _dbContext.Spiders.AnyAsync(x =>
                                                           x.Name == dto.Name);

            if (exists)
            {
                ModelState.AddModelError("Name", "名称已经存在");
            }

            var dockerRepository = await _dbContext.DockerRepositories.FirstOrDefaultAsync(x =>
                                                                                           $"{x.Registry}{x.Repository}".Replace("http://", "").Replace("https://", "") == dto.Repository);

            if (dockerRepository == null)
            {
                ModelState.AddModelError("Repository", "镜像仓库不存在");
                return(await Add(dto.Repository));
            }

            try
            {
                TriggerBuilder.Create().WithCronSchedule(dto.Cron).Build();
            }
            catch
            {
                ModelState.AddModelError("Cron", "CRON 表达式不正确");
            }

            if (ModelState.IsValid)
            {
                // var transaction = await _dbContext.Database.BeginTransactionAsync();
                try
                {
                    var spider = new Portal.Entity.Spider
                    {
                        Name         = dto.Name,
                        Cron         = dto.Cron,
                        Type         = dto.Type,
                        Registry     = dockerRepository.Registry,
                        Repository   = dto.Repository,
                        Environment  = dto.Environment,
                        Tag          = dto.Tag,
                        Arguments    = dto.Arguments,
                        CreationTime = DateTime.Now
                    };
                    _dbContext.Spiders.Add(spider);
                    await _dbContext.SaveChangesAsync();

                    var id      = spider.Id.ToString();
                    var trigger = TriggerBuilder.Create().WithCronSchedule(dto.Cron).WithIdentity(id).Build();
                    var qzJob   = JobBuilder.Create <TriggerJob>().WithIdentity(id).WithDescription(spider.Name)
                                  .RequestRecovery(true).StoreDurably(true)
                                  .Build();
                    await _sched.ScheduleJob(qzJob, trigger);

                    //transaction.Commit();
                }
                catch (Exception e)
                {
                    _logger.LogError($"添加任务失败: {e}");
//					try
//					{
//						transaction.Rollback();
//					}
//					catch (Exception re)
//					{
//						_logger.LogError($"回滚添加任务失败: {re}");
//					}

                    ModelState.AddModelError(string.Empty, "添加任务失败");
                    return(View("Add", dto));
                }

                return(Redirect("/spider"));
            }
            else
            {
                return(View("Add", dto));
            }
        }
Пример #3
0
        public async Task <IActionResult> Add(AddSpiderViewModel dto)
        {
            if (!ModelState.IsValid)
            {
                return(View("Add", dto));
            }

            var exists = await _dbContext.Spiders.AnyAsync(x =>
                                                           x.Name == dto.Name);

            if (exists)
            {
                ModelState.AddModelError("Name", "名称已经存在");
            }

            var imageExists = await _dbContext.DockerImages.AnyAsync(x => x.Image == dto.Image);

            if (!imageExists)
            {
                ModelState.AddModelError("Image", "镜像不存在");
            }

            try
            {
                TriggerBuilder.Create().WithCronSchedule(dto.Cron).Build();
            }
            catch
            {
                ModelState.AddModelError("Cron", "CRON 表达式不正确");
            }

            if (ModelState.IsValid)
            {
                var transaction = await _dbContext.Database.BeginTransactionAsync();

                try
                {
                    var spider = new Portal.Entity.Spider
                    {
                        Name                 = dto.Name,
                        Cron                 = dto.Cron,
                        Image                = dto.Image,
                        Environment          = dto.Environment,
                        CreationTime         = DateTime.Now,
                        LastModificationTime = DateTime.Now
                    };
                    _dbContext.Spiders.Add(spider);
                    await _dbContext.SaveChangesAsync();

                    var id      = spider.Id.ToString();
                    var trigger = TriggerBuilder.Create().WithCronSchedule(dto.Cron).WithIdentity(id).Build();
                    var qzJob   = JobBuilder.Create <TriggerJob>().WithIdentity(id).WithDescription(spider.Name)
                                  .RequestRecovery(true)
                                  .Build();
                    await _sched.ScheduleJob(qzJob, trigger);

                    transaction.Commit();
                }
                catch (Exception e)
                {
                    _logger.LogError($"添加任务失败: {e}");
                    try
                    {
                        transaction.Rollback();
                    }
                    catch (Exception re)
                    {
                        _logger.LogError($"回滚添加任务失败: {re}");
                    }

                    ModelState.AddModelError(string.Empty, "添加任务失败");
                    return(View("Add", dto));
                }

                return(Redirect("/spider"));
            }
            else
            {
                return(View("Add", dto));
            }
        }