GithubHelp home page GithubHelp logo

restart when files change? about forever HOT 28 CLOSED

foreversd avatar foreversd commented on July 3, 2024
restart when files change?

from forever.

Comments (28)

indexzero avatar indexzero commented on July 3, 2024 1

Thanks to @mmalecki here for implementing this. Will be published in 0.6.8

from forever.

indexzero avatar indexzero commented on July 3, 2024

Interesting. I'll leave this open.

from forever.

eliaskg avatar eliaskg commented on July 3, 2024

+1 for me. Right now if I want to roll out a new version of my app I run

forever stopall
git pull
forever start server.js

If forever would observe code there would be only one command.
Also forever would be usable in development as an alternative to node-dev.

from forever.

Marak avatar Marak commented on July 3, 2024

I'm putting in a +1 from @tmpvar :-)

from forever.

derek avatar derek commented on July 3, 2024

If looking for ideas on how to implement, @davglass's spark2 has a '--watch' command that does this. It's very handy. https://github.com/davglass/spark2/blob/master/bin/spark2#L126

from forever.

ded avatar ded commented on July 3, 2024

glad this conversation got sparked up again :)

from forever.

indexzero avatar indexzero commented on July 3, 2024

Commits are welcome. I'm looking at you: @tmpvar, @ded and @drgath :-D

Overall it's probably a ~40-50 line change ... I'm heading back from SF to NYC tonight will see what I can do over the weekend. I'm still just one man though...

from forever.

guybrush avatar guybrush commented on July 3, 2024

you could use githooks

from forever.

ded avatar ded commented on July 3, 2024

i suppose i can have a peak at the source -- but something tells me i'd get lost quickly.

from forever.

indexzero avatar indexzero commented on July 3, 2024

@ded I'm in #nodejitsu most of the time if you want to chat about this. Most of the nuts and bolts to this change would be in monitor.js. The code is pretty well documented with method level granularity.

from forever.

derek avatar derek commented on July 3, 2024

I played around with it for a little bit tonight. Mostly referenced Dav's code. See lines 650-660: https://github.com/davglass/spark2/blob/master/bin/spark2#L650

It's really easy to monitor one file and fire off a restart event when it is edited. For this though, you'd want to monitor the whole tree and I'd want to investigate the performance/memory impacts and determine the appropriate poll time (ms). A couple files? No prob. But with NPM includes, forever currently checks in at 136 files. It would probably be fine, just haven't investigated yet. Here's the watchFile() function that Node will use. https://github.com/joyent/node/blob/master/lib/fs.js#L592

You'll also want to be sure to only restart once within {x} seconds, not one time for any file change within the tree. A git pull could wreak havoc. :)

Also, something Isaac mentioned in his Supervisor README, you'll want to keep an eye on memory leaks. As you spawn off new children, you'll want to make sure you clean up after yourself. forever might already do this, I haven't dug deep enough to know yet, this is my first time reviewing this module. Lots of editing could drastically increase the # of restarts compared to what forever is used to.

Finally, it appears this could be very useful functionality in a separate module considering how many process spawners make use of this pattern. Reminds me of event delegation in a DOM tree. Sample: require('stat-delegation').monitor(someDir, someCallback);

from forever.

indexzero avatar indexzero commented on July 3, 2024

@drgath Thanks for looking into this. There is already a good module for watching an entire directory tree: https://github.com/mikeal/watch

In @isaacs node-supervisor project, he watches the entire directory by default. This can become more problematic in npm > 1.0 because of the node_modules folder as you mentioned. Forever now has a global configuration available:

  config           Lists all forever user configuration
  set <key> <val>  Sets the specified forever config <key>
  clear <key>      Clears the specified forever config <key>

We could configure some default options (maybe watch-ignore?) which could ignore certain files by default. You're right that you would want to have some kind of setTimeout when handling file changes so that multiple file changes close in time do not cause multiple restarts.

This is tangentially related to this issue: https://github.com/indexzero/forever/issues/52 which requests adding a minimum interval between process restarts.

Let me know your thoughts and thanks again for your comment.

from forever.

diversario avatar diversario commented on July 3, 2024

+1 to this. Just started using Forever and I'm definitely missing automatic restarts (I used node-supervisor).

from forever.

clintandrewhall avatar clintandrewhall commented on July 3, 2024

+1 I use @Rem's nodemon. I'd love to be able to start it "forever". :-)

from forever.

clintandrewhall avatar clintandrewhall commented on July 3, 2024

I'd propose a .foreverignore file, rather than edit in config? Or specify a file name of ignored paths/files?

from forever.

clintandrewhall avatar clintandrewhall commented on July 3, 2024

If anyone's interested, I was able to create a work-around using @Rem's nodemon and a git hook:

#!/bin/sh
GIT_WORK_TREE=[SOME PATH]
export GIT_WORK_TREE
git checkout -f

...and then...

forever start -c nodemon [SOME_PATH]/server.js ...

from forever.

indexzero avatar indexzero commented on July 3, 2024

@clintandrewhall Interesting hack, but clearly you don't get all of the benefits of forever (such as the new hooks). Anyway, I will take this on soon enough people have been asking about it.

from forever.

clintandrewhall avatar clintandrewhall commented on July 3, 2024

