简体   繁体   English

Nodejs运行日常任务

[英]Nodejs running daily task

I want to run a nodejs function daily. 我想每天运行一个nodejs函数。 I have pretty much decided to use node-schedule rather than a cron job. 我已经决定使用节点计划而不是cron作业。

The question is: Without a db what is the best way to ensure that I don't somehow run the function more than once? 问题是:没有数据库,什么是确保我不会以某种方式多次运行该函数的最佳方法? My though was to keep a log of each time I send out and double check by reading the log before sending and checking that I haven't sent yet today. 我的做法是保留每次发送的日志,并在发送之前先阅读日志以进行仔细检查,并检查是否今天尚未发送。 I know that node-schedule shouldn't run more often than I tell it but someone might restart the script or something stupid, I want to be extra safe. 我知道节点计划不应该运行得比我说的要多,但是有人可能会重新启动脚本或一些愚蠢的事情,我想特别安全。

Note: I cannot setup a db due to limited access to the server. 注意:由于对服务器的访问受限,我无法设置数据库。

One common solution is to create a pid file somewhere. 一种常见的解决方案是在某处创建一个pid文件。 If the pid file exists, exit the process early. 如果pid文件存在,请尽早退出该过程。 Otherwise create the pid file (writing process.pid to it) and delete it when the process exits. 否则,创建pid文件(将process.pid写入其中),并在进程退出时将其删除。

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

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