|
|
|
/**
|
|
|
|
* 对爬虫任务列表目录进行监控
|
|
|
|
* 发现新任务
|
|
|
|
* 删除已完成的任务文件
|
|
|
|
* 内存中保存所有任务,及其状态
|
|
|
|
* 返回当前任务状态
|
|
|
|
*/
|
|
|
|
import common from './common.mjs';
|
|
|
|
import fs from 'node:fs';
|
|
|
|
import path from 'node:path';
|
|
|
|
import cron from 'node-cron';
|
|
|
|
|
|
|
|
class TaskMoniter {
|
|
|
|
|
|
|
|
constructor(task_list_dir) {
|
|
|
|
this.check_time_gap = 10; //检测间隔时间,单位:秒
|
|
|
|
this.checking = false;
|
|
|
|
|
|
|
|
this.task_dir = task_list_dir; //监控目录:任务列表保存目录
|
|
|
|
this.tasks = {}; //内存中的任务列表
|
|
|
|
this.taskStatus = { //当前任务状态
|
|
|
|
total: 0, //总任务数
|
|
|
|
waiting: 0, //等待执行的任务数
|
|
|
|
running: 0, //正在执行的任务数
|
|
|
|
done: 0, //已完成的任务数
|
|
|
|
failed: 0 //执行失败的任务数
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async getStatus() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
async getNewTask() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
async setTaskDone(task) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
async setTaskFailed(task) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
async checkTasks() {
|
|
|
|
if (this.checking == true) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
this.checking = true;
|
|
|
|
|
|
|
|
//do something
|
|
|
|
console.log('[%s] TaskMoniter auto check...', common.getTimeString());
|
|
|
|
|
|
|
|
|
|
|
|
this.checking = false;
|
|
|
|
}catch(error) {
|
|
|
|
this.checking = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
run() { //开始监控任务目录,把所有任务缓存到内存
|
|
|
|
console.log('[%s] TaskMoniter started.', common.getTimeString());
|
|
|
|
|
|
|
|
//auto run
|
|
|
|
const _self = this;
|
|
|
|
const task_check_time = this.check_time_gap;
|
|
|
|
const task_auto_run = cron.schedule(`*/${task_check_time} * * * * *`, () => {
|
|
|
|
_self.checkTasks();
|
|
|
|
}, {
|
|
|
|
scheduled: false
|
|
|
|
});
|
|
|
|
|
|
|
|
task_auto_run.start();
|
|
|
|
console.log('[%s] TaskMoniter auto check started.', common.getTimeString());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default TaskMoniter;
|