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.
sudo npm install -g @concorde2k/bus.scheduler
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:1835MarketStreet@d-esb-001.concorde.local"]
--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
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();