Rober Rober - 5 months ago 23
Node.js Question

Scheduled woker on Heroku

I'm working on this component that runs scheduled on Heroku and I can't make it run properly. I followed this and this posts to get it working. I manage to run the scheduled task on heroku according to

heroku logs
. The problem that I have is that if I run the code within a module (I need this to run on scheduled basis) it doesn't run the xray module and I'm not sure why this happens.

This is my code without module and its output if I run the code from the bin folder according to one of the links attached up here, which is what heroku will do to run the scheduled task:

var Xray = require('x-ray');

// helpers
var x = Xray({
filters: {
cleanPrice: function (value) {
return typeof value === 'string' ? value.replace(/\r|\t|\n|€/g, "").trim() : value
},
whiteSpaces: function (value) {
return typeof value === 'string' ? value.replace(/ +/g, ' ').trim() : value
}
}
});

console.log("START - Scraping categories...");

x('http://google.com', 'title')(function (err, title) {
console.log(title) // Google
})



λ bin\node init
START - Scraping categories...
Google


This is my code within a module and the output

module.exports = {
start: function () {

var Xray = require('x-ray');

// helpers
var x = Xray({
filters: {
cleanPrice: function (value) {
return typeof value === 'string' ? value.replace(/\r|\t|\n|€/g, "").trim() : value
},
whiteSpaces: function (value) {
return typeof value === 'string' ? value.replace(/ +/g, ' ').trim() : value
}
}
});

console.log("START - Scraping categories...");

x('http://google.com', 'title')(function (err, title) {
console.log(title) // Google
})

}
}


the output


λ bin\node init
START - Scraping categories...


I'm missing the word "Google". Why this happens if I put my code within a module???

Answer

The problem was in the actual task

#! /app/.heroku/node/bin/node

"use strict";

var app = require('../src/app.js');
function initScrape() {
  app.start();
}
initScrape();
process.exit(1); // tell node to exit (prevent promise)

By adding process.exit(1); I was preventing any promise to be resolved. This is needed for nodejs to stop the execution.

Comments