Skip to content

Commit

Permalink
Merge pull request #144 from nitrictech/enable-cron
Browse files Browse the repository at this point in the history
Add cron schedules
  • Loading branch information
tjholm authored Nov 8, 2022
2 parents 9fb227c + d62c102 commit 8362824
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/faas/v0/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
HeaderValue,
TriggerResponse,
TopicResponseContext,
ScheduleCron,
ScheduleRate,
SubscriptionWorker,
ScheduleWorker,
Expand All @@ -41,11 +42,11 @@ import {
TriggerMiddleware,
} from '.';

import { ApiWorkerOptions, RateWorkerOptions, SubscriptionWorkerOptions } from "../../resources";
import { ApiWorkerOptions, CronWorkerOptions, RateWorkerOptions, SubscriptionWorkerOptions } from "../../resources";

class FaasWorkerOptions {}

type FaasClientOptions = ApiWorkerOptions | RateWorkerOptions | FaasWorkerOptions;
type FaasClientOptions = ApiWorkerOptions | RateWorkerOptions | CronWorkerOptions | FaasWorkerOptions;

/**
*
Expand Down Expand Up @@ -214,6 +215,13 @@ export class Faas {
rate.setRate(`${this.options.rate} ${this.options.frequency}`);
scheduleWorker.setRate(rate);
initRequest.setSchedule(scheduleWorker);
} else if (this.options instanceof CronWorkerOptions) {
const scheduleWorker = new ScheduleWorker();
scheduleWorker.setKey(this.options.description);
const cron = new ScheduleCron();
cron.setCron(this.options.cron);
scheduleWorker.setCron(cron);
initRequest.setSchedule(scheduleWorker);
} else if (this.options instanceof SubscriptionWorkerOptions) {
const subscriptionWorker = new SubscriptionWorker()
subscriptionWorker.setTopic(this.options.topic);
Expand Down
23 changes: 21 additions & 2 deletions src/resources/schedule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,25 @@ describe('Schedule', () => {
jest.clearAllMocks();
});

describe("when creating a new schedule with cron expression", () => {
let error = undefined;
afterAll(() => {
jest.resetAllMocks();
});

beforeAll(async () => {
try {
await schedule("main").cron("0 10 * * *", mockFn);
} catch (err) {
error = err
}
});

it("should not return an error", () => {
expect(error).toBe(undefined);
});
});

describe("when creating a new schedule with an invalid rate", () => {
let error = undefined;
afterAll(() => {
Expand All @@ -40,7 +59,7 @@ describe('Schedule', () => {

it("should return an error", () => {
expect(error).not.toBe(undefined);
})
});
});

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

it("should throw an error", () => {
expect(error).not.toBe(undefined);
})
});
});

["day", "hour", "minute"].forEach(rate => {
Expand Down
36 changes: 36 additions & 0 deletions src/resources/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ export class RateWorkerOptions {
}
}

export class CronWorkerOptions {
public readonly description: string;
public readonly cron: string;

constructor(description: string, cron: string) {
this.description = description;
this.cron = cron;
}
}

/**
* Provides a rate based schedule
*
Expand Down Expand Up @@ -65,6 +75,26 @@ class Rate {
}
}

/**
* Provides a cron based schedule
*/
class Cron {
public readonly schedule: Schedule;
private readonly faas: Faas;

constructor(schedule: Schedule, cron: string, ...mw: EventMiddleware[]) {
this.schedule = schedule;
this.faas = new Faas(
new CronWorkerOptions(schedule['description'], cron)
);
this.faas.event(...mw);
}

private async start(): Promise<void> {
return this.faas.start();
}
}

/**
* Providers a scheduled worker.
*/
Expand Down Expand Up @@ -92,6 +122,12 @@ class Schedule {
// Start the new rate immediately
return r['start']();
}

cron = (expression: string, ...mw: EventMiddleware[]): Promise<void> => {
const r = new Cron(this, expression, ...mw);
// Start the new cron immediately
return r['start']();
}
}

/**
Expand Down

0 comments on commit 8362824

Please sign in to comment.