user1816679
user1816679

Reputation: 855

How do I execute a piece of code no more than every X minutes?

Say I have a link aggregation app where users vote on links. I sort the links using hotness scores generated by an algorithm that runs whenever a link is voted on. However running it on every vote seems excessive. How do I limit it so that it runs no more than, say, every 5 minutes.

Upvotes: 0

Views: 558

Answers (3)

Evan P
Evan P

Reputation: 1817

  • First you build a receiver that receives all your links submissions.
  • Secondly, the receiver push()es each link (that has been received) to a queue (I strongly recommend redis)
  • Moreover you have an aggregator which loops with a time interval of your desire. Within this loop each queued link should be poll()ed and continue to your business logic.

I have use this solution to a production level and I can tell you that scales well as it also performs.

Example of use;

var MIN = 5; // don't run aggregation for short queue, saves resources
var THROTTLE = 10; // aggregation/sec
var queue = [];
var bucket = [];
var interval = 1000; // 1sec

flow.on("submission", function(link) {
    queue.push(link);
});

___aggregationLoop(interval);
function ___aggregationLoop(interval) {
    setTimeout(function() {
        bucket = [];
        if(queue.length<=MIN) {
            ___aggregationLoop(100); // intensive
            return;
        }
        for(var i=0; i<THROTTLE; ++i) {
            (function(index) {
                bucket.push(this);
            }).call(queue.pop(), i);
        }
        ___aggregationLoop(interval);
    }, interval);
}

Cheers!

Upvotes: 1

coma
coma

Reputation: 16659

var yourVoteStuff = function() { 

    ...

    setTimeout(yourVoteStuff, 5 * 60 * 1000);
};

yourVoteStuff();

Before asking why not to use setTimeinterval, well, read the comment below.

Why "why setTimeinterval" and no "why cron job?"?, am I that wrong?

Upvotes: 1

beiller
beiller

Reputation: 3135

a) use cron job

b) keep track of the timestamp when the procedure was last run, and when the current timestamp - the timestamp you have stored > 5 minutes then run the procedure and update the timestamp.

Upvotes: 1

Related Questions