NodeJS:如何在服务器端监视大量文件/文件夹的更新?
我正在开发一个小型 NodeJS 应用程序,该应用程序本质上用作基于浏览器的桌面搜索,用于多个用户可以查询的基于 LAN 的服务器。局域网上的用户都可以访问该服务器上的共享文件夹,并且传统上习惯于仅将文件放在该文件夹中以便在每个人之间共享,我希望保持该过程相同。
我遇到的第一个解决方案是 fs.watchFile ,它已在其他 stackoverflow 问题中提到过。在第一个问题中,用户Ivo Wetzel 指出,在 Linux 系统上 fs.watchFile 使用 inotify,但认为 fs.watchFile 不应该用于大量文件/文件夹。
在另一个关于 fs.watchFile 用户的问题中tjameson 首先重申,在 Linux 上 inotify 将由 fs.fileWatch 使用,并建议仅使用 node-inotify-plusplus 和 node-walk 但再次声明此方法不应用于大量文件。通过评论和回应,他建议仅观察目录的修改时间,然后重新扫描相关目录以查找文件更改。
我最大的障碍似乎是,即使有 tjameson 的建议,监视的文件夹数量仍然存在严格限制(其中有很多并且还在不断增长)。此外,它必须递归地完成,因为目录树有点深,并且也可能在较低的分支上发生变化,所以我必须在每个文件夹级别监视以下内容(或者监视文件夹的修改时间,然后扫描以找出发生了什么):
- 创建文件或子文件夹
- 删除文件或子文件夹
- 移动文件或子文件夹
- 删除自我
- 移动自我
假设inotify具有与上面所说的一致的限制,那么仅对我来说这似乎可能当我有大量嵌套子文件夹时,显示器太多。真正很棒的方式看起来像是涉及 kqueue 我随后在 google 中找到了关于更好的 fs.fileWatch 的讨论主题组。
在我看来,很明显,保留相关文件和文件夹信息的数据库是查询方面的适当操作过程,但保持该数据库与相关目录下文件系统的实际状态同步将是挑战。
那么社区怎么想呢?是否有更好或众所周知的解决方案来解决我不知道的这个问题?最好只是观察所有感兴趣的目录的单个更改(例如修改时间),然后扫描以找出发生了什么?观察所有相关的 inotify 警报并适当修改数据库是否更好?这难道不是我一个农民就能解决的问题吗?
I am working on a small NodeJS application that essentially serves as a browser based desktop search for a LAN based server that multiples users can query. The users on the LAN all have access to a shared folder on that server and are traditionally used to just placing files within that folder to sharing among everyone, and I want to keep that process the same.
The first solution I came across was the fs.watchFile which has been touched on in other stackoverflow questions. In the first question user Ivo Wetzel noted that on a linux system fs.watchFile uses inotify but, was of the opinion that fs.watchFile should not be used for large amounts of files/folders.
In another question about fs.watchFile user tjameson first reiterated that on Linux inotify would be used by fs.fileWatch and recommended to just use a combination of node-inotify-plusplus and node-walk but again stated this method should not be used for a large number of files. With a comment and response he suggested only watching the modified times of directories and then rescanning the relevant directory for file changes.
My biggest hurdles seem to be that even with tjameson's suggestion there is still a hard limit to the number of folders monitored (of which there are many and growing). Also it would have to be done recursively because the directory tree is somewhat deep and can also be subject to change at the lower branches so I would have to monitor the following at every folder level (or alternatively monitor the modified time of the folders and then scan to find out what happened):
- creation of file or subfolder
- deletion of file or subfolder
- move of file or subfolder
- deletion of self
- move of self
Assuming the inotify has limits in line with what was said above then this alone to me seems like it may be too many monitors when I have a significant amount of nested subfolders. The real awesome way looks like it would involve kqueue which I subsequently found as a topic of discussion on a better fs.fileWatch in a google group.
It seems clear to me that keeping a database of the relevant file and folder information is the appropriate course of action on the query side of things, but keeping that database synchronized with the actual state of the file system under the directories of concern will be the challenge.
So what does the community think? Is there a better or well known solution for attacking this problem that I am just unaware of? Is it best just to watch all directories of interest for a single change e.g. modified time and then scan to find out what happened? Is it better to watch all the relevant inotify alerts and modify the database appropriately? Is this not a problem which is solvable by a peasant like me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看 monit。我用它来监视开发环境中文件的变化,并在相关项目文件发生变化时重新启动我的节点进程。
Have a look at monit. I use it to monitor files for changes in my dev environment and restart my node processes when relevant project files change.
我建议您查看 Dropbox API。
我在客户端使用 ruby 并在服务器端使用 Nodejs 实现了类似的东西。
最好的方法是保留哈希值以检查文件或文件夹是否发生更改。
I recommend you to take a look at the Dropbox API.
I implemented something similar with ruby on the client side and nodejs on the server side.
The best approach is to keep hashes to check if the files or folders changed.