mirror of
https://github.com/whyour/qinglong.git
synced 2025-07-27 14:28:47 +08:00
更新停止任务日志
This commit is contained in:
parent
bb4e66715c
commit
a2e82cf7c4
|
@ -22,12 +22,20 @@ export default class CronService {
|
|||
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> {
|
||||
const tab = new Crontab(payload);
|
||||
tab.created = new Date().valueOf();
|
||||
tab.saved = false;
|
||||
const doc = await this.insert(tab);
|
||||
await this.set_crontab();
|
||||
await this.set_crontab(this.isSixCron(doc));
|
||||
return doc;
|
||||
}
|
||||
|
||||
|
@ -76,7 +84,7 @@ export default class CronService {
|
|||
|
||||
public async remove(ids: string[]) {
|
||||
this.cronDb.remove({ _id: { $in: ids } }, { multi: true });
|
||||
await this.set_crontab();
|
||||
await this.set_crontab(true);
|
||||
}
|
||||
|
||||
public async crontabs(searchText?: string): Promise<Crontab[]> {
|
||||
|
@ -126,14 +134,7 @@ export default class CronService {
|
|||
for (let i = 0; i < docs.length; i++) {
|
||||
const doc = docs[i];
|
||||
if (doc.pid) {
|
||||
exec(`kill -9 ${doc.pid}`, (err, stdout, stderr) => {
|
||||
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}...`);
|
||||
});
|
||||
exec(`kill -9 ${doc.pid}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -223,7 +224,7 @@ export default class CronService {
|
|||
return crontab_job_string;
|
||||
}
|
||||
|
||||
private async set_crontab() {
|
||||
private async set_crontab(needReloadSchedule: boolean = false) {
|
||||
const tabs = await this.crontabs();
|
||||
var crontab_string = '';
|
||||
tabs.forEach((tab) => {
|
||||
|
@ -246,7 +247,9 @@ export default class CronService {
|
|||
fs.writeFileSync(config.crontabFile, crontab_string);
|
||||
|
||||
execSync(`crontab ${config.crontabFile}`);
|
||||
exec(`pm2 reload schedule`);
|
||||
if (needReloadSchedule) {
|
||||
exec(`pm2 reload schedule`);
|
||||
}
|
||||
this.cronDb.update({}, { $set: { saved: true } }, { multi: true });
|
||||
}
|
||||
|
||||
|
|
12
shell/api.sh
12
shell/api.sh
|
@ -74,10 +74,10 @@ update_cron_api() {
|
|||
}
|
||||
|
||||
del_cron_api() {
|
||||
local id=$1
|
||||
local ids=$1
|
||||
local currentTimeStamp=$(date +%s)
|
||||
local api=$(
|
||||
curl -s "http://localhost:5600/api/crons/$id?t=$currentTimeStamp" \
|
||||
curl -s "http://localhost:5600/api/crons?t=$currentTimeStamp" \
|
||||
-X 'DELETE' \
|
||||
-H "Accept: application/json" \
|
||||
-H "Authorization: Bearer $token" \
|
||||
|
@ -85,14 +85,16 @@ del_cron_api() {
|
|||
-H "Content-Type: application/json;charset=UTF-8" \
|
||||
-H "Origin: http://localhost:5700" \
|
||||
-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)
|
||||
message=$(echo $api | jq -r .message)
|
||||
if [[ $code == 200 ]]; then
|
||||
echo -e "$name -> 删除成功"
|
||||
echo -e "删除成功"
|
||||
else
|
||||
echo -e "$name -> 删除失败(${message})"
|
||||
echo -e "删除失败(${message})"
|
||||
fi
|
||||
}
|
||||
|
||||
|
|
|
@ -109,14 +109,22 @@ del_cron() {
|
|||
local list_drop=$1
|
||||
local author=$2
|
||||
local detail=""
|
||||
local ids=""
|
||||
echo -e "开始尝试自动删除失效的定时任务...\n"
|
||||
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|")
|
||||
result=$(del_cron_api "$id")
|
||||
echo -e "$result"
|
||||
if [[ $ids ]]; then
|
||||
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}"
|
||||
detail="${detail}\n${result}"
|
||||
done
|
||||
result=$(del_cron_api "$ids")
|
||||
detail="${result}\n${detail}"
|
||||
notify "删除失效任务通知" "$detail"
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user