Authored by 郝肖肖

'task-log'

... ... @@ -3,6 +3,7 @@
const Router = require('koa-router');
const _ = require('lodash');
const md5 = require('md5');
const moment = require('moment');
const pager = require('../utils/pager');
const seoModel = require('../models/seoModel');
... ... @@ -16,12 +17,12 @@ const TYPE_LIST = [
];
const JOB_TASK = [
{
k: 'global:yoho:seo:task:minute',
k: 'global:yoho:task:minute',
v: '1 */10 * * * *',
info: '每10分钟执行一次'
},
{
k: 'global:yoho:seo:task:day',
k: 'global:yoho:task:day',
v: '30 30 1 * * *',
info: '每天的1点30分30s执行一次'
}
... ... @@ -694,17 +695,23 @@ const task = {
});
},
log: async(ctx, next) => {
let key = ctx.query.key || '';
let resData = {
typeList: [
{type: 'text', name: '文字友链'},
{type: 'img', name: '图片友链'},
]
};
let params = ctx.request.body || {};
let key = params.code || '';
let start = params.start || 0;
let stop = params.stop || 100;
return ctx.redis.lrangeAsync(`global:yoho:task:log:${key}`, start, stop).then(rdata => {
let data = _.map(rdata, item => {
item = JSON.parse(item || '{}');
return Object.assign(item, {time: moment(item.time).format('YYYY-MM-DD HH:mm:ss')});
});
await ctx.render('action/seo_task', Object.assign(resData, {
title: '定时任务日志',
}));
return ctx.body = {
code: 200,
message: 'success',
data: data
};
});
},
};
... ...
... ... @@ -21,7 +21,7 @@
<a class="btn btn-default deleteAll">删除</a>
<a class="btn btn-default" href="/keywords/add">增加</a>
<a class="btn btn-default" href="javascript:sendUrl();">推送百度</a>
<a class="btn btn-default rand-words" href="javascript:void(0);">关键词随机关联</a>
<!--<a class="btn btn-default rand-words" href="javascript:void(0);">关键词随机关联</a>-->
<div class="input-append pull-right">
<form id="query-form" action="/keywords/expand" class="query-form" method="get">
<!-- <div class="btn-group">
... ... @@ -272,4 +272,4 @@
alert(data.message);
})
}
</script>
\ No newline at end of file
</script>
... ...
<style type="text/css">
.add-task ul {
.task-add {
padding-top: 20px;
}
.task-add ul {
list-style: none;
}
.add-task ul li {
.task-add ul li {
height: 40px;
}
.add-task select,
.add-task input {
.task-add select,
.task-add input {
width: 70%;
height: 30px;
padding: 5px;
}
.task-add .job-select {
margin-left: -3px;
}
.task-log {
padding: 20px;
}
.task-log table, .task-log table tr th,.task-log table tr td {
border: none;
}
.task-log tbody tr {
height: 40px;
}
.task-log table {
border-collapse: separate;
border-spacing: 0 4px;
}
.task-log tbody tr:nth-child(odd) td:first-child {
border-left: 2px solid #00C656;
}
.task-log tbody tr:nth-child(even) td:first-child {
border-left: 2px solid #4F95E8;
}
.task-log tbody tr.empty td {
border: none !important;
}
</style>
<div class="pageheader">
<div class="media">
... ... @@ -57,12 +86,12 @@
{{#each tasks}}
<tr data-code="{{code}}" data-type="{{type}}">
<td>&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" /></td>
<td class="text-center" >{{name}}</td>
<td class="text-center td-name" >{{name}}</td>
<td class="text-center" >{{url}}</td>
<td class="text-center" >{{time}}</td>
<td class="text-center" >{{job_name}}</td>
<td class="text-center">
<a href="#pop" class="edit-btn" data-toggle="modal">执行日志</a>
<a href="javascript:;" class="log-btn">执行日志</a>
<a href="javascript:;" class="del-btn">删除</a>
</td>
</tr>
... ... @@ -96,8 +125,7 @@
</div><!--/contentpanel-->
<script id="task-add-tpl" type="text/tmpl">
<br />
<div class="add-task">
<div class="task-add">
<ul>
<li>
执行频率:
... ... @@ -111,16 +139,34 @@
<li>任务名称:<input type="text" class="job-name" placeholder='请输入任务名称...'></li>
<li>执行链接:<input type="text" class="job-link" placeholder='请输入执行链接...'></li>
<li>延迟时间:<input type="text" class="job-time" placeholder='请输入延迟时间...'></li>
<li><span class="err-tip"></span></li>
</ul>
<p><span class="err-tip"></span></p>
</div>
</script>
<script id="task-log-tpl" type="text/tmpl">
<div class="task-log">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th class="text-center" width="20%">执行时间</th>
<th class="text-center" width="20%">状态码</th>
<th class="text-center" width="50%">执行结果</th>
<th class="text-right" width="10%">
<a href="javascript:;" class="btn btn-default btn-refresh-log">刷新</a>
</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</script>
<script>
var tasks = {
init: function() {
var that = this;
var taskData = {};
this.$base = $('.contentpanel');
... ... @@ -134,6 +180,36 @@
that.del([$tr.data()], () => $tr.remove());
});
// 日志
this.$base.on('click', '.log-btn', function() {
let $tr = $(this).closest('tr');
taskData = $tr.data();
return that.getTaskLog(taskData).then(rdata => {
layer.open({
type: 1,
area: ['800px', '500px'],
title: '《' + $tr.find('.td-name').text() + '》执行明细',
content: that.taskLogTpl
});
that.rendLog(rdata.data);
});
});
// 日志刷新
$('body').on('click', '.btn-refresh-log', function() {
var layerIndex = layer.load(1);
return that.getTaskLog(taskData).then(rdata => {
setTimeout(function() {
layer.close(layerIndex);
that.rendLog(rdata.data);
}, 2000);
});
});
// 删除全部
this.$base.on('click', '.delete-all', function() {
let arr = [];
... ... @@ -144,15 +220,17 @@
that.del(arr, () => history.go(0));
});
this.taskAddTpl = $('#task-add-tpl').html();
this.taskLogTpl = $('#task-log-tpl').html();
},
add: function() {
var that = this;
var layerIndex = layer.open({
type: 1,
width: 500,
area: ['500px'],
area: ['450px'],
title: '任务添加',
content: $('#task-add-tpl').html(),
content: that.taskAddTpl,
btn: '添加',
yes: function() {
var data = {
... ... @@ -163,11 +241,11 @@
};
if (data.type < 0) {
return that.tips('执行频率必填...', '.job-select');
return that.tips('请选择执行频率...', '.job-select');
} else if (data.name === '') {
return that.tips('任务名称必填...', '.job-name');
} else if (data.url === '') {
return that.tips('执行链接必填...', '.job-link');
} else if (data.url === '' || data.url.indexOf('http') === -1) {
return that.tips('请输入正确的执行链接...', '.job-link');
}
return that.ajax({
... ... @@ -187,6 +265,10 @@
});
},
del: function(data, callback) {
if (data.length <= 0) {
return false;
}
var that = this;
var layerIndex = layer.confirm('您确定要删除吗!!!', {
btn: ['确定', '取消']
... ... @@ -202,13 +284,38 @@
});
});
},
rendLog: function(rdata) {
var $tbody = $('.task-log tbody');
$tbody.empty();
if (rdata.length <= 0) {
$tbody.append(`<tr class="empty">
<td class="text-center" colspan="4">暂无数据</td>
</tr>`);
} else {
$.each(rdata, (key, item)=> {
$tbody.append(`<tr>
<td class="text-center" >${item.time}</td>
<td class="text-center" >${item.code}</td>
<td class="text-center" colspan="2">${item.message}</td>
</tr>`);
});
}
},
getTaskLog: function(data) {
return this.ajax({
url: '/seo/task/log',
type: 'POST',
data: data,
});
},
ajax: function(options) {
return Promise.resolve($.ajax(options));
},
tips: function(tip, dom) {
return layer.tips(tip, dom, {
tips: [1, '#000'],
time: 4000
time: 2000
});
}
};
... ...