1、拆分任务日志

This commit is contained in:
td_zhangyu 2019-12-24 14:27:29 +08:00
parent fb9b587830
commit 3e84c8ef8f
7 changed files with 26 additions and 8 deletions

View File

@ -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());

View File

@ -55,7 +55,7 @@ public interface SpiderFlowMapper extends BaseMapper<SpiderFlow>{
@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<SpiderFlow>{
@Select("select id,name from sp_flow where id != #{id}")
List<SpiderFlow> 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);
}

View File

@ -173,4 +173,8 @@ public class SpiderFlowService extends ServiceImpl<SpiderFlowMapper, SpiderFlow>
}
return list;
}
public Integer getFlowMaxTaskId(String flowId){
return sfMapper.getFlowMaxTaskId(flowId);
}
}

View File

@ -156,8 +156,12 @@ public class SpiderFlowController {
}
@RequestMapping("/log")
public JsonBean<List<Line>> 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<List<Line>> 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,"日志文件不存在");

View File

@ -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,

View File

@ -170,10 +170,12 @@
<script type="text/javascript">
$(function(){
var logId = getQueryString('id');
var taskId = getQueryString('taskId');
var viewer = new LogViewer({
url: 'spider/log',
maxLines : parseInt(($('.log-container').height() - 8) / 14),
logId : logId,
taskId: taskId,
element : $('.log-container'),
onSearchFinish : function(hasData){
if(hasData){

View File

@ -90,14 +90,14 @@
layui.layer.close(index);
})
}).on('click','.btn-log',function(){
parent.openTab(decodeURIComponent(decodeURIComponent(getQueryString('name'))) + '-日志',$(this).data('id') + '-log','log.html?id=' + $(this).data('id'));
parent.openTab(decodeURIComponent(decodeURIComponent(getQueryString('name'))) + '-日志',$(this).data('id') + '-log','log.html?id=' + $(this).data('id') + "&taskId=" + $(this).data("task"));
})
</script>
<script type="text/html" id="buttons">
{{# if(!d.endTime){ }}
<a class="layui-btn layui-btn-sm btn-stop" data-id="{{d.id}}">停止</a>
{{# } }}
<a class="layui-btn layui-btn-sm btn-log" data-id="{{d.flowId}}">查看日志</a>
<a class="layui-btn layui-btn-sm btn-log" data-id="{{d.flowId}}" data-task="{{d.id}}">查看日志</a>
<a class="layui-btn layui-btn-sm btn-remove" data-id="{{d.id}}">删除记录</a>
</script>
</body>