I'm sorry... in this case, I've been using Forever as:

  1. An indexed list of currently-running node instances;
  2. A manager of different log outputs;
  3. An "auto-restarter" in the case of error, (nodemon halts in those cases)

I'm using a git-sourced nginx configuration as I add apps to a remote server; combined with nodemon, I've been able to restart my apps instantly. I'm afraid I don't see any documentation on the new hooks, (though I haven't delved far into the source code). Can you point me in the right direction, particularly in regards a better approach?

from forever.

FGRibreau avatar FGRibreau commented on July 3, 2024

+1 but for now I'll use the workaround:
forever start -c nodemon [SOME_PATH]/server.js ...

from forever.

mmalecki avatar mmalecki commented on July 3, 2024

I'm currently working on this. However, I'm not sure under what conditions should we restart script.

Possibilities:

  • when script itself changes
    yeah, but it's 2011 and we have things like require
  • when any file in script directory changes
    many node modules have directory structure:
bin/
  executable
lib/
  file.js
  nextfile.js

And what really changes are files in lib, thus it doesn't make sense.

  • when anything in parent directory of script changes
    why not parent of parent, then?
  • current working directory of forever
$ pwd
/home/maciej
$ forever start /usr/bin/whatever

So, again, it doesn't make sense.

  • wrapping each script into context with require replaced
    And no, I don't mean : running it in forever. I mean: let's write a tiny wrapper script, which makes use of VM module, runs script in different context and checks what it requires. We can later watch for changes only on files which application really uses.
    But well, it's hackish. It would require a lot of context mangling (not only require, also process.argv, etc.). I'm not sure if it would be acceptable for production (on the other hand, I think this auto restart mode shouldn't be used in production anyway).

I would really appreciate any feedback on this.

from forever.

clintandrewhall avatar clintandrewhall commented on July 3, 2024

Why not use a combination of .gitignore and a new .foreverignore file, (the model nodemon uses)?

If the user wants to ignore certain folders, have them configure it? Otherwise, if anything changes in the project, it restarts?

from forever.

lbdremy avatar lbdremy commented on July 3, 2024

+1 it's a great feature that miss to Forever.

from forever.

indexzero avatar indexzero commented on July 3, 2024

@mmalecki Well put together list of options, thanks for taking this on. It's something I've wanted to implement for a while but just haven't had the time to commit.

I think that a list of file watchers is really the way to go here, working with require statements and a shim can be very fragile. It also won't work for non-nodejs processes (which many users actually run with forever). e.g.

$ forever start -c python somescript.py

The approach suggested by @clintandrewhall is probably most prudent. Just watch everything unless it is in the .gitignore or .foreverignore (also ignore any dotfiles or directories by default, e.g. .git). For the file watching this library: https://github.com/mikeal/watch

from forever.

clintandrewhall avatar clintandrewhall commented on July 3, 2024

+1 for https://github.com/mikeal/watch I just stumbled upon this library and really like it.

Thanks for looking into this; it'll make my deployments infinitely easier. I use a post-receive git hook on EC2 with a bare repo: git push ec2 and my code goes in and everything restarts. So convenient. :-)

One feature to consider that other monitor solutions don't offer: triggering cleanup or pre-restart tasks. For example, if certain files change (e.g. package.json), run a task/script (e.g. npm install, npm update). I'd be happy to look into adding it after your enhancement work, but it's something to keep in mind?

Thanks again!

from forever.

mmalecki avatar mmalecki commented on July 3, 2024

For the file watching this library: https://github.com/mikeal/watch

Yes, I'm going to use this library.

The approach suggested by @clintandrewhall is probably most prudent. Just watch everything unless it is in the .gitignore or .foreverignore (also ignore any dotfiles or directories by default, e.g. .git).

OK, so this is the way to go. However, I'm not sure about .gitignore. This is usually where output files go, and we may experience cases where file we're running is an output file (e.g. compiled CoffeeScript or just C applications).

Thanks for looking into this; it'll make my deployments infinitely easier. I use a post-receive git hook on EC2 with a bare repo: git push ec2 and my code goes in and everything restarts. So convenient. :-)

I'm using quite similar method for deployments to my server :).

One feature to consider that other monitor solutions don't offer: triggering cleanup or pre-restart tasks. For example, if certain files change (e.g. package.json), run a task/script (e.g. npm install, npm update). I'd be happy to look into adding it after your enhancement work, but it's something to keep in mind?

That's in fact a great idea, we would only need to specify when to run actions, etc. Also, keep in mind that forever is content-agnostic, it can run whatever you like, so defining this kinds of actions and hooks in forever itself might not be a good idea.
We may try emitting an event when file changes, what do you think, @indexzero?

from forever.

indexzero avatar indexzero commented on July 3, 2024

One feature to consider that other monitor solutions don't offer: triggering cleanup or pre-restart tasks. For example, if
certain files change (e.g. package.json), run a task/script (e.g. npm install, npm update). I'd be happy to look into adding it
after your enhancement work, but it's something to keep in mind?

This is interesting, but I'm curious what the CLI api would look like? It seems like a messy problem.

from forever.

ded avatar ded commented on July 3, 2024

wow. awesome. will upgrade soon

from forever.

clintandrewhall avatar clintandrewhall commented on July 3, 2024

LOVE IT. Thanks for all your effort...!

from forever.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.