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

@concorde2k/bus.scheduler

v1.0.15

Published

A scheduler based on cron that is bus aware

Downloads

18

Readme

Bus Scheduler

This little utility is designed to allow you to create specific heartbeats that listeners can respond to. It can called as a library or used as a container.

Options

When working with this tool, there are several options that are available as CLI options, environment variables, or directly to the main function. These options and concepts are described here and when these options are referenced elsewhere, there, you can refer back here for the description.

Schedule

There isn't much point in a scheduler if you can't schedule. This tool uses cron syntax for defining the period that it operates in. cron defines 5 fields for the period, namely:

| field | values | |---|---| |minute |0-59| |hour |0-23| |day of month |1-31| |month |1-12 (or names, see below)| |day of week |0-7 (0 or 7 is Sunday, or use names)|

These fields are laid out in a string like so * * * * *, where each * asterisk can be replaced with a number as above, from the top of the table to the bottom mapping from left to right in the string, so that

Minute   Hour   Day of Month       Month          Day of Week
(0-59)  (0-23)     (1-31)    (1-12 or Jan-Dec)  (0-6 or Sun-Sat)
  0        2          12             *                *

There are also a number of handy shortcuts for various things, but I'll let you look those up on your own.

Examples

Schedule a cron to execute at 2am daily.

0 2 * * *

Schedule a cron to execute twice a day. Below example command will execute at 5 AM and 5 PM daily. You can specify multiple time stamp by comma separated.

0 5,17 * * *

Schedule a cron to execute every minute

* * * * *

Schedule a cron to execute on every Sunday at 5 PM

0 17 * * sun

Schedule a cron to execute on every 10 minutes

*/10 * * * *

Schedule a cron to execute on selected days. Below example will run on each Sunday and Friday at 5 PM.

0 17 * * sun,fri

Schedule a cron to execute on every four hours

0 */4 * * *

Etc. There is lots of good info on cron on the web. This utility accepts all valid cron strings.

Bus

The idea here is that when the scheduler fires, it will then send a message to the bus allowing responders to deal with it. It does not specify the contents of the message, it only specifies that the scheduler fired and responders should perform some time based task. On route and direct messages, you can use they key to indiate what kind of shedule event fired. For task and pub/sub messages use the exchange to tell you what kind of event fired.

Each scheduler fires one and only kind of message. You can specify the message type via environment variables.

EXCHANGE - The name of the exchange

KEY - The key for Direct and Route messages

EX_TYPE - The kind of message to send, can be one of task, pubSub, direct, route. The scheduler does not support RPC messages

A sample docker compose entry would look like:

version: '3'

services:

  api:
    image: registry.concorde2000.com:5000/busscheduler:latest
    environment:
      EXCHANGE: MyExchange
      KEY: SomeKey
      EX_TYPE: direct
      SCHEDULE: "0 9 * * *"
      MQ_URL: "amqp://concorde:1835MarketStreet@rabbitmq"
      JQ_URL: "redis://redis"
      LOG_LEVEL: "info"
      MSG: "Some string that will be sent with each message"
      DEBUG: "bus:scheduler:*"
    

CLI

You can just install this to the command and call it that way.

# install
sudo npm install -g @concorde2k/bus.scheduler

# run
bsched --help
Options:
  --help          Show help                                            [boolean]
  --version       Show version number                                  [boolean]
  -v              Sets the logging level for the process. `false` to turn
                  logging off. -v, -vv, -vvv...-vvvvv sets the logging level
                                                                         [count]
  --log-level     Sets the logging level for the process.
     [string] [choices: "trace", "debug", "warn", "data", "log", "info", "warn",
                                                       "error"] [default: "log"]
  --qUrl          The URL of the message queue     [string] [required] [default:
                    "amqp://concorde:[email protected]"]
  --jUrl          The URL of the job queue
                     [string] [default: "redis://d-esb-001.concorde.local:6379"]
  --exType, -t    The type of exchange to fire the message to[string] [required]
  --schedule, -s  A cron style definition for when the scheduler should fire
                                                             [number] [required]
  --exchange, -e  The exchange to send the message to                   [string]
  --key, -k       The key that describes the message                    [number]

Linking

Finally, you can also use this as a library. And if there is enough time, also a breakfast cereal. Install the library first

# install
npm install --save @concorde2k/bus.scheduler

Then use it like so:

const {connectedSchedule, ExchangeTypes} = require("@concorde2k/bus.scheduler");

// ...

const job = await connectedSchedule({
    cron: "0 * * * *",
    exchange: "SomeExchange",
    key: "SomeKey",
    type: ExchangeTypes.direct,
    autoStart: false,
    message: "Hi, I am  message. Love me."
});

job.start();