用node.js调用flock?

发布于 2024-10-31 17:56:38 字数 186 浏览 1 评论 0原文

我有 cron 作业来运行 node.js 脚本。

想要使用集群锁定文件以确保我的 cron 作业不被锁定 重叠。

有什么好的模块可以进行文件锁定吗?

或者我应该在子进程中调用它?

或者我不应该做任何文件锁定?

抱歉,我是新手,不确定文件锁定是否适合异步 env 类似节点。谢谢

I have cron job to run node.js scripts.

Want to use flock to lock a file to make sure my cron jobs are not
overlapped.

Any good module for doing file locking ?

Or I should call that in child process ?

Or I should not do any file locking ?

Sorry, I am new to this and not sure file locking is good for async
env like node. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

零度° 2024-11-07 17:56:38

如果您只是想防止 cron 作业重叠,请考虑在 crontab 中使用“flock”实用程序。

如果您的 cron 行看起来像这样:

*/10 * * * * /usr/bin/node /usr/local/share/myscript

您可以将其更改为:

*/10 * * * * /usr/bin/flock -n /var/lock/myscript /usr/bin/node /usr/local/share/myscript

这将尝试获取锁定文件 /var/lock/myscript 上的锁定。如果可以,它将在该行的其余部分运行该命令,然后释放锁定;如果没有(因为有另一个作业正在运行),它将失败。

这使您不必添加大量对“fs-ext”等的依赖项。

有关更多信息,请访问 http://linux.die.net/man/1/flock

If you're just trying to keep cron jobs from overlapping, consider using the "flock" utility in your crontab instead.

If your cron line looks something like this:

*/10 * * * * /usr/bin/node /usr/local/share/myscript

You can just change it to this:

*/10 * * * * /usr/bin/flock -n /var/lock/myscript /usr/bin/node /usr/local/share/myscript

This will try to get the lock on the lockfile /var/lock/myscript. If it can, it will run the command on the rest of the line and then release the lock; if not (because there's another job running), it will fail.

This keeps you from having to add a lot of dependencies on 'fs-ext' and so on.

There's more information at http://linux.die.net/man/1/flock

一江春梦 2024-11-07 17:56:38

请参阅 fs-ext 包中的 flock 函数: https://github.com/baudehlo/node-fs-ext

See flock function in fs-ext package: https://github.com/baudehlo/node-fs-ext

梦毁影碎の 2024-11-07 17:56:38

还有一个用于锁定文件的数据包 fcf-framework-lock: https://www.npmjs.com/package/fcf-framework-lock" rel="nofollow noreferrer">https://www.npmjs.com/package/fcf-framework-lock" npmjs.com/package/fcf-framework-lock

包功能描述页面: https://fcf-framework.tech/packages/fcf-framework-lock

let lock = require("fcf-framework-lock");
let util = require("util");

async main() {
  // Take the lock
  const lockId = await util.promisify(lock.lockFile)("LOCK_FILE_PATH");

  // ...
  // let's do something
  // ..

  // Free the lock
  await util.promisify(lock.unlockFile)(lockId);
}

main();

There is also a packet fcf-framework-lock for locking files: https://www.npmjs.com/package/fcf-framework-lock

Package feature description page: https://fcf-framework.tech/packages/fcf-framework-lock

let lock = require("fcf-framework-lock");
let util = require("util");

async main() {
  // Take the lock
  const lockId = await util.promisify(lock.lockFile)("LOCK_FILE_PATH");

  // ...
  // let's do something
  // ..

  // Free the lock
  await util.promisify(lock.unlockFile)(lockId);
}

main();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文