Skip to content

Commit

Permalink
Release (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjholm committed Jun 8, 2023
2 parents 28d29b0 + 00a2af9 commit 87633e5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
32 changes: 28 additions & 4 deletions src/resources/schedule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import * as faas from '../faas/index';
import { schedule, RateWorkerOptions } from '.';

import { schedule, RateWorkerOptions, Frequency } from '.';
jest.mock('../faas/index');

describe('Schedule', () => {
Expand Down Expand Up @@ -83,7 +82,7 @@ describe('Schedule', () => {
});
});

['day', 'hour', 'minute'].forEach((rate) => {
['day', 'hour', 'minute'].forEach((rate: Frequency) => {
describe(`when create a new schedule with rate ${rate}`, () => {
afterAll(() => {
jest.resetAllMocks();
Expand All @@ -101,7 +100,7 @@ describe('Schedule', () => {
const expectedOpts = new RateWorkerOptions(
'main',
1,
`${rate}s` as any
`${rate}s` as Frequency
);
expect(faas.Faas).toBeCalledWith(expectedOpts);
});
Expand All @@ -111,4 +110,29 @@ describe('Schedule', () => {
});
});
});

['days', 'hours', 'minutes'].forEach((rate: Frequency) => {
describe(`when create a new schedule with rate ${rate}`, () => {
afterAll(() => {
jest.resetAllMocks();
});

beforeAll(async () => {
await schedule('main').every(`7 ${rate}`, mockFn);
});

it('should create a new FaasClient', () => {
expect(faas.Faas).toBeCalledTimes(1);
});

it('should provide Faas with ApiWorkerOptions', () => {
const expectedOpts = new RateWorkerOptions('main', 7, rate);
expect(faas.Faas).toBeCalledWith(expectedOpts);
});

it('should call FaasClient::start()', () => {
expect(startSpy).toBeCalledTimes(1);
});
});
});
});
26 changes: 18 additions & 8 deletions src/resources/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
// limitations under the License.
import { EventMiddleware, Faas, ScheduleMiddleware } from '../faas';

type Frequency = 'days' | 'hours' | 'minutes';
const Frequencies = ['days', 'hours', 'minutes'] as const;

const FREQUENCIES: Frequency[] = ['days', 'hours', 'minutes'];
export type Frequency = (typeof Frequencies)[number];

export class RateWorkerOptions {
public readonly description: string;
Expand Down Expand Up @@ -65,9 +65,11 @@ class Rate {
);
}

if (!FREQUENCIES.includes(normalizedFrequency)) {
if (!Frequencies.includes(normalizedFrequency)) {
throw new Error(
`invalid rate expression, frequency must be one of ${FREQUENCIES}, received ${frequency}`
`invalid rate expression, frequency must one of [${Frequencies.join(
', '
)}] received ${frequency}`
);
}

Expand Down Expand Up @@ -122,20 +124,28 @@ class Schedule {
/**
* Run this schedule on the provided frequency.
*
* @param rate to run the schedule, e.g. '7 days'. All rates accept a number and a frequency. Valid frequencies are 'days', 'hours' or 'minutes'.
* @param rate to run the schedule, e.g. '7 days'. All rates accept a number and a frequency. Valid frequencies are 'day/days', 'hour/hours' or 'minute/minutes'.
* @param middleware the handler/middleware to run on a schedule
* @returns A promise that resolves when the schedule worker stops running.
*/
every = (
rate: string,
...middleware: ScheduleMiddleware[]
): Promise<void> => {
// handle singular frequencies. e.g. schedule('something').every('day')
if (FREQUENCIES.indexOf(`${rate}s` as Frequency) !== -1) {
// handle singular frequencies without a value, e.g. schedule('something').every('day')
if (Frequencies.indexOf(`${rate}s` as Frequency) !== -1) {
rate = `1 ${rate}s`; // 'day' becomes '1 days'
}

const r = new Rate(this, rate, ...middleware);
// handle singular frequencies with a value, e.g. schedule('something').every('1 day')
const rateParts = rate.split(' ');
const value = rateParts[0];
let unit = rateParts[1];
if (Frequencies.indexOf(`${unit}s` as Frequency) !== -1) {
unit = `${unit}s`; // 'day' becomes 'days'
}

const r = new Rate(this, `${value} ${unit}`, ...middleware);
// Start the new rate immediately
return r['start']();
};
Expand Down

0 comments on commit 87633e5

Please sign in to comment.