PM2 Managing PHP Script - Cluster mode - javascript

Currently i'm successfully managing some PHP deamons (single instance) with PM2, and, so far so good!
With PM2 and while managing Node.js/IO.js apps, i can launch them in cluster mode on PM2 with no stress! The same does not happen with the mentioned php deamons!
So, first of all, is it even possible to manage/launch clustered instances of a PHP script using PM2 (or is that PM2 feature only usable on Node.js/IO.js scripts)?
If so (possible to do), what special "tuneups" are required to cluster the PHP scripts with PM2!?
While trying to launch some pm2 start xxx.php -i 2 --name XXX, i get the following error # the error log path:
SyntaxError: Unexpected token <
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function._load (/usr/local/lib/node_modules/pm2/node_modules/pmx/lib/transaction.js:62:21)
at /usr/local/lib/node_modules/pm2/lib/ProcessContainer.js:200:23
at /usr/local/lib/node_modules/pm2/node_modules/async/lib/async.js:52:16
at /usr/local/lib/node_modules/pm2/node_modules/async/lib/async.js:1209:30
at WriteStream.<anonymous> (/usr/local/lib/node_modules/pm2/lib/Utility.js:126:13)
at WriteStream.EventEmitter.emit (events.js:95:17)
... that makes me believe that it can't even parse the given PHP file!
Thanks in advance for any help on this!

Short answer: you can't use cluster mode with php.
The pm2 cluster mode only works with javascript. Internally it's using the nodejs cluster module and requiring your script (code line).
This also explains your error, pm2 tries to execute the php code with javascript (hence the SyntaxError).
When using fork mode, it's using your interpreter (here php) to launch the script. This is why you can also run python or ruby on top of nodejs!

Related

Beginning JavaScript question using the Eclipse IDE

