npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

aws-scheduler

v1.0.5

Published

This package is responsible for adding up cron jobs to the queue when the time is due.

Downloads

7

Readme

AWS Job Scheduler

Project Architecture

This package is responsible for creating new scheduled jobs, and adding them up to the queue when their time is due.

When cron service is started, it connects to DynamoDB table, and gets previously created jobs. After jobs are fetched, cron service schedules the jobs accordingly to their time format (cron / specific ISO date). Cron service is smart enough to work with recurrent jobs (such as: clear cache every night). Recurrent jobs implemented using ES6 iterators.

When job time is due, cron service adds it the job to the SQS queue accordingly to the job name. If queue does not exists, it's being created. On the other side, job handler must be created with the same job name.

Prerequisites

What things you need to install the software and how to install them

  • Node.js
  • AWS Account with access to the SQS and DynamoDB

Installation

A step by step series of examples that tell you have to get a development env running

yarn add aws-scheduler

You have to setup these environment variables before the use:

AWS_ACCESS_KEY_ID=XXX
AWS_SECRET_ACCESS_KEY=XXX
AWS_REGION=eu-west-1
DYNAMODB_JOBS_TABLE=scheduled_jobs

Usage

import cronApi from "aws-scheduler";

const cron = cronApi();
cron.start({ port: 8080 });

Congratulation! Cron API is now running on "http://localhost:8080".

Available routes:

| Description | URL | Method | | ----------------------- | :----------- | :----: | | Get all scheduled tasks | /list | GET | | Create new task | /create | POST | | Delete task | /delete/:id | DELETE | | Reload jobs from DB | /reload-jobs | GET |

To create new task, you need to send POST request to /create, with body such as:

{
  "name": String,
  "data": Object,
  "cron": String,
  "date": String
}

When you create a new task, you can choose between "cron", and ISO date formats.

Example of task that executes once at specific date:

{
  "name": "Remind user before the event",
  "date": "2018-04-28T09:48:35.429Z",
  "data": { "eventName": "Eminem Concert", "email": "[email protected]" }
}

Example of task that executes every 30 seconds:

{
  "name": "Check for software updates",
  "cron": "*/30 * * * * *",
  "data": { "name": "winrar" }
}

Job Handlers

After job is added to the queue, job handler has to pull it off, do it's thing, and mark job as done.

Let's create job handler for user reminder example:

import { createJob, startJobs } from "aws-scheduler";

const userReminderJob = createJob({
  name: "Remind user before the event",
  handleJob({ email, eventName }, done) {
    console.log(`Sending ${email} reminder about the ${eventName} event.`);
    done();
  }
});

startJobs([userReminderJob]);

"createJob" function provides us with an easy API to setup the jobs listener. You must provide it a configuration object with the job name, and handleJob method that receives the data object from the job, and done method. After you job is finished, you have to call "done" method, to delete it from the queue. If job is failed for some reason, you want to return it to the queue. To mark job as "failed" and return it to the queue, you have to call "done" method with an Error object.

Build package before publishing to NPM

yarn build

Tests

yarn test