Skip to content

Commit 8362824

Browse files
authored
Merge pull request #144 from nitrictech/enable-cron
Add cron schedules
2 parents 9fb227c + d62c102 commit 8362824

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

src/faas/v0/start.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
HeaderValue,
2323
TriggerResponse,
2424
TopicResponseContext,
25+
ScheduleCron,
2526
ScheduleRate,
2627
SubscriptionWorker,
2728
ScheduleWorker,
@@ -41,11 +42,11 @@ import {
4142
TriggerMiddleware,
4243
} from '.';
4344

44-
import { ApiWorkerOptions, RateWorkerOptions, SubscriptionWorkerOptions } from "../../resources";
45+
import { ApiWorkerOptions, CronWorkerOptions, RateWorkerOptions, SubscriptionWorkerOptions } from "../../resources";
4546

4647
class FaasWorkerOptions {}
4748

48-
type FaasClientOptions = ApiWorkerOptions | RateWorkerOptions | FaasWorkerOptions;
49+
type FaasClientOptions = ApiWorkerOptions | RateWorkerOptions | CronWorkerOptions | FaasWorkerOptions;
4950

5051
/**
5152
*
@@ -214,6 +215,13 @@ export class Faas {
214215
rate.setRate(`${this.options.rate} ${this.options.frequency}`);
215216
scheduleWorker.setRate(rate);
216217
initRequest.setSchedule(scheduleWorker);
218+
} else if (this.options instanceof CronWorkerOptions) {
219+
const scheduleWorker = new ScheduleWorker();
220+
scheduleWorker.setKey(this.options.description);
221+
const cron = new ScheduleCron();
222+
cron.setCron(this.options.cron);
223+
scheduleWorker.setCron(cron);
224+
initRequest.setSchedule(scheduleWorker);
217225
} else if (this.options instanceof SubscriptionWorkerOptions) {
218226
const subscriptionWorker = new SubscriptionWorker()
219227
subscriptionWorker.setTopic(this.options.topic);

src/resources/schedule.test.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,25 @@ describe('Schedule', () => {
2424
jest.clearAllMocks();
2525
});
2626

27+
describe("when creating a new schedule with cron expression", () => {
28+
let error = undefined;
29+
afterAll(() => {
30+
jest.resetAllMocks();
31+
});
32+
33+
beforeAll(async () => {
34+
try {
35+
await schedule("main").cron("0 10 * * *", mockFn);
36+
} catch (err) {
37+
error = err
38+
}
39+
});
40+
41+
it("should not return an error", () => {
42+
expect(error).toBe(undefined);
43+
});
44+
});
45+
2746
describe("when creating a new schedule with an invalid rate", () => {
2847
let error = undefined;
2948
afterAll(() => {
@@ -40,7 +59,7 @@ describe('Schedule', () => {
4059

4160
it("should return an error", () => {
4261
expect(error).not.toBe(undefined);
43-
})
62+
});
4463
});
4564

4665
describe("when creating a new schedule with an invalid frequency", () => {
@@ -59,7 +78,7 @@ describe('Schedule', () => {
5978

6079
it("should throw an error", () => {
6180
expect(error).not.toBe(undefined);
62-
})
81+
});
6382
});
6483

6584
["day", "hour", "minute"].forEach(rate => {

src/resources/schedule.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ export class RateWorkerOptions {
2929
}
3030
}
3131

32+
export class CronWorkerOptions {
33+
public readonly description: string;
34+
public readonly cron: string;
35+
36+
constructor(description: string, cron: string) {
37+
this.description = description;
38+
this.cron = cron;
39+
}
40+
}
41+
3242
/**
3343
* Provides a rate based schedule
3444
*
@@ -65,6 +75,26 @@ class Rate {
6575
}
6676
}
6777

78+
/**
79+
* Provides a cron based schedule
80+
*/
81+
class Cron {
82+
public readonly schedule: Schedule;
83+
private readonly faas: Faas;
84+
85+
constructor(schedule: Schedule, cron: string, ...mw: EventMiddleware[]) {
86+
this.schedule = schedule;
87+
this.faas = new Faas(
88+
new CronWorkerOptions(schedule['description'], cron)
89+
);
90+
this.faas.event(...mw);
91+
}
92+
93+
private async start(): Promise<void> {
94+
return this.faas.start();
95+
}
96+
}
97+
6898
/**
6999
* Providers a scheduled worker.
70100
*/
@@ -92,6 +122,12 @@ class Schedule {
92122
// Start the new rate immediately
93123
return r['start']();
94124
}
125+
126+
cron = (expression: string, ...mw: EventMiddleware[]): Promise<void> => {
127+
const r = new Cron(this, expression, ...mw);
128+
// Start the new cron immediately
129+
return r['start']();
130+
}
95131
}
96132

97133
/**

0 commit comments

Comments
 (0)