Giter Club home page Giter Club logo

bandai-cron's Introduction

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.

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

Rate expressions syntax

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

functions:
  rateHandler:
    handler: handler.run
    events:
      - schedule: rate(1 minute)

Detailed information about rate expressions is available in official AWS docs.

Cron expressions syntax

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

functions:
  cronHandler:
    handler: handler.run
    events:
      - schedule: cron(0/2 * ? * MON-FRI *)

Detailed information about cron expressions in available in official AWS docs.

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:

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:

Your cron function "aws-node-scheduled-cron-dev-rateHandler" ran at Fri Mar 05 2021 15:14:39 GMT+0100 (Central European Standard Time)

bandai-cron's People

Contributors

0xknon avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.