I'm a beginner at JavaScript so go easy on me here. (This is not supposed to be Java. I know they're different)
So I'm trying to run my first script in the Eclipse IDE and the code is just:
document.write("<h1>Hello World<h1>");
But my console says:
<terminated> js Proj 2-ffirst.js [Node Application] Node.js Process
at Object.<anonymous> (D:\Javascript\js Proj 2\ffirst.js:1:1)
at Module._compile (node:internal/modules/cjs/loader:1092:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
at Module.load (node:internal/modules/cjs/loader:972:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
at node:internal/main/run_main_module:17:47
I suspect my issue is in how I set up the software but I haven't learned how to use this yet really so I'm open to any suggestions you have. Thanks!
you are running a node app - as node is on the server it doesn't know what the document and window are (they are browser api's only accessible on the client side)
Because they don't know what the document Object is you get an error.
Try either console.log to print a response in the terminal from your node server or move to something like VS code to write a plain .js file that can be launched through the browser.

Grave (`) is marked as illegal token

I've been trying to transfer a node program I created locally to an EC2 server via Cloud9, and I have already manually replaced all of the (req,res) => {} statements because it didn't accept that. However, now it's giving me
console.log(`foo ${bar}`)
^
SyntaxError: Unexpected token ILLEGAL
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:945:3
It works fine on my local server and it runs fine here too, even without nodejs.
var bar = "bar"
console.log(`foo ${bar}`)
The version of node is v13.9.0 and it was installed via nvp.
Anyone know why this is happening? I really don't want to go through and convert all the strings with `
Thanks for the help, I figured out that cloud9 automatically installs nodejs, just not the right version. It installs 10.19.0, while the current latest version is 13.10.0. I installed the latest version, but the default was still 10.19.0. For anyone else having this problem, do
nvm ls
to list your installed node versions, then use
nvm alias default 13.10.0 (replace with whatever version you want)

I get a SyntaxError in Node.JS, but I can't figure out the origin of that error from the log messages

I am running a Node app on Azure. It's a Web API and when I try to call it, I see this message in the Node console:
Application has thrown an uncaught exception and is terminated:
SyntaxError: Unexpected token (
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:414:25)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (D:\home\site\wwwroot\app.js:17:23)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
I ran my whole code through eslint and didn't find any syntax errors. It also runs fine on my local machine, but doesn't run on Azure.
The message above points to line 17 and column 23 in app.js, which is a require:
let warehouseExport = require('./routes/warehouse-export-api');
I ran eslint and jshint specifically on the file referenced above, and there are no syntax errors.
What are some other ways I can get the exact location of the syntax error, and why could it be that the code fails on Azure but runs on my local machine?
What I tried:
Wrap in try-catch and grab the line number - it shows up undefined:
try {
var warehouseExport = require('./routes/warehouse-export-api');
} catch (error) {
console.log(error + ', line ' + error.lineNumber);
}
You should check version of nodejs you have installed on Azure. Maybe it's <= 4 and it doesn't support let keyword. Just a wild guess.
The cause of the error was that the Node version somehow got reset to 4.2.4.
When I go to Azure Portal and run node --version, I get version 8.1.4.
However, if I console.log the Node version inside my script, it is somehow still 4.2.4.
I will investigate why that happened and how to fix it, but for now it's clear that the bug is due to async function not being recognized by the old Node.
Update: the package.json file where the node version was specified got overwritten somehow, which is what caused the issue.

Arrow function from meteor build is crashing heroku deploy

I have deployed a js application on heroku that uses meteor. The build works on localhost but fails on the heroku server. I checked the logs and found this:
.js:312:12)
2017-03-18T03:29:07.070711+00:00 app[web.1]: const unicodeNormalizePath = (path) => {
2017-03-18T03:29:07.074825+00:00 app[web.1]: at Module.require (module.js:364:17)
2017-03-18T03:29:07.074821+00:00 app[web.1]: SyntaxError: Unexpected token >
2017-03-18T03:29:07.074824+00:00 app[web.1]: at Module.load (module.js:356:32)
2017-03-18T03:29:07.070741+00:00 app[web.1]: ^
This gave me the thought that it was not compatible with ES6 arrow functions so I installed ecmascript for meteor to make it compatible with ES5.
This has not solved the problem and this arrow function is still crashing the web app.
Any suggestions on how to stop this?
I had the same problem using, probably, an old buildpack for Heroku. I switched to the buildpack https://github.com/AdmitHub/meteor-buildpack-horse.git and it solved the problem perfectly.
Working buildpack:
heroku buildpacks:set https://github.com/AdmitHub/meteor-buildpack-horse.git
I also disabled the previous buildpack by typing:
heroku config:unset LINK_TO_OLD_BUILDPACK_REPO
The old one I used was https://github.com/jordansissel/heroku-buildpack-meteor.git
May be you can use babel to transform es6 to es5

Playing a local file on RPi with Node.js

I have a Raspberry Pi set up with a Node.js app that responds when it sees button push from an Amazon Dash Button. It was originally supposed to be a silent doorbell from https://github.com/initialstate/silent-doorbell, but I would like to just have it play a local sound file. Which I think should be easy enough, but my inexperience with coding leaves me just trying new stuff that I find all over the internet.
I can play the file from the terminal with the following and it plays just fine:
$ omxplayer example.mp3
But, no matter how I try to put it in the Node.js app and trigger when the Dash Button is pressed it won't work.
var dash_button = require('node-dash-button'),
dash = dash_button('XX:XX:XX:XX:XX:XX'), //REPLACE WITH YOUR ADDRESS
exec = require('child_process').exec;
Omx = require('node-omxplayer');
player = Omx('~/node_modules/node-dash-button/example.mp3');
let spawn = require('child_process').spawn;
dash.on('detected', function() {
console.log('Button pushed!');
player.play();
});
When run with my latest as above, I get this:
/home/pi/node_modules/node-dash-button/doorbell.js:7
let spawn = require('child_process').spawn;
^^^^^
SyntaxError: Unexpected identifier
at exports.runInThisContext (vm.js:73:16)
at Module._compile (module.js:443:25)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
After upgrading Node.js to the newest version as suggested by #Quentin using the Major Version Upgrade directions on this site http://thisdavej.com/upgrading-to-more-recent-versions-of-node-js-on-the-raspberry-pi/ I was able to get past this. Now I can't get past how to properly use omxplayer. When running the same code as above after the Node.js upgrade I now get this error after pressing the Amazon Dash button which then crashes the app:
pi#raspberrypi:~/node_modules/node-dash-button $ sudo node doorbell.js
Button pushed!
/home/pi/node_modules/node-omxplayer/index.js:103
throw new Error('Player is closed.');
^
Error: Player is closed.
at writeStdin (/home/pi/node_modules/node-omxplayer/index.js:103:10)
at EventEmitter.Omx.omxplayer.play (/home/pi/node_modules/node-omxplayer/index.js:133:27)
at Readable.<anonymous> (/home/pi/node_modules/node-dash-button/doorbell.js:13:12)
at emitOne (events.js:96:13)
at Readable.emit (events.js:188:7)
at PcapSession.<anonymous> (/home/pi/node_modules/node-dash-button/index.js:87:28)
at emitOne (events.js:96:13)
at PcapSession.emit (events.js:188:7)
at PcapSession.on_packet_ready (/home/pi/node_modules/node-dash-button/node_modules/pcap/pcap.js:99:10)
at packet_ready (/home/pi/node_modules/node-dash-button/node_modules/pcap/pcap.js:44:14)
I tried a few different things to try and get the player to spawn with no luck. The index.js file referenced mentions to use the player.running command but I still get the player is closed error when attempting to use this.
You are using a version of Node older than the 4.x series.
Consequently it sees let as an identifier rather than the keyword, so it doesn't expect it to be immediately followed by another identifier (spawn).
Upgrade your installation of Node to a current version.
Alternatively, use a different variable declaration, such as var.

Categories