Skip to content

Commit

Permalink
做了定时任务同步功能
Browse files Browse the repository at this point in the history
  • Loading branch information
Deali-Axy committed Jan 24, 2022
1 parent 3140d47 commit d0549a4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
21 changes: 21 additions & 0 deletions CrawlCenter.Web/Controllers/RecurringTaskController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
using CrawlCenter.Contrib.WebMessages;
using CrawlCenter.Data.Models;
using CrawlCenter.Data.Repositories;
using CrawlCenter.Web.Tasks;
using CrawlCenter.Web.ViewModels.RecurringTasks;
using Hangfire;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
Expand Down Expand Up @@ -93,12 +95,31 @@ public IActionResult Edit(RecurringTaskEditViewModel viewModel) {

[HttpPost]
public IActionResult Delete([FromForm] Guid id) {
var job = _recurringTaskRepo.GetById(id);
if (job == null) return NotFound();
RecurringJob.RemoveIfExists(job.Id.ToString());

var affectRows = _recurringTaskRepo.Delete(id);
if (affectRows > 0)
_messages.Success($"删除定时任务 {id} 成功!");
else
_messages.Error($"删除定时任务 {id} 失败!");
return RedirectToAction(nameof(Index));
}

[HttpGet]
public IActionResult SyncData() {
var jobs = _recurringTaskRepo.GetAll().ToList();
foreach (var job in jobs) {
RecurringJob.RemoveIfExists(job.Id.ToString());
RecurringJob.AddOrUpdate(job.Id.ToString(),
() => new RunCrawl().Run(job.CrawlTask),
() => job.Cron);
}

_messages.Info($"已同步{jobs.Count}个定时任务!");

return RedirectToAction(nameof(Index));
}
}
}
4 changes: 4 additions & 0 deletions CrawlCenter.Web/Views/RecurringTask/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
<span data-feather="plus"></span>
新增
</a>
<a class="btn btn-outline-success" asp-controller="RecurringTask" asp-action="SyncData">
<span data-feather="refresh-cw"></span>
同步
</a>
</div>
</div>
</div>
Expand Down

0 comments on commit d0549a4

Please sign in to comment.