3
votes

I'm trying to implement the custom clock processes model that heroku suggests for custom-based cron scheduling, found here.

My Procfile is identical to the one suggested in the link:

web: node index.js
worker: node bot.js
clock: node clock.js

My app lives in index.js, and I can successfully run that with heroku local web and see my content through http://localhost:5000. The problem is, though, that some actions on my app cause a cron-job to be scheduled, and it is this part that I am not successfully getting to work at the moment. I am inspiring this code from this tutorial.

My clock.js file looks like this:

var CronJob = require('cron').CronJob;
var bot = require('./bot.js');

function setupJob(cronTimeFormatString) {
    new CronJob({
    cronTime: cronTimeFormatString,
    onTick: bot.setupJob(),
    start: true,
    timeZone: "America/Los_Angeles"
  });
}

The goal would be to call this from my app in a way like:

 var clock = require('./clock.js');
 clock.setupJob('00 00 11 * * 1-5'); // this is a job that runs on weekdays at 11am

Currently, bot.js looks like:

module.exports = {
  setupJob: function() { /* do stuff */
    console.log("start background job");
  }
};

The first question is all of this needed? My end goal is simply to set up a cron job that will run a user-defined number of times, but the Heroku Scheduler is not powerful enough for my needs, from my exploration.

The actual question, though, is when I try to run the worker process alongside the clock and app, using either heroku local OR foreman start, I get an immediate SIGTERM for all processes. It seems like it is because the worker process is immediately exiting, but does that mean I need to make it listen on a port? Wouldn't that defeat the purpose of it being a spun-up worker process only active when needed?

1

1 Answers

2
votes

Just run it with node clock.js

You can test individual pieces of the code(replace clock.js with index.js or bot.js), but cant run them all together.

Make sure you change the cronjob to something that executes every minute or whatever you prefer before you run clock.js