0

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;
}

}

Gu5t4v
  • 15
  • 6
  • I don't quite get this. Are you trying to run your scheduled tasks on the backend (ie with nodejs) or on the frontend (ie in the browser)? If the latter: You cannot run `node-cron` in the browser (that's why it's called *node*-cron) – derpirscher Mar 03 '23 at 12:26
  • Oh, I see. Is there any other alternative for node-cron that I can run in the browser? – Gu5t4v Mar 03 '23 at 12:54
  • I'm actually not sure how much sense this might make because 1) The browser tab has to be open ALL the time 2) even if the tab is open, but not in foreground, the timing might be off. So maybe you describe, what you are actually trying to do and if there might be another better way to do this ... – derpirscher Mar 03 '23 at 13:13

0 Answers0