@corva/node-scheduled-lambda v5.0.0
node-scheduled-lambda
Scheduled lambda class for constructing data apps that run on schedule.
ToC
Requirements
- node.js - >=12.0.0
Getting started
Installation
npm i @corva/node-scheduled-lambda
Features
- Defines scheduled workflow
Workflow
Scheduled lambda has 3 phases:
- pre-process
- process
- post-process
@startuml
(*) -->[{event}] "preProcess"
if "Exception?" then
-->[true, {error}] "postProcess"
else
-->[false, {event}] "process"
-->[{data, error}] "postProcess"
endif
-->[End] (*)
@enduml
Usually you need to define process
function to handle the data. Pre-process is used to modify incoming data in some way. Post-process is used to handle processed data or/and handle and log errors that were thrown during pre-process or process.
Also it expose .run
method that is used to launch processing.
Post-process
In scheduled lambda when process
has finished it's job and returned no error scheduler status update is called automatically.
This is just Corva API method call that flags scheduler that app is ready to accept and process next schedule.
If error is thrown in process
, then scheduler status will not be updated and current event will be rescheduled.
If you need to enable automatic schedule status updates, add following configuration
object to constructor:
{
configuration: {
updateStatus: true,
},
}
const lambda = new ScheduledLambda({
apiClient,
configuration: {
updateStatus: true,
},
process: async ({ event, context }) => {
const result = event.a + event.b;
return result;
},
});
Examples
const { ScheduledLambda } = require('@corva/node-scheduled-lambda');
// you may need api client and other libs
const { ApiClient } = require('@corva/node-api-client');
const apiClient = new ApiClient();
const lambda = new ScheduledLambda({
apiClient,
process: async ({ event, context }) => {
const result = event.a + event.b;
return result;
},
});
Scheduled event
Scheduled event could look like this:
const event = {
type: 'data_app',
collection: 'activity-groups',
cron_string: '*/5 * * * *',
environment: 'qa',
app: 1,
app_key: 'corva.activity-group',
app_version: '0.0.1',
app_connection: 12345,
source_type: 'drilling',
company: 1,
provider: 'corva',
api_url: 'https://some-url',
api_key: 'activity-group-key',
schedule: 116027101,
interval: 300,
schedule_start: 1575970800000,
schedule_end: 1575971100000,
asset_id: 1,
asset_name: 'My Asset',
asset_type: 'Well',
timezone: 'America/Chicago',
};