David Tuite
David Tuite

Reputation: 22643

Using forever with a watch file for restarting

Given the following directory structure.

# Currently in ~/myapp/current
tree
.
├── tmp
│   ├── restart.txt
│   ├── pids
├── log
│   ├── forever.log
├── myapp
│   ├── myapp.js

This works.

sudo NODE_ENV=production forever \
  -c "node --use_strict" \
  --append \
  -f \
  -l ~/myapp/current/log/forever.log \
  --pidFile ~/myapp/current/tmp/pids/myapp \
  --uid myapp \
  myapp/myapp.js

I want to be able to touch a file to restart forever so I'm trying to use the --watch argument. I have an empty file called restart.txt to watch. Unfortunately this fails:

sudo NODE_ENV=production forever \
  -c "node --use_strict" \
  --append \
  -f \
  -l ~/myapp/current/log/forever.log \
  --pidFile ~/myapp/current/tmp/pids/myapp \
  --uid myapp \
  --watch tmp/restart.txt \
  myapp/myapp.js

With this cryptic error:

warn:    --minUptime not set. Defaulting to: 1000ms
warn:    --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
error: Could not read .foreverignore file.
error: ENOENT: no such file or directory, open '/home/deployer/myapp/releases/20151224185314/.foreverignore'
error: Forever detected script exited with code: 0
events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: watch /home/deployer/myapp/releases/20151224185314/node_modules/lodash/lang/isTypedArray.js ENOSPC
    at exports._errnoException (util.js:855:11)
    at FSWatcher.start (fs.js:1313:19)
    at Object.fs.watch (fs.js:1341:11)
    at createFsWatchInstance (/usr/lib/node_modules/forever/node_modules/chokidar/lib/nodefs-handler.js:37:15)
    at setFsWatchListener (/usr/lib/node_modules/forever/node_modules/chokidar/lib/nodefs-handler.js:80:15)
    at FSWatcher.NodeFsHandler._watchWithNodeFs (/usr/lib/node_modules/forever/node_modules/chokidar/lib/nodefs-handler.js:228:14)
    at FSWatcher.NodeFsHandler._handleFile (/usr/lib/node_modules/forever/node_modules/chokidar/lib/nodefs-handler.js:255:21)
    at FSWatcher.<anonymous> (/usr/lib/node_modules/forever/node_modules/chokidar/lib/nodefs-handler.js:473:21)
    at FSReqWrap.oncomplete (fs.js:82:15)

I've tried a bunch of different paths (absolute, relative, relative to a --watchDirectory etc.) for the watch file and I can't get it to work. What's wrong?

Upvotes: 2

Views: 1432

Answers (1)

roskelld
roskelld

Reputation: 1130

From what I understand (the documentation on this is non-existent from what I can see). the --watch function makes forever check for a .foreverignore file, and if this is missing the program essentially fails and gets stuck in a loop never starting the module.

in your app directory create a file called .foreverignore and list everything that you do not want forever to watch. This basically tells forever to ignore changes to these files and not to restart if anything happens to them, which is great for log files or things that don't actually require your module to restart to get the benefits from.

Here's an example taken from my implementation:

File:

/apps/myapp/.foreverignore

Contents

node_modules/*
logs/*
conf/*
htdocs/*
*.log
*.gif
*.jpg
*.html

Once created make sure you restart forever for the file to be picked up.

Upvotes: 1

Related Questions