如何每隔X分钟执行一段代码?

时间:2022-10-19 01:06:19

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.

假设我有一个链接聚合应用程序,用户在链接上投票。我使用由算法生成的热度分数对链接进行排序,该算法在链接被投票时运行。然而,在每次投票中运行它似乎过多。我如何限制它,使其运行时间不超过每5分钟一次。

3 个解决方案

#1


1  

a) use cron job

a)使用cron作业

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.

b)跟踪上次运行该过程的时间戳,以及当前时间戳 - 您存储的时间戳> 5分钟然后运行该过程并更新时间戳。

#2


1  

var yourVoteStuff = function() { 

    ...

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

yourVoteStuff();

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

在询问为什么不使用setTimeinterval之前,请阅读下面的注释。

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

为什么“为什么setTimeinterval”而没有“为什么要做cron工作?”?我错了吗?

#3


1  

  • 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)
  • 其次,接收器push()es每个链接(已经收到)到队列(我强烈推荐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.
  • 此外,你有一个聚合器,它以你想要的时间间隔循环。在此循环中,每个排队的链接都应该是poll()ed并继续您的业务逻辑。

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!

干杯!

#1


1  

a) use cron job

a)使用cron作业

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.

b)跟踪上次运行该过程的时间戳,以及当前时间戳 - 您存储的时间戳> 5分钟然后运行该过程并更新时间戳。

#2


1  

var yourVoteStuff = function() { 

    ...

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

yourVoteStuff();

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

在询问为什么不使用setTimeinterval之前,请阅读下面的注释。

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

为什么“为什么setTimeinterval”而没有“为什么要做cron工作?”?我错了吗?

#3


1  

  • 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)
  • 其次,接收器push()es每个链接(已经收到)到队列(我强烈推荐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.
  • 此外,你有一个聚合器,它以你想要的时间间隔循环。在此循环中,每个排队的链接都应该是poll()ed并继续您的业务逻辑。

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!

干杯!