kartikk221 / live-directory

A Simple-To-Use Dynamic Template Content Manager For Webservers
MIT License
12 stars 5 forks source link

Possible to disable file watching? #4

Closed moufmouf closed 1 year ago

moufmouf commented 1 year ago

The question might sound like a joke given the name of the package is "live-directory", but this is actually quite serious.

I'm using live-directory to serve static files for my application (https://github.com/thecodingmachine/workadventure)

It works very well and is providing great services (like automatic etag computation).

When I'm doing development, it is great to have the live-reload feature with new files detected automatically.

But when in production, my files are copied in a container that will always be static. The files will never change (if they change, this is because I'm deploying a new container). And when I scale my container (creating many instances of it), I'm getting this error message:

Error: ENOSPC: System limit for number of file watchers reached

I would like to know if there is a way to disable file watching completely when I'm running in production (and still have live-directory server static assets, compute etags, etc...)

Related issue: https://github.com/thecodingmachine/workadventure/issues/2741

kartikk221 commented 1 year ago

Hello, I don't believe there is a toggle to disable file-watching at the moment. It can def be implemented though with a simple boolean passed to the constructor, possibly something like hot_reload. Let me know If you would like to make a PR or If I can go ahead and push an update for that.

moufmouf commented 1 year ago

Hey @kartikk221 ,

Thanks a lot for the quick answer! I'll try to open a PR for that today.

kartikk221 commented 1 year ago

Hey @moufmouf upon merging and reviewing your PR request, I noticed a major problem that would prevent a flag like hot_reload from working with the current codebase. At the moment, live-directory uses chokidar to watch the specified root path and then populate it's directory tree of files. The problem is that chokidar creates watchers for all files as it scans the contents and there seems to be no way to simply receive the file identities without creating watchers for them hence you will always exhaust the system limit of file watchers.

In the next major update for live-directory I plan on taking a "map first, watch later" approach which would use a different mechanism to first map out all the contents of the specified path and then assign watchers as neccessary which should allow for the fine tuned control you require.

moufmouf commented 1 year ago

Hey @kartikk221 ,

Thanks a lot for the feedback. You are perfectly right. The PR is not perfect because we are actually starting by listening to all files, and then reverting back to not listening at all. That's better than nothing, but indeed, the right approach would be the "map first, watch later" approach :+1:

kartikk221 commented 1 year ago

Hey @moufmouf Just following up, was the recent 3.0.1 update with the addition of the static constructor option sufficient for your use case?

moufmouf commented 1 year ago

Hi @kartikk221 ,

Yes, this was sufficient and everything is working fine now :+1: