te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - NodeJs setInterval without extending process life time - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - NodeJs setInterval without extending process life time - Stack Overflow

programmeradmin3浏览0评论

I simplified it to better understand what I need: The task that I have to do is, that we have a long unknown nodejs process (with a lot of queued async functions where we never know when they are finished or something like that) and we want to have an update process, that we store current process state in database. For that we have a start up (it stores "start") and a end process that is on top of process.on('beforeExit', function () { ... });. Now we have to handle the "still in running" process that is requested by our customer. For that we want to update the state every ten minutes to running with timestamp (this function already exists and is called state.setRunningState()

Now I have the problem how can I trigger that function every ten minutes. For that I was going the approach to trigger on each event of the working process this function and pares if it is older then 10 minutes ago. Problem is: Some times there are much more time without any event. So second option is setInterval() and here is what my question is about: If I use setInterval my nodejs process will never reach an end, so the process will run endless until Interval is cleared. But I also do never know when I should call clearInterval()

So the Question is: Is there a way to create such a timeout without extending the life time of the nodejs process. If everything is done it should end and ignore the rest of the interval.

I simplified it to better understand what I need: The task that I have to do is, that we have a long unknown nodejs process (with a lot of queued async functions where we never know when they are finished or something like that) and we want to have an update process, that we store current process state in database. For that we have a start up (it stores "start") and a end process that is on top of process.on('beforeExit', function () { ... });. Now we have to handle the "still in running" process that is requested by our customer. For that we want to update the state every ten minutes to running with timestamp (this function already exists and is called state.setRunningState()

Now I have the problem how can I trigger that function every ten minutes. For that I was going the approach to trigger on each event of the working process this function and pares if it is older then 10 minutes ago. Problem is: Some times there are much more time without any event. So second option is setInterval() and here is what my question is about: If I use setInterval my nodejs process will never reach an end, so the process will run endless until Interval is cleared. But I also do never know when I should call clearInterval()

So the Question is: Is there a way to create such a timeout without extending the life time of the nodejs process. If everything is done it should end and ignore the rest of the interval.

Share Improve this question asked Apr 6, 2017 at 12:10 Jessica WeberJessica Weber 1322 silver badges11 bronze badges 6
  • FYI you can exit the process by process.exit() – E. Sundin Commented Apr 6, 2017 at 12:12
  • I know that, but that is no answer here because still: I do not know, when the main process is ending. It is pletely async and only event driven. Btw: process.exit() also skip the event process.on("beforeExit") – Jessica Weber Commented Apr 6, 2017 at 12:14
  • I've read this question like 3 times and I still can't understand what you are asking... "Is there a way to create such a timeout without extending the life time of the nodejs process" <-- So, you don't want an interval? Have you had a look at setTimeOut instead? "But I also do never know when I should call clearInterval()" <-- So, it's event-based, but you don't have an event that will help you to know when to clear the interval? And you expect other people to know? – Josep Commented Apr 6, 2017 at 12:21
  • Again: I have to solve it on side of the interval. setTimeout will not change that because it has to be used recursive. The problem is here the behaviour of setTimeout. Means I need an alternative to basic timeout handling of nodejs – Jessica Weber Commented Apr 6, 2017 at 12:23
  • "I have to solve it on side of the interval." <-- What does that even mean? You need to solve it inside the interval function? "I need an alternative to basic timeout handling of nodejs" <--- And this? What does this mean? – Josep Commented Apr 6, 2017 at 12:27
 |  Show 1 more ment

2 Answers 2

Reset to default 18

Contrary to some of the ments here, this is not a strange requirement to have something executed periodically while the process is running without making the process run infinitely which would make it quite pointless.

There is a built-in mechanism for that. If you don't want your interval (or timeout) to stop the process from exiting then you need to use the .unref() method.

Instead of:

setInterval(() => {
  console.log('Interval');
}, 1000);

use:

setInterval(() => {
  console.log('Interval');
}, 1000).unref();

and your interval will not stop the process from exiting if there are no other events pending.

Try running this example:

setInterval(() => {
  console.log('Interval');
}, 1000).unref();

setTimeout(() => {
  console.log('Timeout 1');
}, 3000);

setTimeout(() => {
  console.log('Timeout 2');
}, 5000);

See the docs:

  • https://nodejs/api/timers.html#timers_timeout_unref

Let me see if I get this straight. 1- You want to trigger an event every x mins unless if the existing process has ended? 2- You say that node would not quit the process as long as there is a running set interval. 3- you say that since your process runs " lot of queued async functions" you cannot know when the process should end

I think the simplest solution would be to just set another interval to run at a higher frequency and to clear the interval if all functions have returned. Otherwise you might be interested in reading about webworkers

https://www.npmjs./package/webworker-threads

发布评论

评论列表(0)

  1. 暂无评论