-
Notifications
You must be signed in to change notification settings - Fork 639
node-cron fails in docker-compose #299
Description
I have dockertized this script to fire off every X seconds, which goes and grabs some data from an employee page and serves it out over rest. Problem is -- I can't get it to run in a docker container, but I can if i just node server.js --
Any ideas?
// ```
const express = require('express');
// const expressGraphQL = require('express-graphql');
// const schema = require('./schema/schema');
// const bodyParser = require('body-parser');
var CronJob = require('cron').CronJob;
var Nightmare = require('nightmare');
require('nightmare-inline-download')(Nightmare);
var fs = require('fs');
var nightmare = Nightmare({
switches: {
'ignore-certificate-errors': true,
'show': true
}
});
//libs for json conversion
xml2js = require('xml2js');
var util = require('util')
var path = require('path');
// const app = express();
// app.use('/graphql', expressGraphQL({
// schema,
// graphiql: true
// }));
// app.use(bodyParser.json());
// app.listen(4000 || process.env.port, () => {
// console.log('Listening');
// });
const STATE = process.env.state || "AR";
const TIME = process.env.time || 1 * 10000;
console.log('STATE: ' + STATE);
console.log('TIME: ' + TIME);
var job1 = new CronJob({
cronTime: '0 */2 * * * *',
onTick: function() {
console.log('job 1 ticked');
var savejson = __dirname + '/latest.json';
var options = {
object: false,
reversible: false,
coerce: false,
sanitize: false,
trim: false,
arrayNotation: false
};
var chars = {
'<': '<',
'>': '>',
'(': '(',
')': ')',
'#': '#',
'&': '&',
"'": '''
};
nightmare
.goto('https://google.com')
.type('#city', STATE)
.type('#outtype', 'XML')
.click('#submitpanel1')
.wait('#webkit-xml-viewer-source-xml')
.evaluate(() => document.querySelector('#webkit-xml-viewer-source-xml').innerHTML)
.then((result) => {
console.log(result)
fs.writeFileSync('latest.xml', result);
})
.then(() => {
xmlFileToJs('latest.xml', function(err, obj) {
var jsonStr = '{"employees":[]}';
var object = JSON.parse(jsonStr);
for (var prop in obj.VFPData.temp) {
var data = obj.VFPData.temp[prop];
object['employees'].push({
id: parseInt(prop).toString(),
firstname: data.firstname,
lastname: data.lastname,
name: data.name,
fullname: data.fullname,
organization: data.ofcdesc,
email: data.inet,
voice: data.voice,
cellphone: data.pp_cell,
reportTo: data.rptto,
fax: data.fax,
bd_name: data.bd_name,
bd_address: data.bd_address,
bd_city: data.bd_city,
bd_state: data.bd_state,
bd_zip: data.bd_zip,
bd_room: data.room,
appointed: data.appointed,
formltitle: data.formltitle
});
}
jsonStr = JSON.stringify(object, null, 4);
console.log(jsonStr)
fs.writeFile(savejson, jsonStr, function(err) {
if (err) {
console.log(err);
} else {
console.log("JSON saved to " + savejson);
}
});
if (err)
throw(err);
jsToXmlFile('latest2.xml', obj, function(err) {
if (err)
console.log(err);
}
)
});
})
.catch((error) => {
console.error('Search failed:', error);
});
function xmlFileToJs(filename, cb) {
var filepath = path.normalize(path.join(__dirname, filename));
fs.readFile(filepath, 'utf8', function(err, xmlStr) {
if (err)
throw(err);
xml2js.parseString(xmlStr, {
ignoreAttrs: true,
mergeAttrs: true,
emptyTag: null,
explicitArray: false,
trim: true,
normalize: true,
preserveChildrenOrder: true,
async: true
}, cb);
});
}
},
start: true,
runOnInit: true,
onComplete: function() {
console.log("Completed")
},
timeZone: 'America/Chicago'
});