From 3e84c8ef8fbbf1c1c0c8c38dc6aadc79541effde Mon Sep 17 00:00:00 2001 From: td_zhangyu <457760778@qq.com> Date: Tue, 24 Dec 2019 14:27:29 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E6=8B=86=E5=88=86=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/org/spiderflow/core/job/SpiderJob.java | 9 ++++++--- .../org/spiderflow/core/mapper/SpiderFlowMapper.java | 5 ++++- .../org/spiderflow/core/service/SpiderFlowService.java | 4 ++++ .../org/spiderflow/controller/SpiderFlowController.java | 8 ++++++-- .../src/main/resources/static/js/log-viewer.js | 2 ++ spider-flow-web/src/main/resources/static/log.html | 2 ++ spider-flow-web/src/main/resources/static/task.html | 4 ++-- 7 files changed, 26 insertions(+), 8 deletions(-) diff --git a/spider-flow-core/src/main/java/org/spiderflow/core/job/SpiderJob.java b/spider-flow-core/src/main/java/org/spiderflow/core/job/SpiderJob.java index d49764c..a106b04 100644 --- a/spider-flow-core/src/main/java/org/spiderflow/core/job/SpiderJob.java +++ b/spider-flow-core/src/main/java/org/spiderflow/core/job/SpiderJob.java @@ -62,14 +62,15 @@ public class SpiderJob extends QuartzJobBean { } public void run(SpiderFlow spiderFlow, Date nextExecuteTime) { + SpiderJobContext context = null; Date now = new Date(); - SpiderJobContext context = SpiderJobContext.create(this.spiderLogPath, spiderFlow.getId() + ".log"); - SpiderContextHolder.set(context); Task task = new Task(); task.setFlowId(spiderFlow.getId()); task.setBeginTime(new Date()); try { taskService.save(task); + context = SpiderJobContext.create(this.spiderLogPath, spiderFlow.getId() + task.getId() + ".log"); + SpiderContextHolder.set(context); contextMap.put(task.getId(), context); logger.info("开始执行任务{}", spiderFlow.getName()); spider.run(spiderFlow, context); @@ -77,7 +78,9 @@ public class SpiderJob extends QuartzJobBean { } catch (Exception e) { logger.error("执行任务{}出错", spiderFlow.getName(), e); } finally { - context.close(); + if (context != null) { + context.close(); + } task.setEndTime(new Date()); taskService.saveOrUpdate(task); contextMap.remove(task.getId()); diff --git a/spider-flow-core/src/main/java/org/spiderflow/core/mapper/SpiderFlowMapper.java b/spider-flow-core/src/main/java/org/spiderflow/core/mapper/SpiderFlowMapper.java index 8f9086e..a092295 100644 --- a/spider-flow-core/src/main/java/org/spiderflow/core/mapper/SpiderFlowMapper.java +++ b/spider-flow-core/src/main/java/org/spiderflow/core/mapper/SpiderFlowMapper.java @@ -55,7 +55,7 @@ public interface SpiderFlowMapper extends BaseMapper{ @Update("update sp_flow set enabled = #{enabled} where id = #{id}") int resetSpiderStatus(@Param("id") String id, @Param("enabled") String enabled); - @Select("update sp_flow set next_execute_time = null where id = #{id}") + @Update("update sp_flow set next_execute_time = null where id = #{id}") int resetNextExecuteTime(@Param("id") String id); @Select("select id,name from sp_flow") @@ -63,4 +63,7 @@ public interface SpiderFlowMapper extends BaseMapper{ @Select("select id,name from sp_flow where id != #{id}") List selectOtherFlows(@Param("id") String id); + + @Select("select max(a.id) from `sp_task` a left join sp_flow b on a.flow_id = b.id where b.id = #{id}") + int getFlowMaxTaskId(@Param("id")String id); } diff --git a/spider-flow-core/src/main/java/org/spiderflow/core/service/SpiderFlowService.java b/spider-flow-core/src/main/java/org/spiderflow/core/service/SpiderFlowService.java index 1e1263d..a85d190 100644 --- a/spider-flow-core/src/main/java/org/spiderflow/core/service/SpiderFlowService.java +++ b/spider-flow-core/src/main/java/org/spiderflow/core/service/SpiderFlowService.java @@ -173,4 +173,8 @@ public class SpiderFlowService extends ServiceImpl } return list; } + + public Integer getFlowMaxTaskId(String flowId){ + return sfMapper.getFlowMaxTaskId(flowId); + } } diff --git a/spider-flow-web/src/main/java/org/spiderflow/controller/SpiderFlowController.java b/spider-flow-web/src/main/java/org/spiderflow/controller/SpiderFlowController.java index 68824fc..fa59cce 100644 --- a/spider-flow-web/src/main/java/org/spiderflow/controller/SpiderFlowController.java +++ b/spider-flow-web/src/main/java/org/spiderflow/controller/SpiderFlowController.java @@ -156,8 +156,12 @@ public class SpiderFlowController { } @RequestMapping("/log") - public JsonBean> log(String id, String keywords, Long index, Integer count, Boolean reversed,Boolean matchcase,Boolean regx){ - try (RandomAccessFileReader reader = new RandomAccessFileReader(new RandomAccessFile(new File(spiderLogPath,id + ".log"),"r"), index == null ? -1 : index, reversed == null || reversed)){ + public JsonBean> log(String id, String taskId, String keywords, Long index, Integer count, Boolean reversed, Boolean matchcase, Boolean regx) { + if (StringUtils.isBlank(taskId)) { + Integer maxId = spiderFlowService.getFlowMaxTaskId(id); + taskId = maxId == null ? "" : maxId.toString(); + } + try (RandomAccessFileReader reader = new RandomAccessFileReader(new RandomAccessFile(new File(spiderLogPath,id + taskId +".log"),"r"), index == null ? -1 : index, reversed == null || reversed)){ return new JsonBean<>(reader.readLine(count == null ? 10 : count,keywords,matchcase != null && matchcase,regx != null && regx)); } catch(FileNotFoundException e){ return new JsonBean<>(0,"日志文件不存在"); diff --git a/spider-flow-web/src/main/resources/static/js/log-viewer.js b/spider-flow-web/src/main/resources/static/js/log-viewer.js index 6c811dd..591f81a 100644 --- a/spider-flow-web/src/main/resources/static/js/log-viewer.js +++ b/spider-flow-web/src/main/resources/static/js/log-viewer.js @@ -5,6 +5,7 @@ function LogViewer(options){ this.onSearchFinish = options.onSearchFinish || function(){}; this.bufferSize = this.maxLines * 10; this.logId = options.logId; + this.taskId = options.taskId; this.url = options.url; this.buffer = []; this.displayIndex = -1; @@ -148,6 +149,7 @@ LogViewer.prototype.loadLines = function(count,callback,async){ reversed : this.reversed, count : this.bufferSize, id : this.logId, + taskId: this.taskId, index : _this.index, keywords : this.keywords, matchcase : this.matchcase, diff --git a/spider-flow-web/src/main/resources/static/log.html b/spider-flow-web/src/main/resources/static/log.html index 094672d..b221b74 100644 --- a/spider-flow-web/src/main/resources/static/log.html +++ b/spider-flow-web/src/main/resources/static/log.html @@ -170,10 +170,12 @@