So I have built a website on which I'm trying to run a script to schedule a task to run at 12am every night. Since I can't use require()
, due to the fact that I am running a web server, I am using browserify to use the node-cron tasks.
I have data_retrieve.js
which I am trying to implement in my HTML:
class RunCron {
constructor() {
var cron = require('node-cron');
cron.schedule('0 0 * * *', () => {
$.ajax({
type: "POST",
url: "/main.py",
data: { param: text}
}).done(function( o ) {
console.log("Done running py!");
});
});
//const cron = require('node-cron');
}
}
var r = new RunCron();
and then I have bundle.js
which is being generated, and I am getting the following error every time:
Uncaught TypeError: process.hrtime is not a function
at Scheduler.start (bundle.js:1791:33)
at new ScheduledTask (bundle.js:1749:29)
at createTask (bundle.js:1564:12)
at Object.schedule (bundle.js:1553:18)
at new RunCron (bundle.js:1229:8)
at 5.node-cron (bundle.js:1246:9)
at o (bundle.js:1:633)
at r (bundle.js:1:799)
at bundle.js:1:828
at bundle.js:1:318
Following the error to (bundle.js:1791:33)
:
Lines 1780 - 1821:
class Scheduler extends EventEmitter{
constructor(pattern, timezone, autorecover){
super();
this.timeMatcher = new TimeMatcher(pattern, timezone);
this.autorecover = autorecover;
}
start(){
// clear timeout if exists
this.stop();
let lastCheck = process.hrtime();
let lastExecution = this.timeMatcher.apply(new Date());
const matchTime = () => {
const delay = 1000;
const elapsedTime = process.hrtime(lastCheck);
const elapsedMs = (elapsedTime[0] * 1e9 + elapsedTime[1]) / 1e6;
const missedExecutions = Math.floor(elapsedMs / 1000);
for(let i = missedExecutions; i >= 0; i--){
const date = new Date(new Date().getTime() - i * 1000);
let date_tmp = this.timeMatcher.apply(date);
if(lastExecution.getTime() < date_tmp.getTime() && (i === 0 || this.autorecover) && this.timeMatcher.match(date)){
this.emit('scheduled-time-matched', date_tmp);
date_tmp.setMilliseconds(0);
lastExecution = date_tmp;
}
}
lastCheck = process.hrtime();
this.timeout = setTimeout(matchTime, delay);
};
matchTime();
}
stop(){
if(this.timeout){
clearTimeout(this.timeout);
}
this.timeout = null;
}
}