更新停止任务日志

This commit is contained in:
whyour 2021-05-10 18:04:46 +08:00
parent bb4e66715c
commit a2e82cf7c4
3 changed files with 33 additions and 20 deletions

View File

@ -22,12 +22,20 @@ export default class CronService {
return this.cronDb; return this.cronDb;
} }
private isSixCron(cron: Crontab) {
const { schedule } = cron;
if (schedule.split(' ').length === 6) {
return true;
}
return false;
}
public async create(payload: Crontab): Promise<Crontab> { public async create(payload: Crontab): Promise<Crontab> {
const tab = new Crontab(payload); const tab = new Crontab(payload);
tab.created = new Date().valueOf(); tab.created = new Date().valueOf();
tab.saved = false; tab.saved = false;
const doc = await this.insert(tab); const doc = await this.insert(tab);
await this.set_crontab(); await this.set_crontab(this.isSixCron(doc));
return doc; return doc;
} }
@ -76,7 +84,7 @@ export default class CronService {
public async remove(ids: string[]) { public async remove(ids: string[]) {
this.cronDb.remove({ _id: { $in: ids } }, { multi: true }); this.cronDb.remove({ _id: { $in: ids } }, { multi: true });
await this.set_crontab(); await this.set_crontab(true);
} }
public async crontabs(searchText?: string): Promise<Crontab[]> { public async crontabs(searchText?: string): Promise<Crontab[]> {
@ -126,14 +134,7 @@ export default class CronService {
for (let i = 0; i < docs.length; i++) { for (let i = 0; i < docs.length; i++) {
const doc = docs[i]; const doc = docs[i];
if (doc.pid) { if (doc.pid) {
exec(`kill -9 ${doc.pid}`, (err, stdout, stderr) => { exec(`kill -9 ${doc.pid}`);
let logFile = `${config.manualLogPath}${doc._id}.log`;
this.cronDb.update(
{ _id: doc._id },
{ $set: { status: CrontabStatus.idle } },
);
fs.appendFileSync(logFile, `\n\n${stderr}\n${stdout}...`);
});
} }
} }
}); });
@ -223,7 +224,7 @@ export default class CronService {
return crontab_job_string; return crontab_job_string;
} }
private async set_crontab() { private async set_crontab(needReloadSchedule: boolean = false) {
const tabs = await this.crontabs(); const tabs = await this.crontabs();
var crontab_string = ''; var crontab_string = '';
tabs.forEach((tab) => { tabs.forEach((tab) => {
@ -246,7 +247,9 @@ export default class CronService {
fs.writeFileSync(config.crontabFile, crontab_string); fs.writeFileSync(config.crontabFile, crontab_string);
execSync(`crontab ${config.crontabFile}`); execSync(`crontab ${config.crontabFile}`);
exec(`pm2 reload schedule`); if (needReloadSchedule) {
exec(`pm2 reload schedule`);
}
this.cronDb.update({}, { $set: { saved: true } }, { multi: true }); this.cronDb.update({}, { $set: { saved: true } }, { multi: true });
} }

View File

@ -74,10 +74,10 @@ update_cron_api() {
} }
del_cron_api() { del_cron_api() {
local id=$1 local ids=$1
local currentTimeStamp=$(date +%s) local currentTimeStamp=$(date +%s)
local api=$( local api=$(
curl -s "http://localhost:5600/api/crons/$id?t=$currentTimeStamp" \ curl -s "http://localhost:5600/api/crons?t=$currentTimeStamp" \
-X 'DELETE' \ -X 'DELETE' \
-H "Accept: application/json" \ -H "Accept: application/json" \
-H "Authorization: Bearer $token" \ -H "Authorization: Bearer $token" \
@ -85,14 +85,16 @@ del_cron_api() {
-H "Content-Type: application/json;charset=UTF-8" \ -H "Content-Type: application/json;charset=UTF-8" \
-H "Origin: http://localhost:5700" \ -H "Origin: http://localhost:5700" \
-H "Referer: http://localhost:5700/crontab" \ -H "Referer: http://localhost:5700/crontab" \
-H "Accept-Language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7" -H "Accept-Language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7" \
--data-raw "[$ids]" \
--compressed
) )
code=$(echo $api | jq -r .code) code=$(echo $api | jq -r .code)
message=$(echo $api | jq -r .message) message=$(echo $api | jq -r .message)
if [[ $code == 200 ]]; then if [[ $code == 200 ]]; then
echo -e "$name -> 删除成功" echo -e "删除成功"
else else
echo -e "$name -> 删除失败(${message})" echo -e "删除失败(${message})"
fi fi
} }

View File

@ -109,14 +109,22 @@ del_cron() {
local list_drop=$1 local list_drop=$1
local author=$2 local author=$2
local detail="" local detail=""
local ids=""
echo -e "开始尝试自动删除失效的定时任务...\n" echo -e "开始尝试自动删除失效的定时任务...\n"
for cron in $(cat $list_drop); do for cron in $(cat $list_drop); do
local id=$(cat $list_crontab_user | grep -E "$cmd_task $cron$" | perl -pe "s|.*ID=(.*) $cmd_task $cron$|\1|") local id=$(cat $list_crontab_user | grep -E "$cmd_task $cron$" | perl -pe "s|.*ID=(.*) $cmd_task $cron$|\1|")
result=$(del_cron_api "$id") if [[ $ids ]]; then
echo -e "$result" ids="$ids,\"$id\""
else
ids="\"$id\""
fi
cron_name=$(grep "new Env" "$dir_scripts/${cron}" | awk -F "'|\"" '{print $2}' | head -1)
[[ -z $cron_name ]] && cron_name="$cron"
detail="${detail}\n${cron_name}"
rm -f "$dir_scripts/${cron}" rm -f "$dir_scripts/${cron}"
detail="${detail}\n${result}"
done done
result=$(del_cron_api "$ids")
detail="${result}\n${detail}"
notify "删除失效任务通知" "$detail" notify "删除失效任务通知" "$detail"
} }