@corva/node-task-lambda v5.0.1
node-task-lambda
Task lambda class for constructing task data apps.
ToC
Requirements
- node.js - >=12.0.0
Getting started
Installation
npm i @corva/node-task-lambda
Features
- Defines task workflow
- Get task info
- Updates task status
Workflow
Task lambda has 3 phases:
- pre-process (gets task info)
- process
- post-process (updates task status)
@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.
Pre-process
For task lambdas event
that's passed to pre-process
handler usually looks like:
{
"task_id": "dc5c16e4-c0e3-43fc-85b2-83b08b9da7d0",
"version": 2
}
And pre-process
handler automatically will fetch task data from Corva API.
As a result task event will be passed to process
handler and it will have following structure:
{
"app_key": "tasks.my-task-app",
"provider": "corva",
"asset_id": 1,
"properties": {
"foo": "bar",
"bar": "baz"
},
"payload": {}
}
Post-process
Task app post-process
handler will mark task as succeeded
or failed
depending on what will be passed to it.
If error
parameter will not be falsy, then task will be considered as failed
, otherwise it will be succeeded
.
If process
handler returns data object, it will be saved to task as well and will be stored in payload
field in task.
NOTE: Avoid returning and storing too much data in it, for big volumes use saveData
from API instead.
Examples
const { TaskLambda } = require('@corva/node-task-lambda');
// you may need api client and other libs
const { ApiClient } = require('@corva/node-api-client');
const apiClient = new ApiClient();
const lambda = new TaskLambda({
apiClient,
process: async ({ event, context }) => {
const result = event.a + event.b;
// Don't return too large results
// Returned result will be saved in task object
return result;
},
});