public async Task <bool> CreateAsync(SpiderDto viewModel) { if (ModelState.IsValid) { var exists = await _dockerCrawlerService.CountAsync <SpiderDto>(x => x.Name == viewModel.Name); if (exists > 0) { ModelState.AddModelError("Name", "名称已经存在"); } try { CrontabSchedule.Parse(viewModel.Cron); } catch { ModelState.AddModelError("Cron", "Cron 表达式不正确"); } viewModel.CreationTime = DateTimeOffset.Now; viewModel.LastModificationTime = DateTimeOffset.Now; viewModel = await _dockerCrawlerService.AddAsync(viewModel); var id = viewModel.Id; return(true); } else { throw new ApplicationException("ModelState is invalid"); } }
public async Task <bool> UpdateAsync(int id, SpiderDto viewModel) { if (ModelState.IsValid) { var spider = await _dockerCrawlerService.Get <SpiderDto>(x => x.Id == id); if (spider == null) { throw new ApplicationException($"Spider {id} exists"); } var exists = await _dockerCrawlerService.CountAsync <SpiderDto>(x => x.Name == viewModel.Name && x.Id != id); if (exists > 0) { ModelState.AddModelError("Name", "名称已经存在"); } try { CrontabSchedule.Parse(viewModel.Cron); } catch { ModelState.AddModelError("Cron", "Cron 表达式不正确"); } spider.Name = viewModel.Name; spider.Cron = viewModel.Cron; spider.Image = viewModel.Image; spider.Volume = viewModel.Volume; spider.Environment = viewModel.Environment; spider.LastModificationTime = DateTimeOffset.Now; await _dockerCrawlerService.AddOrUpdate <SpiderDto, int>(spider); _recurringJobManager.RemoveIfExists(spider.Name); await ScheduleJobAsync(spider); return(true); } else { throw new ApplicationException("ModelState is invalid"); } }
private async Task ScheduleJobAsync(SpiderDto spider) { _recurringJobManager.AddOrUpdate <JobService>(spider.Name, x => x.ExecuteJob(spider.Id), spider.Cron, TimeZoneInfo.Local); await Task.CompletedTask; }