修复手动执行任务日志

This commit is contained in:
hanhh 2021-06-24 23:46:02 +08:00
parent e11d0c25f3
commit ce3543779e
4 changed files with 14 additions and 52 deletions

View File

@ -19,7 +19,6 @@ const authError = '错误的用户名密码,请重试';
const loginFaild = '请先登录!';
const configString = 'config sample crontab shareCode diy';
const dbPath = path.join(rootPath, 'db/');
const manualLogPath = path.join(rootPath, 'manual_log/');
const cronDbFile = path.join(rootPath, 'db/crontab.db');
const envDbFile = path.join(rootPath, 'db/env.db');
const configFound = dotenv.config({ path: confFile });
@ -56,7 +55,6 @@ export default {
dbPath,
cronDbFile,
envDbFile,
manualLogPath,
configPath,
scriptPath,
blackFileList: ['auth.json', 'config.sh.sample', 'cookie.sh', 'crontab.list'],

View File

@ -187,12 +187,6 @@ export default class CronService {
this.logger.silly('ID: ' + _id);
this.logger.silly('Original command: ' + command);
let logFile = `${config.manualLogPath}${_id}.log`;
fs.writeFileSync(
logFile,
`开始执行... ${new Date().toLocaleString()}\n\n`,
);
let cmdStr = command;
if (!cmdStr.includes('task ') && !cmdStr.includes('ql ')) {
cmdStr = `task ${cmdStr}`;
@ -200,50 +194,17 @@ export default class CronService {
if (cmdStr.endsWith('.js')) {
cmdStr = `${cmdStr} now`;
}
const cmd = spawn(cmdStr, { shell: true });
const cp = exec(cmdStr, (err, stdout, stderr) => {
this.cronDb.update(
{ _id },
{ $set: { status: CrontabStatus.idle }, $unset: { pid: true } },
);
resolve();
});
this.cronDb.update(
{ _id },
{ $set: { status: CrontabStatus.running, pid: cmd.pid } },
{ $set: { status: CrontabStatus.running, pid: cp.pid } },
);
cmd.stdout.on('data', (data) => {
this.logger.silly(`stdout: ${data}`);
fs.appendFileSync(logFile, data);
});
cmd.stderr.on('data', (data) => {
this.logger.silly(`stderr: ${data}`);
fs.appendFileSync(logFile, data);
});
cmd.on('close', (code) => {
this.logger.silly(`child process exited with code ${code}`);
this.cronDb.update(
{ _id },
{ $set: { status: CrontabStatus.idle }, $unset: { pid: true } },
);
});
cmd.on('error', (err) => {
this.logger.info(err);
fs.appendFileSync(logFile, err.stack);
});
cmd.on('exit', (code: number, signal: any) => {
this.logger.silly(`cmd exit ${code}`);
this.cronDb.update(
{ _id },
{ $set: { status: CrontabStatus.idle }, $unset: { pid: true } },
);
fs.appendFileSync(logFile, `\n执行结束...`);
resolve();
});
process.on('SIGINT', function () {
fs.appendFileSync(logFile, `\n执行结束...`);
resolve();
});
});
}

View File

@ -10,7 +10,6 @@ dir_repo=$dir_root/repo
dir_raw=$dir_root/raw
dir_log=$dir_root/log
dir_db=$dir_root/db
dir_manual_log=$dir_root/manual_log
dir_list_tmp=$dir_log/.tmp
dir_code=$dir_log/code
dir_update_log=$dir_log/update
@ -146,7 +145,6 @@ fix_config() {
make_dir $dir_config
make_dir $dir_log
make_dir $dir_db
make_dir $dir_manual_log
make_dir $dir_scripts
make_dir $dir_list_tmp
make_dir $dir_repo

View File

@ -89,10 +89,15 @@ run_normal() {
make_dir "$log_dir"
local id=$(cat $list_crontab_user | grep -E "$cmd_task $p1$" | perl -pe "s|.*ID=(.*) $cmd_task $p1$|\1|" | xargs | sed 's/ /","/g')
local begin_time=$(date '+%Y-%m-%d %H:%M:%S')
echo -e "开始执行... $begin_time\n" >> $log_path
update_cron_status "\"$id\"" "0"
timeout $command_timeout_time $which_program $p1 2>&1 | tee $log_path
timeout $command_timeout_time $which_program $p1 2>&1 | tee -a $log_path
. $file_task_after
update_cron_status "\"$id\"" "1"
local end_time=$(date '+%Y-%m-%d %H:%M:%S')
local diff_time=$(($(date +%s -d "$end_time") - $(date +%s -d "$begin_time")))
echo -e "\n执行结束... $end_time 耗时 $diff_time" >> $log_path
}
## 并发执行时,设定的 RandomDelay 不会生效,即所有任务立即执行