Skip to content

Commit

Permalink
修复定时任务 real_time 参数
Browse files Browse the repository at this point in the history
  • Loading branch information
whyour committed Jun 1, 2024
1 parent 70f2bef commit 3f86374
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
11 changes: 8 additions & 3 deletions back/services/cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,10 @@ export default class CronService {
const logPath = `${uniqPath}/${logTime}.log`;
const absolutePath = path.resolve(config.logPath, `${logPath}`);
const cp = spawn(
`real_log_path=${logPath} real_time=true no_delay=true ${this.makeCommand(cron)}`,
`real_log_path=${logPath} no_delay=true ${this.makeCommand(
cron,
true
)}`,
{ shell: '/bin/bash' },
);

Expand Down Expand Up @@ -526,12 +529,14 @@ export default class CronService {
}
}

private makeCommand(tab: Crontab) {
private makeCommand(tab: Crontab, realTime?: boolean) {
let command = tab.command.trim();
if (!command.startsWith(TASK_PREFIX) && !command.startsWith(QL_PREFIX)) {
command = `${TASK_PREFIX}${tab.command}`;
}
let commandVariable = `no_tee=true ID=${tab.id} `;
let commandVariable = `real_time=${Boolean(realTime)} no_tee=true ID=${
tab.id
} `;
if (tab.task_before) {
commandVariable += `task_before='${tab.task_before
.replace(/'/g, "'\\''")
Expand Down
2 changes: 1 addition & 1 deletion back/services/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import taskLimit from '../shared/pLimit';
import { spawn } from 'cross-spawn';

export interface ScheduleTaskType {
id: number;
id?: number;
command: string;
name?: string;
schedule?: string;
Expand Down
28 changes: 17 additions & 11 deletions back/services/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,28 +75,31 @@ export default class SubscriptionService {
}

public async handleTask(
doc: Subscription,
_doc: Subscription,
needCreate = true,
runImmediately = false,
) {
const { url } = formatUrl(doc);
const { url } = formatUrl(_doc);

doc.command = formatCommand(doc, url as string);
const doc = {
..._doc,
command: formatCommand(_doc, url as string),
} as Omit<Subscription, 'command'> & { command: string };

if (doc.schedule_type === 'crontab') {
this.scheduleService.cancelCronTask(doc as any);
this.scheduleService.cancelCronTask(doc);
needCreate &&
(await this.scheduleService.createCronTask(
doc as any,
doc,
this.taskCallbacks(doc),
runImmediately,
));
} else {
this.scheduleService.cancelIntervalTask(doc as any);
const { type, value } = doc.interval_schedule as any;
this.scheduleService.cancelIntervalTask(doc);
const { type, value } = doc.interval_schedule!;
needCreate &&
(await this.scheduleService.createIntervalTask(
doc as any,
doc,
{ [type]: value } as SimpleIntervalSchedule,
runImmediately,
this.taskCallbacks(doc),
Expand Down Expand Up @@ -256,7 +259,7 @@ export default class SubscriptionService {
);
}

public async remove(ids: number[], query: any) {
public async remove(ids: number[], query: { force?: boolean }) {
const docs = await SubscriptionModel.findAll({ where: { id: ids } });
for (const doc of docs) {
await this.handleTask(doc, false);
Expand All @@ -279,8 +282,11 @@ export default class SubscriptionService {
public async getDb(
query: FindOptions<Subscription>['where'],
): Promise<Subscription> {
const doc: any = await SubscriptionModel.findOne({ where: { ...query } });
return doc && (doc.get({ plain: true }) as Subscription);
const doc = await SubscriptionModel.findOne({ where: { ...query } });
if (!doc) {
throw new Error(`${JSON.stringify(query)} not found`);
}
return doc.get({ plain: true });
}

public async run(ids: number[]) {
Expand Down

0 comments on commit 3f86374

Please sign in to comment.