gabriele-v/hubitat

Documentation notes for AlexaCookieNode.js - issues with old nodejs/npm versions?

Opened this issue · 2 comments

Since many people may be doing this on a Raspberry Pi, and the nodejs is very old (version 8 at this time), and so is the npm in the archive.raspberrypi.org here are some things to consider:
If you get an error with the npm install -g pm2

  1. run sudo npm install -g npm@latest # this will update npm (with some warnings)
  2. run sudo npm install -g pm2 # this will allow you to change files in shared areas.

Between the pm2 startup and the pm2 save commands you might need to run:
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u pi --hp /home/pi

Still a work in progress for me, since pm2 is restarting AlexaCookie every couple of seconds (based on pid changes in pm2 status). However I wanted to capture the troubleshooting so far...

Also had to change the package.json to read:
"dependencies": {
"alexa-cookie2": "https://github.com/Apollon77/alexa-cookie",

rather than:
github:Appolon77/alexa-cookie.

Still no luck in getting server to run... possibly need newer version of nodejs? Still investigating.

and I'm guessing that the problem is that the pm2 commands to start the server need to be sudo pm2 ...

Since this server config is running on a low port (<1024), the normal pi user can't do that...

Hopefully these notes will help others in getting their configs sorted out.

Another way to avoid the need to run as root/sudo would be to change the ports from 81 & 82 to something above the 1024 boundary. 8081 & 8082 would work. Recommend default config and documentation go to values above 1024.

More thoughts on documentation / troubleshooting. Not sure how much of this needs to go in the documentation, but I thought I should capture this so that it could be referenced by others who are just starting on their install work...

These apply to raspbian (/debian) running systemd, but any system that uses systemd version 229 or greater can follow this method. It assumes you have sudo rights.

Handy references:
https://pm2.keymetrics.io/docs/usage/startup/
https://www.freedesktop.org/software/systemd/man/systemd.service.html#RuntimeMaxSec=

Run systemctl --version to check the version. If systemctl does not exist, or it is less than 229, then you can't use the parts about modifying the service file. Some of the other information about troubleshooting may be of value.

After running the previously documented pm2 startup command, followed by the subsequent sudo ... command, a service file will be created. One place that file might live is: /etc/systemd/system/pm2-pi.service.

Run the pm2 save command to synchronize the status.

Run pm2 status and see if AlexaCookie is running. Run the same command again after about 15 seconds - if the pm2 process number (4th column) is changing, then that means there's a problem with the AlexaCookie.js and it is dying and restarting. Check if something else is running on the ports assigned in the configuration file (sudo netstat -anpt will help). Troubleshoot until it is running sanely.

Tips:

  • If something else is running on the port, use the PID provided at the end of the netstat command to investigate (the -p option is what generates that info).
  • Do a sanity check by running sudo pm2 status to see if you might have started AlexaCookie while running as root. You shouldn't have, but who knows what mistakes might be made while trying this out :-). If it is running that way (as the root user), run the following to clear up that issue. Don't ask why I happen to know this ...
sudo pm2 delete AlexaCookie
sudo pm2 unstartup

Once it is running sanely, then stop it. Yep counter-intuitive, but that allows solving one class of problem before possibly introducing another. Stop it by running pm2 stop AlexaCookie.

Edit the pm2-pi.service file with sudo vi, or sudo nano, or your editor of choice - just has to be edited with root permissions. In the '[Service]' section, change the Restart line from 'On-Failure' to 'always', and add a RuntimeMaxSec. This will cause the automatic restart at the interval specified. (I find it better to define this as part of the service than to have to have a separate cron job for restarts. This approach does a full restart of the pm2 process, rather than leaving pm2 running, and having just the AlexaCookie process restarted. Choice is up to you... )

[Service]
... other stuff here ...
Restart=always
RuntimeMaxSec=86400

Save and exit.

Run sudo systemctl daemon-reload to let the system know that the service file has been changed on disk.

Run sudo systemctl restart pm2-pi

Check pm2 status a few times to ensure pm2 process number remains constant.
Check ps aux | grep AlexaCookie a few times to ensure PID remains constant.

If you want to verify that the restart happens every MaxSec, you can go back a few steps, edit the value to something like 60, repeat the steps and watch to see if the restart is happening every 60 seconds. Because the restart is a clean one, the 4th column in the pm2 status report should remain as 0, however the PID from the ps aux will change, because a new node process is launched.

If you see a message Spawning PM2 daemon while running the pm status, it is likely you are catching it right when the systemctl is doing its restart of the process.

Then of course go back one more time and set it to a larger value.