# @corva/node-scheduled-lambda

> Generic lambda class for constructing scheduled data apps.

Latest version **5.0.0** (published 2020-10-12) · UNLICENSED license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @corva/node-scheduled-lambda
pnpm add @corva/node-scheduled-lambda
yarn add @corva/node-scheduled-lambda
bun add @corva/node-scheduled-lambda
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 5.0.0 |
| Published | 2020-10-12 |
| First published | 2020-10-01 |
| Weekly downloads | 0 |
| License | UNLICENSED |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 516.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Corva LLC |
| Maintainers | semykin, dumavit1, jordanambra, villeti, ryandawson, bestfit |
| Keywords | lambda |

## Links

- npm: https://www.npmjs.com/package/@corva/node-scheduled-lambda
- npm.io page: https://npm.io/package/@corva/node-scheduled-lambda

## Dependencies (1)

- [@corva/node-generic-lambda](https://npm.io/package/@corva/node-generic-lambda.md) ^5.0.0

## Recent versions

- 5.0.0 (latest) — 2020-10-12
- 4.3.0 — 2020-10-05
- 4.2.0 — 2020-10-01

## README

# node-scheduled-lambda

Scheduled lambda class for constructing data apps that run on schedule.

## ToC

- [node-scheduled-lambda](#node-scheduled-lambda)
  - [ToC](#toc)
  - [Requirements](#requirements)
  - [Getting started](#getting-started)
    - [Installation](#installation)
  - [Features](#features)
  - [Workflow](#workflow)
    - [Post-process](#post-process)
    - [Examples](#examples)
      - [Scheduled event](#scheduled-event)

## 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

```plantuml
@startuml
(*) -->[{event}] "preProcess"

if "Exception?" then
  -->[true, {error}] "postProcess"
else
  -->[false, {event}] "process"
  -->[{data, error}] "postProcess"
endif
-->[End] (*)
@enduml
```

![workflow diagram](https://www.plantuml.com/plantuml/png/TOun3i8m34LtdyBA1AYz0dJfpeuwHCsFY5IIod4HKiJT2QL2B6oM_Px_xx9QqUKsqUwmvxOzNWhkYFeSsCo2iwGHEHkYuDdqZn6pXXHFXlMAIBmQAWiQBX19idefwrV4bB5ntjQvuT-03VhSYxDg6_xp9xhWwOtrqGrSsr9NTxNv2m00 'Workflow')

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:

```json5
{
  configuration: {
    updateStatus: true,
  },
}
```

```js
const lambda = new ScheduledLambda({
  apiClient,
  configuration: {
    updateStatus: true,
  },
  process: async ({ event, context }) => {
    const result = event.a + event.b;
    return result;
  },
});
```

### Examples

```js
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:

```js
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',
};
```

---
_Source: https://npm.io/package/@corva/node-scheduled-lambda · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
