0

I am experiencing strange behavior of a process (Illuminate\Support\Facades\Process) running through Laravel 10 scheduler on a VPS and I really don't know how is this happening or how to fix this behavior.

Crontab takes care of starting the Laravel scheduler, the cron job looks like this:

* * * * * root cd /var/www/stage.betvia.sk/public_html/ && /usr/bin/php artisan schedule:run >> /var/www/stage.betvia.sk/logs/checkscheduler.txt 2 >&1

There is a command in the scheduler that executes a process - it's a node js command (I'm using node version 18.16.1)

$process = Process::forever()->run(implode(" ", $process_data['process']));
if (!$process->successful()) {
         $this->log($LOG_CHANNEL, 'error', 'Partial process was not successful - failed parsing.');
         $this->log($LOG_CHANNEL, 'info', json_encode($process->errorOutput()));
         continue;
}

If I type the command directly to the console on the VPS, it works as it should.

But if scheduler runs this command, the process fails right at the beginning with this error:

"\/var\/www\/stage.betvia.sk\/public_html\/node_modules\/jsdom\/lib\/api.js:140
referrer: req.getHeader(\"referer\") ?? undefined

SyntaxError: Unexpected token ?
at Module._compile (internal\/modules\/cjs\/loader.js:723:23)
at Object.Module._extensions..js (internal\/modules\/cjs\/loader.js:789:10)
at Module.load (internal\/modules\/cjs\/loader.js:653:32)
at tryModuleLoad (internal\/modules\/cjs\/loader.js:593:12)
at Function.Module._load (internal\/modules\/cjs\/loader.js:585:3)
at Module.require (internal\/modules\/cjs\/loader.js:692:17)
at require (internal\/modules\/cjs\/helpers.js:25:18)
at Object.<anonymous> (\/var\/www\/stage.betvia.sk\/public_html\/storage\/parsers\/parser_1\/parser_source.js:1:13)
at Module._compile (internal\/modules\/cjs\/loader.js:778:30)
at Object.Module._extensions..js (internal\/modules\/cjs\/loader.js:789:10)"

I'm not sure if this is important (probably is), but here is the beginning of the node JS file:

var jsdom = require("jsdom");
var JSDOM = jsdom.JSDOM;
var window = new JSDOM().window;
var document = window.document;
var navigator = require("navigator");
var jQuery = require("jquery")(window);
var glob_fcn;

It looks like it has a problem with the first line but not sure why, jsdom is installed

"jsdom": "^22.1.0",

I have already tried to remove the node_modules, reinstall node and google the issue but no success.

Can anyone explain why is the command working fine if I type it manually to the console but doesn't work when the scheduler executes it? Scheduler also executes some Python 3 commands but they work fine, I have problems just with the node JS ones.

Paulis
  • 13
  • 2
  • And what is the process you are trying to run? I am lost – matiaslauriti Jul 18 '23 at 13:34
  • @matiaslauriti The command that scheduler is executing is php artisan data:obtain And this command contains code where the process executes a command (the second code block in my post) and that command is node /var/www/stage.betvia.sk/public_html/storage/parsers/parser_1/parser_source.js . If I type the command php artisan data:obtain manually into the console it executes fine, but when scheduler executes it, I get the error – Paulis Jul 18 '23 at 13:38
  • Unexpected token is often a mismatch in node versions, check the node version on your hosting compared to how you would run the code in your dev environment – mrhn Jul 18 '23 at 13:42
  • @mrhn I am typing the command manually directly on the server (via console), the scheduler is running on the same server, there is only 1 node version – Paulis Jul 18 '23 at 13:46

1 Answers1

0

So in the end the proces (Illuminate\Support\Facades\Process) executed via scheduler used node js 10.x version and when I executed that command from the command line directly it used a node js 16.x version. For some reason when I listed all nide versions (nvm list), the 10.x wasn't even there. I uninstalled nodejs, npm, nvm, installed everything again and forced the nodejs version 16.10.0 to be installed (because when I let to be installed any version, it installed the 10.x for some reason) and then the scheduler used it too.

Paulis
  • 13
  • 2