Skip to content

Commit

Permalink
fix: better types
Browse files Browse the repository at this point in the history
  • Loading branch information
Meemaw committed Aug 11, 2019
1 parent 2022b80 commit 13444ca
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
8 changes: 6 additions & 2 deletions README.md
Expand Up @@ -23,7 +23,9 @@ plugins:
```sh
λ → sls schedule
Serverless: Starting serverless-offline-schedule in standalone process. Press CTRL + C to stop.
Serverless: Scheduling [my-function] with [*/1 * * * *]
Serverless: Scheduling [schedule-function] cron: [*/1 * * * *] input: {"scheduler":"1-minute"}
...
Serverless: Succesfully invoked scheduled function: [my-function]
```

#### Part of serverless-offline
Expand All @@ -33,5 +35,7 @@ Serverless: Scheduling [my-function] with [*/1 * * * *]
```sh
λ → sls offline
...
Serverless: Scheduling [my-function] with [*/1 * * * *]
Serverless: Scheduling [schedule-function] cron: [*/1 * * * *] input: {"scheduler":"1-minute"}
...
Serverless: Succesfully invoked scheduled function: [my-function]
```
14 changes: 10 additions & 4 deletions src/scheduler.ts
Expand Up @@ -12,6 +12,12 @@ type SchedulerConfig = {
functionProvider: FunctionProvider;
};

type FunctionConfiguration = {
input: object;
functionName: string;
cron: string;
};

class OfflineScheduler {
private log: (message: string) => void;
private functionProvider: FunctionProvider;
Expand All @@ -32,20 +38,20 @@ class OfflineScheduler {

configurations.forEach(functionConfiguration => {
const { functionName, cron, input } = functionConfiguration;
this.log(`Scheduling [${functionName}] with [${cron}]`);
this.log(`Scheduling [${functionName}] cron: [${cron}] input: ${JSON.stringify(input)}`);

schedule.scheduleJob(cron, () => {
const func = slsInvokeFunction(functionName, input);
if (func === undefined) {
this.log(`Unable to find source for function [${functionName}]`);
return;
}
this.log(`Succesfully run scheduled function: [${functionName}]`);
this.log(`Succesfully invoked scheduled function: [${functionName}]`);
});
});
};

private getFunctionConfigurations = () => {
private getFunctionConfigurations = (): FunctionConfiguration[] => {
const functions = this.functionProvider();

const scheduleConfigurations = Object.keys(functions).map(functionName => {
Expand All @@ -57,7 +63,7 @@ class OfflineScheduler {
return {
functionName,
cron: convertExpressionToCron(event['schedule'].rate),
input: event['schedule'].input,
input: event['schedule'].input || {},
};
});
});
Expand Down
9 changes: 7 additions & 2 deletions tests/scheduler.spec.ts
Expand Up @@ -64,7 +64,9 @@ describe('OfflineScheduler', () => {
});

scheduler.scheduleEvents();
expect(log).toBeCalledWith('Scheduling [schedule-function] with [*/1 * * * *]');
expect(log).toBeCalledWith(
'Scheduling [schedule-function] cron: [*/1 * * * *] input: {"scheduler":"1-minute"}'
);
expect(scheduleJob).toBeCalledTimes(1);
});

Expand All @@ -81,7 +83,10 @@ describe('OfflineScheduler', () => {
1,
'Starting serverless-offline-schedule in standalone process. Press CTRL + C to stop.'
);
expect(log).nthCalledWith(2, 'Scheduling [schedule-function] with [*/1 * * * *]');
expect(log).nthCalledWith(
2,
'Scheduling [schedule-function] cron: [*/1 * * * *] input: {"scheduler":"1-minute"}'
);
expect(scheduleJob).toBeCalledTimes(1);
});
});
2 changes: 1 addition & 1 deletion typings/Serverless.d.ts
Expand Up @@ -35,7 +35,7 @@ declare namespace Serverless {
extraServicePath?: string;
}

type Event = { [type: string]: { rate: string; input: object } };
type Event = { [type: string]: { rate: string; input?: object } };

interface Function {
handler: string;
Expand Down

0 comments on commit 13444ca

Please sign in to comment.