Skip to content

Commit

Permalink
fix: allow singular rate frequencies (#187)
Browse files Browse the repository at this point in the history
e.g. 'hour' not just 'hours'
  • Loading branch information
davemooreuws committed Jun 8, 2023
2 parents 47639ed + 2aba969 commit 85cc063
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 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);
});
});
});
});
21 changes: 15 additions & 6 deletions src/resources/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@
// limitations under the License.
import { EventMiddleware, Faas, ScheduleMiddleware } from '../faas';

type Frequency = 'days' | 'hours' | 'minutes';
const Frequencies = [
'day',
'days',
'hour',
'hours',
'minute',
'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 +72,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,7 +131,7 @@ 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.
*/
Expand All @@ -131,7 +140,7 @@ class Schedule {
...middleware: ScheduleMiddleware[]
): Promise<void> => {
// handle singular frequencies. e.g. schedule('something').every('day')
if (FREQUENCIES.indexOf(`${rate}s` as Frequency) !== -1) {
if (Frequencies.indexOf(`${rate}s` as Frequency) !== -1) {
rate = `1 ${rate}s`; // 'day' becomes '1 days'
}

Expand Down

0 comments on commit 85cc063

Please sign in to comment.