简体   繁体   English

如何检查 node-cron 执行 cron-job 所花费的时间

[英]How to check how much time node-cron is taking for executing cron-job

I want to check how much time cron job is taking to execute the cron job process for that particular cron job.我想检查 cron 作业执行该特定 cron 作业的 cron 作业进程需要多少时间。

After which if cron-job is taking more than 10 minutes to execute that process then that particular cron-job should be stopped and rest cron job should process as normal.之后,如果 cron-job 执行该过程的时间超过 10 分钟,则应停止该特定 cron-job 并且 rest cron 作业应正常处理。

I am using node-cron package for cron job.我正在使用 node-cron package 进行 cron 作业。

In below code I want to get how much time it takes to executes that process if it it is more than 10 minutes then in setTimeout() function i can use task.stop() method of cron to stop that cron job.在下面的代码中,我想知道执行该过程需要多长时间,如果它超过 10 分钟,那么在 setTimeout() function 中,我可以使用 cron 的 task.stop() 方法来停止该 cron 作业。

var task = cron.schedule('0 1 * * *', () => { console.log('Runing a job at 01:00') }

If I understand you correctly, you want to set a timeout to cancel a process if it exceeds 10 minutes of runtime.如果我理解正确,您想设置超时以在超过 10 分钟的运行时间时取消进程。 The best answer would depend on what kind of activity the process is doing.最佳答案将取决于流程正在执行的活动类型。 If you are using the child_process module to spawn another process you can start with an example from the documentation which kills the child process after 2 seconds and adjust it to your needs:如果您使用child_process模块生成另一个进程,您可以从文档中的一个示例开始,该示例会在 2 秒后终止子进程并根据您的需要进行调整:

'use strict';
const { spawn } = require('child_process');

const subprocess = spawn(
  'sh',
  [
    '-c',
    `node -e "setInterval(() => {
      console.log(process.pid, 'is alive')
    }, 500);"`
  ], {
    stdio: ['inherit', 'inherit', 'inherit']
  }
);

setTimeout(() => {
  subprocess.kill(); // Does not terminate the Node.js process in the shell.
}, 2000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM