NodeJS Forever package minUptime and spinSleepTime warnings - node.js

I'm trying to run the forever function for node.js but I get below warnings;
C:\serv>forever start SERVER.js
warn: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up f
or at least 1000ms
info: Forever processing file: SERVER.js
How to set --minUptime and --spinSleepTime to remove these warnings
Installed forever package with npm install forever -g

These are only warnings. You could go on ignoring them, if you want.
But if you want to explicitly set them, forever --help tells you how to do so. Just start forever with:
forever start --minUptime 1000 --spinSleepTime 1000 SERVER.js

The documentation is not very exhaustive, a bit of additional information.
In the following examples we will use two scripts:
fail-fast.js:
process.exit(1);
fail-slow.js:
setTimeout(() => { process.exit(1); }, 2000);
1) using defaults
forever fail-fast.js
fail-fast.js script will execute only once, then no other start attempts will be made.
forever fail-slow.js
fail-slow.js script will be restarted indefinitely, as it stays up more than 1000ms (default value of minUptime if not specified). You can limit the number of restarts with the -m parameter.
2) setting only minUptime
forever --minUptime 10000 fail-fast.js
forever --minUptime 10000 fail-slow.js
Both fail-fast.js and fail-slow.js will be never be restarted, because we extended minUptime to 10 seconds and now fail-slow.js is considered spinning.
3) setting spinSleepTime
Whenever you set spinSleepTime (with or without minUptime), your process will restart even if it is considered 'spinning'.
forever --spinSleepTime 30000 fail-fast.js
forever --spinSleepTime 30000 fail-slow.js
Both scripts will be restarted forever, waiting spinSleepTime milliseconds between restarts.

In short:
When stop
if hadRunTime >= minUptime
restart
else if spinSleepTime != 0
wait spinSleepTime
restart
else
stop and no restart
#Megadix The answer has something wrong with spinSleepTime.
fail-fast.js will restart wating spinSleepTime,but fail-slow.js will restart immediately,no waiting! It can be Proved by:
console.log((new Date()).getTime());
setTimeout(() => {
process.exit(1);
}, 2000);
output like:
1468812185697
error: Forever detected script exited with code: 1
error: Script restart attempt #1
1468812187766
error: Forever detected script exited with code: 1
error: Script restart attempt #2
1468812189834
error: Forever detected script exited with code: 1
error: Script restart attempt #3
1468812191901
error: Forever detected script exited with code: 1
error: Script restart attempt #4
1468812193977
error: Forever detected script exited with code: 1
error: Script restart attempt #5
1468812196039
error: Forever detected script exited with code: 1
error: Script restart attempt #6
1468812198107
error: Forever detected script exited with code: 1
error: Script restart attempt #7
1468812200172
error: Forever detected script exited with code: 1

forever start --minUptime 1234 --spinSleepTime 3421 SERVER.js
https://github.com/nodejitsu/forever#usage

I'm assuming you are using express module that is way after you using - forever start SERVER.js - your server is not running, because express module run server in path ./bin/www - so you should use this command - forever start ./bin/www --name="SERVER" - name is the name of your js file, by default app.js

Related

Live debugging a frozen nodejs program

I have a nodejs process running but after a couple hours it keeps just getting stuck in some infinite loop somewhere, no error message or timeout message right now. So I am trying to live debug the frozen program:
On the command line I run $ top to get the process id of my node program, then run these commands to go into the node debug mode:
//my node PID=2599
$ kill -SIGUSR1 2599
$ node inspect -p 2599
debug> pause
break in internal/timers.js:483
481
482 function processTimers(now) {
>483 debug('process timer lists %d', now);
484 nextExpiry = Infinity;
485
debug> bt
#0 processTimers internal/timers.js:483:4
debug>
The two debug commands I've used so far are bt and pause, and I can see it points to a line in internal/timers.js, but where can I fine the location of this file? I've searched for the filename and code in my project but can't find it anywhere. I am running this version:
$ npm -v
7.12.0
$ node -v
v14.16.1

jit-grunt: Plugin for the "watch" task not found

after executing >> "grunt server" i saw these errors i don't know what and where i need to attach or detach some code.
one more thing DeprecationWarning: while nowhere in the code i wrote node --debug and node --debug-brk.
$ grunt server
Running "server" task
>> The `server` task has been deprecated. Use `grunt serve` to start a server.
Running "serve" task
Running "clean:server" (clean) task
>> 0 paths cleaned.
Running "env:all" (env) task
Running "express:dev" (express) task
Starting background Express server
(node:5497) [DEP0062] DeprecationWarning: `node --debug` and `node --debug-brk` are invalid. Please use `node --inspect` or `node --inspect-brk` instead.
Stopping Express server
Running "wait" task
>> Waiting for server reload...
Done waiting!
jit-grunt: Plugin for the "watch" task not found.
If you have installed the plugin already, please setting the static mapping.
See https://github.com/shootaroo/jit-grunt#static-mappings
Warning: Task "watch" failed. Use --force to continue.
Aborted due to warnings.
Execution Time (2018-09-25 12:24:48 UTC+5:30)
loading tasks 124ms ▇▇▇▇ 7%
express:dev 106ms ▇▇▇ 6%
wait 1.5s ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 85%
Total 1.8s
Solution
npm install grunt-contrib --save-dev
and add this line before the last line of grunt.js:
grunt.loadNpmTasks('grunt-contrib');

Heroku custom clock process fails to run at each interval using cron

I'm not sure what I'm doing wrong, but I'm trying to follow this article. I have the following Procfile:
web: node index.js
worker: node bot.js
clock: node clock.js
and inside my clock.js, I have:
var CronJob = require('cron').CronJob;
var bot = require('./bot.js');
new CronJob({
cronTime: "* * * * *",
onTick: bot.start(),
start: true,
timeZone: "America/Los_Angeles"
});
and my bot.js is structured like so:
var config = require('./config');
// ... other includes
module.exports = {
start: function() {
// code
}
}
My structure matches the article pretty much exactly, but what's going on? Here are my logs:
2016-12-16T06:00:48.935847+00:00 heroku[web.1]: Starting process with command `node index.js`
2016-12-16T06:00:49.332925+00:00 heroku[worker.1]: Starting process with command `node bot.js`
2016-12-16T06:00:49.736519+00:00 heroku[clock.1]: Starting process with command `node clock.js`
2016-12-16T06:00:50.058125+00:00 heroku[worker.1]: State changed from starting to up
2016-12-16T06:00:50.395082+00:00 heroku[clock.1]: State changed from starting to up
2016-12-16T06:00:51.899971+00:00 app[web.1]: app running on port 21470
2016-12-16T06:00:52.505353+00:00 heroku[worker.1]: State changed from up to crashed
2016-12-16T06:00:52.491705+00:00 heroku[worker.1]: Process exited with status 0
2016-12-16T06:00:52.698864+00:00 heroku[web.1]: State changed from starting to up
So I can see that I'm starting up the worker (which does nothing) and it then crashes. Then my clock starts up , but never calls the worker again? It's definitely not starting up every minute.
Is the article I followed old and no longer applies? How should I correctly call the worker?
Do you have a process.exit(0) command somewhere in bot.start()?
Heroku crashes a dyno when it comes across the command (see this link for more info). If you remove that, it should potentially solve the problem. It would be helpful to post the full bot.start function to verify.

forever start automatic.js error

so i wanted to make the app running on openshift
i made an application and added the bitbucket url
(https://bitbucket.org/srabouin/backpack.tf-automatic)
its used to sell ingame items
so i open putty and do: cd app-root/runtime/repo/
then i type: node automatic.js
then it starts, but when i close putty, the app closes too...
its not made for openshift, so i saw maby you needed to edit the package.json file or something.
1) i would like to know what to change, or if you could change it for me
2) i use filezilla now to edit files and i think its working
i tryed to do this https://blog.openshift.com/run-your-nodejs-projects-on-openshift-in-two-simple-steps/
Could someone help me with forever?
i did: npm forever install
then: forever start automatic.js
[trading-kurryworst003.rhcloud.com repo]\> forever start automatic.js
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
info: Forever processing file: automatic.js
fs.js:549
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
Error: ENOENT: no such file or directory, open '/var/lib/openshift/56d88d2e0c1e661d250000a9/.forever/_pTz.log'
at Error (native)
at Object.fs.openSync (fs.js:549:18)
at Object.forever.startDaemon (/var/lib/openshift/56d88d2e0c1e661d250000a9/io-js/bin/iojs/lib/node_modules/forever/lib/forever.js:460:14)
at /var/lib/openshift/56d88d2e0c1e661d250000a9/io-js/bin/iojs/lib/node_modules/forever/lib/forever/cli.js:315:15
at /var/lib/openshift/56d88d2e0c1e661d250000a9/io-js/bin/iojs/lib/node_modules/forever/lib/forever/cli.js:161:5
at /var/lib/openshift/56d88d2e0c1e661d250000a9/io-js/bin/iojs/lib/node_modules/forever/lib/forever.js:412:11
at FSReqWrap.oncomplete (fs.js:82:15)
[trading-kurryworst003.rhcloud.com repo]\>
if i dont cd to app-root/runtime/repo
and i use forever start automatic.js i get:
[trading-kurryworst003.rhcloud.com 56d88d2e0c1e661d250000a9]\> forever start automatic.js
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
info: Forever processing file: automatic.js
error: Cannot start forever
error: script /var/lib/openshift/56d88d2e0c1e661d250000a9/automatic.js does not exist.
also tryed screen but doesnt work

foreverjs turns process in STOPPED state instead of restarting

I have nodejs process that listens for an connection. I want to let foreverjs restart it on any connection error this way:
amqpConnection.on('error', function (err) {
console.error(err);
process.exit(1);
});
But this don't work. After error I see my process in this state:
~> forever list
info: Forever processes running
data: uid command script logfile
data: [0] OpQF /usr/bin/node my-script.js my-script-log.log STOPPED
Log file has connection error and two attempts to restart script.
error: Script restart attempt #2
{ [Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect' }
error: Forever detected script exited with code: 1
Why forever stops trying to restart my process?
I should have read warnings that forever prints!
Script that not stay running more than --minUptime (1000ms by default) considered "spinning". One should set --spinSleepTime to make spinning scripts to be restarted. When this parameter is omitted spinning script won't be restarted.
I have reproduced your problem and found that forever successfully restart your process if some delay (1sec) take place before process.exit.
It's look that forever don't restart process when it crash too fast.
setTimeout(function() {
process.exit(1);
}, 1000);

Resources