Skip to content
This repository has been archived by the owner on Sep 7, 2022. It is now read-only.

Commit

Permalink
Implement context.getRemainingTimeInMillis (#13)
Browse files Browse the repository at this point in the history
* implement context.getRemainingTimeInMillis()

* added getRemainingTimeInMillis tests

* - use the function then provider timeout before we fall back to default
- constants to upper case

* getRemainingTimeInMillis tests

* Use node 4.8 for CI
  • Loading branch information
maxholman authored and ajmath committed Jul 16, 2018
1 parent 9186272 commit 7ac3c2a
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 3 deletions.
2 changes: 2 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
machine:
node:
version: 4.8
environment:
PATH: "${PATH}:${HOME}/${CIRCLE_PROJECT_REPONAME}/node_modules/.bin"

Expand Down
20 changes: 17 additions & 3 deletions lib/scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const fs = require("fs");
const schedule = require("node-schedule");
const utils = require("./utils");

const DEFAULT_TIMEOUT = 6;
const MS_PER_SEC = 1000;

class EventData {
constructor(name, cron, enabled, input) {
this.name = name;
Expand Down Expand Up @@ -35,6 +38,7 @@ class Scheduler {
this.location = offlinePlugin.options.location;
}
this.funcConfigs = this._getFuncConfigs();

for (const i in this.funcConfigs) {
const fConfig = this.funcConfigs[i];
for (const j in fConfig.events) {
Expand All @@ -52,7 +56,7 @@ class Scheduler {
this.serverless.cli.log(`scheduler: running scheduled job: ${fConfig.id}`);
func(
this._getEvent(eventData.input),
this._getContext(fConfig.id),
this._getContext(fConfig),
() => {}
);
});
Expand Down Expand Up @@ -104,7 +108,14 @@ class Scheduler {
};
}

_getContext(functionName) {
_getContext(fConfig) {

const functionName = fConfig.id;

const timeout = fConfig.timeout || this.serverless.service.provider.timeout || DEFAULT_TIMEOUT;

const endTime = Math.max(0, Date.now() + timeout * MS_PER_SEC);

return {
awsRequestId: utils.guid(),
invokeid: utils.guid(),
Expand All @@ -115,7 +126,8 @@ class Scheduler {
functionName,
memoryLimitInMB: "1024",
callbackWaitsForEmptyEventLoop: true,
invokedFunctionArn: `arn:aws:lambda:serverless-offline:123456789012:function:${functionName}`
invokedFunctionArn: `arn:aws:lambda:serverless-offline:123456789012:function:${functionName}`,
getRemainingTimeInMillis: () => endTime - Date.now()
};
}

Expand Down Expand Up @@ -171,6 +183,7 @@ class Scheduler {
_getFuncConfigs() {
const funcConfs = [];
const inputfuncConfs = this.serverless.service.functions;

for (const funcName in inputfuncConfs) {
const funcConf = inputfuncConfs[funcName];
const scheduleEvents = funcConf.events
Expand All @@ -181,6 +194,7 @@ class Scheduler {
funcConfs.push({
id: funcName,
events: scheduleEvents,
timeout: funcConf.timeout,
moduleName: funcConf.handler.split(".")[0]
});
}
Expand Down
70 changes: 70 additions & 0 deletions tests/scheduler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const expect = chai.expect;

const Scheduler = require("../lib/scheduler");

const MS_PER_SEC = 1000;

describe("validate", () => {
let module;
let serverless;
Expand Down Expand Up @@ -162,4 +164,72 @@ describe("validate", () => {
expect(event).to.have.property("input");
expect(event.input).to.have.property("key1").that.equals("value1");
});

it("should use the *function* timeout for getRemainingTimeInMillis", () => {

const timeout = 45; // secs
const maxDuration = 2; // msecs

module.serverless.service.functions = {
scheduled1: {
handler: "handler.test1",
timeout,
events: [{
schedule: {
rate: "cron(1/* * * * *)"
}
}]
}
};

const funcs = module._getFuncConfigs();
const context = module._getContext(funcs[0]);
expect(context.getRemainingTimeInMillis()).to.be.at.most(timeout * MS_PER_SEC);
expect(context.getRemainingTimeInMillis()).to.be.at.least(timeout * MS_PER_SEC - maxDuration);
});

it("should use the *provider* timeout for getRemainingTimeInMillis", () => {

const timeout = 35; // secs
const maxDuration = 2; // msecs

module.serverless.service.provider.timeout = timeout;
module.serverless.service.functions = {
scheduled1: {
handler: "handler.test1",
events: [{
schedule: {
rate: "cron(1/* * * * *)"
}
}]
}
};

const funcs = module._getFuncConfigs();
const context = module._getContext(funcs[0]);
expect(context.getRemainingTimeInMillis()).to.be.at.most(timeout * MS_PER_SEC);
expect(context.getRemainingTimeInMillis()).to.be.at.least(timeout * MS_PER_SEC - maxDuration);
});

it("should use the *default* timeout for getRemainingTimeInMillis", () => {

const timeout = 6; // secs
const maxDuration = 2; // msecs

module.serverless.service.functions = {
scheduled1: {
handler: "handler.test1",
events: [{
schedule: {
rate: "cron(1/* * * * *)"
}
}]
}
};

const funcs = module._getFuncConfigs();
const context = module._getContext(funcs[0]);
expect(context.getRemainingTimeInMillis()).to.be.at.most(timeout * MS_PER_SEC);
expect(context.getRemainingTimeInMillis()).to.be.at.least(timeout * MS_PER_SEC - maxDuration);
});
});

0 comments on commit 7ac3c2a

Please sign in to comment.