Skip to content

Commit

Permalink
定时任务支持多个定时规则
Browse files Browse the repository at this point in the history
  • Loading branch information
whyour committed Sep 15, 2023
1 parent 042d7d3 commit acc7443
Show file tree
Hide file tree
Showing 17 changed files with 306 additions and 275 deletions.
2 changes: 2 additions & 0 deletions back/api/cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export default (app: Router) => {
name: Joi.string().optional(),
labels: Joi.array().optional(),
sub_id: Joi.number().optional().allow(null),
extra_schedules: Joi.array().optional().allow(null),
}),
}),
async (req: Request, res: Response, next: NextFunction) => {
Expand Down Expand Up @@ -333,6 +334,7 @@ export default (app: Router) => {
schedule: Joi.string().required(),
name: Joi.string().optional().allow(null),
sub_id: Joi.number().optional().allow(null),
extra_schedules: Joi.array().optional().allow(null),
id: Joi.number().required(),
}),
}),
Expand Down
5 changes: 4 additions & 1 deletion back/data/cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class Crontab {
last_running_time?: number;
last_execution_time?: number;
sub_id?: number;
extra_schedules?: Array<{ schedule: string }>;

constructor(options: Crontab) {
this.name = options.name;
Expand All @@ -39,6 +40,7 @@ export class Crontab {
this.last_running_time = options.last_running_time || 0;
this.last_execution_time = options.last_execution_time || 0;
this.sub_id = options.sub_id;
this.extra_schedules = options.extra_schedules;
}
}

Expand All @@ -49,7 +51,7 @@ export enum CrontabStatus {
'disabled',
}

export interface CronInstance extends Model<Crontab, Crontab>, Crontab {}
export interface CronInstance extends Model<Crontab, Crontab>, Crontab { }
export const CrontabModel = sequelize.define<CronInstance>('Crontab', {
name: {
unique: 'compositeIndex',
Expand All @@ -75,4 +77,5 @@ export const CrontabModel = sequelize.define<CronInstance>('Crontab', {
last_running_time: DataTypes.NUMBER,
last_execution_time: DataTypes.NUMBER,
sub_id: { type: DataTypes.NUMBER, allowNull: true },
extra_schedules: DataTypes.JSON
});
5 changes: 4 additions & 1 deletion back/loaders/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ export default async () => {
} catch (error) {}
try {
await sequelize.query('alter table Crontabs add column sub_id NUMBER');
} catch (error) {}
} catch (error) { }
try {
await sequelize.query('alter table Crontabs add column extra_schedules JSON');
} catch (error) { }

// 2.10-2.11 升级
const cronDbFile = path.join(config.rootPath, 'db/crontab.db');
Expand Down
3 changes: 3 additions & 0 deletions back/protos/cron.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ service Cron {
rpc delCron(DeleteCronRequest) returns (DeleteCronResponse);
}

message ISchedule { string schedule = 1; }

message ICron {
string id = 1;
string schedule = 2;
string command = 3;
repeated ISchedule extra_schedules = 4;
}

message AddCronRequest { repeated ICron crons = 1; }
Expand Down
Loading

0 comments on commit acc7443

Please sign in to comment.