add crawl copy

This commit is contained in:
AlanTuring 2022-06-04 17:17:08 +08:00
parent a75b15484d
commit 8b4e49021d
3 changed files with 71 additions and 37 deletions

View File

@ -35,13 +35,13 @@ import java.util.stream.Collectors;
*/
@Service
public class SpiderFlowService extends ServiceImpl<SpiderFlowMapper, SpiderFlow> {
@Autowired
private SpiderFlowMapper sfMapper;
@Autowired
private SpiderJobManager spiderJobManager;
@Autowired
private FlowNoticeMapper flowNoticeMapper;
@ -73,15 +73,15 @@ public class SpiderFlowService extends ServiceImpl<SpiderFlowMapper, SpiderFlow>
public IPage<SpiderFlow> selectSpiderPage(Page<SpiderFlow> page, String name){
return sfMapper.selectSpiderPage(page,name);
}
public int executeCountIncrement(String id, Date lastExecuteTime, Date nextExecuteTime){
if(nextExecuteTime == null){
return sfMapper.executeCountIncrement(id, lastExecuteTime);
}
return sfMapper.executeCountIncrementAndExecuteTime(id, lastExecuteTime, nextExecuteTime);
}
/**
* 重置定时任务
* @param id 爬虫的ID
@ -130,13 +130,20 @@ public class SpiderFlowService extends ServiceImpl<SpiderFlowMapper, SpiderFlow>
}
return true;
}
public void stop(String id){
sfMapper.resetSpiderStatus(id,"0");
sfMapper.resetNextExecuteTime(id);
spiderJobManager.remove(id);
}
public void copy(String id){
// 复制ID
SpiderFlow spiderFlow = sfMapper.selectById(id);
String new_id = UUID.randomUUID().toString().replace("-", "");
sfMapper.insertSpiderFlow(new_id, spiderFlow.getName() + "-copy", spiderFlow.getXml());
}
public void start(String id){
spiderJobManager.remove(id);
SpiderFlow spiderFlow = getById(id);
@ -147,11 +154,11 @@ public class SpiderFlowService extends ServiceImpl<SpiderFlowMapper, SpiderFlow>
sfMapper.resetSpiderStatus(id, "1");
}
}
public void run(String id){
spiderJobManager.run(id);
}
public void resetExecuteCount(String id){
sfMapper.resetExecuteCount(id);
}
@ -160,11 +167,11 @@ public class SpiderFlowService extends ServiceImpl<SpiderFlowMapper, SpiderFlow>
spiderJobManager.remove(id);
flowNoticeMapper.deleteById(id);
}
public List<SpiderFlow> selectOtherFlows(String id){
return sfMapper.selectOtherFlows(id);
}
public List<SpiderFlow> selectFlows(){
return sfMapper.selectFlows();
}

View File

@ -48,29 +48,29 @@ import java.util.stream.Collectors;
@RestController
@RequestMapping("/spider")
public class SpiderFlowController {
@Autowired
private List<FunctionExecutor> functionExecutors;
@Autowired
private List<FunctionExtension> functionExtensions;
@Autowired
private List<Grammerable> grammerables;
@Autowired
private SpiderFlowService spiderFlowService;
@Autowired(required = false)
private List<PluginConfig> pluginConfigs;
@Value("${spider.workspace}")
private String workspace;
private final List<Grammer> grammers = new ArrayList<Grammer>();
private static Logger logger = LoggerFactory.getLogger(SpiderFlowController.class);
@PostConstruct
private void init(){
for (FunctionExecutor executor : functionExecutors) {
@ -84,7 +84,7 @@ public class SpiderFlowController {
grammer.setFunction(function);
grammers.add(grammer);
}
for (FunctionExtension extension : functionExtensions) {
String owner = extension.support().getSimpleName();
grammers.addAll(Grammer.findGrammers(extension.getClass(),null,owner,true));
@ -93,7 +93,7 @@ public class SpiderFlowController {
grammers.addAll(grammerable.grammers());
}
}
/**
* 爬虫列表
* @param page 页数
@ -104,7 +104,7 @@ public class SpiderFlowController {
public IPage<SpiderFlow> list(@RequestParam(name = "page", defaultValue = "1") Integer page, @RequestParam(name = "limit", defaultValue = "1") Integer size, @RequestParam(name = "name", defaultValue = "") String name) {
return spiderFlowService.selectSpiderPage(new Page<>(page, size), name);
}
@RequestMapping("/save")
public String save(SpiderFlow spiderFlow){
spiderFlowService.save(spiderFlow);
@ -119,12 +119,12 @@ public class SpiderFlowController {
return new JsonBean<>(spiderFlowService.historyList(id));
}
}
@RequestMapping("/get")
public SpiderFlow get(String id){
return spiderFlowService.getById(id);
}
@RequestMapping("/other")
public List<SpiderFlow> other(String id){
if(StringUtils.isBlank(id)){
@ -132,32 +132,37 @@ public class SpiderFlowController {
}
return spiderFlowService.selectOtherFlows(id);
}
@RequestMapping("/remove")
public void remove(String id){
spiderFlowService.remove(id);
}
@RequestMapping("/start")
public void start(String id){
spiderFlowService.start(id);
}
@RequestMapping("/stop")
public void stop(String id){
spiderFlowService.stop(id);
}
@RequestMapping("/copy")
public void copy(String id){
spiderFlowService.copy(id);
}
@RequestMapping("/run")
public void run(String id){
spiderFlowService.run(id);
}
@RequestMapping("/cron")
public void cron(String id,String cron){
spiderFlowService.resetCornExpression(id, cron);
}
@RequestMapping("/xml")
public String xml(String id){
return spiderFlowService.getById(id).getXml();
@ -192,17 +197,17 @@ public class SpiderFlowController {
return new JsonBean<>(-1,"读取日志文件出错");
}
}
@RequestMapping("/shapes")
public List<Shape> shapes(){
return ExecutorsUtils.shapes();
}
@RequestMapping("/pluginConfigs")
public List<Plugin> pluginConfigs(){
return null == pluginConfigs ? Collections.emptyList() : pluginConfigs.stream().filter(e-> e.plugin() != null).map(plugin -> plugin.plugin()).collect(Collectors.toList());
}
@RequestMapping("/grammers")
public JsonBean<List<Grammer>> grammers(){
return new JsonBean<>(this.grammers);

View File

@ -84,7 +84,7 @@
align : 'center'
},{
title : '操作',
width : 195,
width : 250,
align : 'center',
templet : '#buttons'
}]]
@ -121,6 +121,28 @@
}
$("body").on('click','.btn-search',function(){
reloadTable();
}).on('click','.btn-copy',function(){
var id = $(this).data('id');
layui.layer.confirm('您确定要复制此爬虫吗',{
title : '复制'
},function(index){
$table.reload();
$.ajax({
url : 'spider/copy',
data : {
id : id
},
success : function(){
layui.layer.msg('复制成功',{time : 500},function(){
$table.reload();
})
},
error : function(){
layui.layer.msg('复制失败')
}
})
layui.layer.close(index);
})
}).on('click','.btn-remove',function(){
var id = $(this).data('id');
layui.layer.confirm('您确定要删除此爬虫吗',{
@ -205,4 +227,4 @@
<a class="layui-btn layui-btn-sm btn-remove" data-id="{{d.id}}" title="删除"><i class="layui-icon">&#xe640;</i></a>
</script>
</body>
</html>
</html>