Skip to content

Commit

Permalink
added rule engine, switched to pnpm cause npm broken with monorepo
Browse files Browse the repository at this point in the history
  • Loading branch information
pruales committed Sep 12, 2022
1 parent cff2819 commit 7c94e3a
Show file tree
Hide file tree
Showing 14 changed files with 5,012 additions and 5,746 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ coverage
out/
build

# serverless
.build
.esbuild
.serverless

# misc
.DS_Store
*.pem
Expand All @@ -21,6 +26,7 @@ build
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*

# local env files
.env.local
Expand Down
121 changes: 121 additions & 0 deletions apps/serverless-rule-engine/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<!--
title: 'AWS Node Scheduled Cron example in NodeJS'
description: 'This is an example of creating a function that runs as a cron job using the serverless ''schedule'' event.'
layout: Doc
framework: v3
platform: AWS
language: nodeJS
priority: 1
authorLink: 'https://github.com/0dj0bz'
authorName: 'Rob Abbott'
authorAvatar: 'https://avatars3.githubusercontent.com/u/5679763?v=4&s=140'
-->

# Serverless Framework Node Scheduled Cron on AWS

This template demonstrates how to develop and deploy a simple cron-like service running on AWS Lambda using the traditional Serverless Framework.

## Schedule event type

This examples defines two functions, `cron` and `secondCron`, both of which are triggered by an event of `schedule` type, which is used for configuring functions to be executed at specific time or in specific intervals. For detailed information about `schedule` event, please refer to corresponding section of Serverless [docs](https://serverless.com/framework/docs/providers/aws/events/schedule/).

When defining `schedule` events, we need to use `rate` or `cron` expression syntax.

### Rate expressions syntax

```pseudo
rate(value unit)
```

`value` - A positive number

`unit` - The unit of time. ( minute | minutes | hour | hours | day | days )

In below example, we use `rate` syntax to define `schedule` event that will trigger our `rateHandler` function every minute

```yml
functions:
rateHandler:
handler: handler.run
events:
- schedule: rate(1 minute)
```
Detailed information about rate expressions is available in official [AWS docs](https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html#RateExpressions).
### Cron expressions syntax
```pseudo
cron(Minutes Hours Day-of-month Month Day-of-week Year)
```

All fields are required and time zone is UTC only.

| Field | Values | Wildcards |
| ------------- |:--------------:|:-------------:|
| Minutes | 0-59 | , - * / |
| Hours | 0-23 | , - * / |
| Day-of-month | 1-31 | , - * ? / L W |
| Month | 1-12 or JAN-DEC| , - * / |
| Day-of-week | 1-7 or SUN-SAT | , - * ? / L # |
| Year | 192199 | , - * / |

In below example, we use `cron` syntax to define `schedule` event that will trigger our `cronHandler` function every second minute every Monday through Friday

```yml
functions:
cronHandler:
handler: handler.run
events:
- schedule: cron(0/2 * ? * MON-FRI *)
```
Detailed information about cron expressions in available in official [AWS docs](https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html#CronExpressions).
## Usage
### Deployment
This example is made to work with the Serverless Framework dashboard, which includes advanced features such as CI/CD, monitoring, metrics, etc.
In order to deploy with dashboard, you need to first login with:
```
serverless login
```

and then perform deployment with:

```
serverless deploy
```

After running deploy, you should see output similar to:

```bash
Deploying aws-node-scheduled-cron-project to stage dev (us-east-1)

✔ Service deployed to stack aws-node-scheduled-cron-project-dev (205s)

functions:
rateHandler: aws-node-scheduled-cron-project-dev-rateHandler (2.9 kB)
cronHandler: aws-node-scheduled-cron-project-dev-cronHandler (2.9 kB)
```

There is no additional step required. Your defined schedules becomes active right away after deployment.

### Local invocation

In order to test out your functions locally, you can invoke them with the following command:

```
serverless invoke local --function rateHandler
```

After invocation, you should see output similar to:

```bash
Your cron function "aws-node-scheduled-cron-dev-rateHandler" ran at Fri Mar 05 2021 15:14:39 GMT+0100 (Central European Standard Time)
```
4 changes: 4 additions & 0 deletions apps/serverless-rule-engine/eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
root: true,
extends: ["custom"],
};
20 changes: 20 additions & 0 deletions apps/serverless-rule-engine/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "serverless-rule-engine",
"version": "1.0.0",
"description": "Example of creating a function that runs as a cron job using the serverless `schedule` event",
"author": "",
"license": "MIT",
"scripts": {
"deploy": "serverless deploy",
"invoke": "serverless invoke local -f rule_engine"
},
"devDependencies": {
"esbuild": "^0.15.7",
"eslint-config-custom": "workspace:*",
"serverless-esbuild": "^1.32.8",
"serverless-offline": "^10.0.2",
"serverless-plugin-typescript": "^2.1.2",
"tsconfig": "workspace:*",
"typescript": "^4.8.3"
}
}
32 changes: 32 additions & 0 deletions apps/serverless-rule-engine/serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
service: serverless-rule-engine

frameworkVersion: "3"

provider:
name: aws
runtime: nodejs16.x

plugins:
- serverless-plugin-typescript
- serverless-esbuild
- serverless-offline

package:
individually: true

custom:
esbuild:
bundle: true
minify: false
sourcemap: true
target: node16
platform: node
concurrency: 10
exclude: "[*]"

functions:
rule_engine:
handler: src/rule-engine.rule_engine
events:
# Invoke Lambda function every minute
- schedule: cron(*/5 * * * * *)
8 changes: 8 additions & 0 deletions apps/serverless-rule-engine/src/rule-engine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports.rule_engine = async () => {
console.log("rule_engine");

return {
body: "OK",
statusCode: 200,
};
};
15 changes: 15 additions & 0 deletions apps/serverless-rule-engine/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "tsconfig/base.json",
"compilerOptions": {
"preserveConstEnums": true,
"strictNullChecks": true,
"sourceMap": true,
"target": "es5",
"moduleResolution": "node",
"lib": ["es2015"],
"rootDir": "./",
"noImplicitAny": true,
"esModuleInterop": true,
"isolatedModules": false
}
}
4 changes: 2 additions & 2 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"@types/react-dom": "18.0.6",
"eslint": "8.23.1",
"typescript": "4.8.3",
"tsconfig": "*",
"eslint-config-custom": "*"
"tsconfig": "workspace:*",
"eslint-config-custom": "workspace:*"
}
}
Loading

0 comments on commit 7c94e3a

Please sign in to comment.