Sample RabbitMQ scheduler implementation for scheduled device tracking
Follow the installations instructions on the project website.
After the installation, please ensure you can run the local server on your machine before proceeding.
If you are on OSX, you should be able to start a local server with the rabbitmq-server
command.
Before getting started with the scheduler, please ensure you have ...
- installed RabbitMQ locally or have the service running in your production environment
- a HyperTrack account
- the HyperTrack SDK in your application (iOS, Android, or React Native) or use our sample app to send location data (iOS or Android)
- Set the following environmental varibales in an
.env
file or in on your host (e.g. Heroku)process.env.CLOUDAMQP_URL
needs to resolve to an accessible serverprocess.env.HT_ACCOUNT_ID
needs to be your HyperTrack account ID (from the dashboard)process.env.HT_SECRET_KEY
needs to be your HyperTrack secret key (from the dashboard)process.env.NODE_ENV
is set toproduction
to avoid test execution on a minute basis
This project is set up to be deployed to Heroku within seconds.
In the /scheduler
folder, you will find a schedule.js
file. Please review the constants and change according to your preferences. You can start the scheduler using node:
node scheduler/schedule.js
Note: If deployed outside of production, the scheduler will add tasks to the queue every minute. Keep in mind that the scheduler will add tasks indepdent of execution. The tasks will "pile up" if not executed right away.
In the /worker
folder, you will find a work.js
file. Please review the constants and change according to your preferences. You can start the scheduler using node:
node worker/work.js
Note: If deployed outside of production, the worker will seize tasks from the queue every minute. Keep in mind that the worker will seize ALL tasks in the queue at once.
You can modify the cron schedules and add/update/remove jobs based on yoru own requirements.
In the /jobs
folder, you will find multiple files based. These files define job names and the cron schedule expression. Feel free to change the schedule (with the help of tools like crontab guru).
In on of the /jobs
files, you cann append a JSON object to the array:
{
name: "Sample Task Name",
message: {
taskName: "sampleTask", // this needs to match in the worker exeuction
queue
},
cronTime,
repeat: 1
}
Next, you need to update your worker/work.js
and add a new switch ase for the newly defined taskName
:
switch (message.taskName) {
case "sampleTask":
someMethod(); // this is your own method for the task execution
break;
...