&1", $out, $ret);
// echo "cmd: {$cmd}
RETURN CODE: '{$ret}'
OUTPUT:\n" . implode("\n", $out). "";
if (0 !== $ret) {
// [PM2][ERROR] Script already launched, add -f option to force re-execution
$firstLine = reset($out);
if (0 === strpos($firstLine, '[PM2][ERROR] Script already launched')) {
$out = [ "[PM2][ERROR] Script already launched" ];
}
// throw new Exception("Nie uruchomiono zadania. " . implode(" ", $out));
} else {
// [PM2] Applying action restartProcessId on app [biall-backup-db](ids: [ 0 ])
// [PM2] [biall-backup-db](0) ✓
// [PM2] Process successfully started
$line0 = reset($out);
$line1 = next($out);
$line2 = next($out);
if (0 === strpos($line0, '[PM2] Applying action')
&& 0 === strpos($line1, '[PM2] [')
&& 0 === strpos($line2, '[PM2] Process successfully started')
) {
$out = [ "[PM2] Process successfully started" ];
return 0;
}
// [PM2] Starting /opt/local/pl.procesy5/async_jobs/2020-02-26/3/start.sh in fork_mode (1 instance) [PM2] Done.
if (0 === strpos($line0, '[PM2] Starting ')
&& 0 === strpos($line1, '[PM2] Done')
) {
$out = [ "[PM2] Process successfully started" ];
return 0;
}
// throw new AlertInfoException("Uruchomiono zadanie. " . implode(" ", $out));
}
return $ret;
}
static function runPm2Script($props, &$out) { // TODO: mv to AsyncJobs / @return bool $ret from exec
if (empty($props['jobName'])) throw new Exception("Missing jobName");
if (empty($props['path'])) throw new Exception("Missing path");
// cwd (string) “/var/www/” the directory from which your app will be launched
$jobName = $props['jobName'];
$outLogPath = "/tmp/p5-async-jobs--{$jobName}--out.log";
$errLogPath = "/tmp/p5-async-jobs--{$jobName}--err.log";
$path = $props['path'];
$args = trim(V::get('args', '', $props));
$cmd = implode(" ", [
"pm2 start '{$path}'",
(!empty($props['cwd'])) ? "--cwd=\"{$props['cwd']}\"" : "",
"--name '{$jobName}'",
"--no-autorestart",
"--output '{$outLogPath}'",
"--error '{$errLogPath}'",
"--time", // prefix time to log entry
(!empty($args)) ? "-- {$args}" : "",
]);
V::exec($cmd . " 2>&1", $out, $ret);
// echo "cmd: {$cmd}OUTPUT:\n" . implode("\n", $out). "";
if (0 !== $ret) {
// [PM2][ERROR] Script already launched, add -f option to force re-execution
$firstLine = reset($out);
if (0 === strpos($firstLine, '[PM2][ERROR] Script already launched')) {
$out = [ "[PM2][ERROR] Script already launched" ];
}
// throw new Exception("Nie uruchomiono backupu. " . implode(" ", $out));
} else {
// [PM2] Applying action restartProcessId on app [biall-backup-db](ids: [ 0 ])
// [PM2] [biall-backup-db](0) ✓
// [PM2] Process successfully started
$line0 = reset($out);
$line1 = next($out);
$line2 = next($out);
if (0 === strpos($line0, '[PM2] Applying action')
&& 0 === strpos($line1, '[PM2] [')
&& 0 === strpos($line2, '[PM2] Process successfully started')
) {
$out = [ "[PM2] Process successfully started" ];
}
// throw new AlertInfoException("Uruchomiono backup. " . implode(" ", $out));
}
return $ret;
}
static function isInstalled() {
// V::exec("/usr/local/bin/pm2 --version 2>&1", $out, $ret);
V::exec("/usr/local/bin/pm2 ping 2>&1", $out, $ret); // expected "{ msg: 'pong' }"
// echo UI::h('pre', [], "ret({$ret}):\n" . implode("\n", $out));
if ($ret === 0) {
// [PM2] Spawning PM2 daemon with pm2_home=/Library/WebServer/.pm2
// [PM2] PM2 Successfully daemonized
// { msg: 'pong' }
return true;
}
if ($ret !== 0) {
if (!file_exists(self::path_nodeJS())) throw new Exception("pm2 not installed");
// if [ ! -f "$node_path" ]; then
// echo "$node_path not exists"
// wget https://nodejs.org/dist/v12.15.0/node-v12.15.0.pkg
// sudo installer -verbose -pkg node-v12.15.0.pkg -target /
// fi
// # node -v # expected v12.15.0
if (!file_exists(self::path_pm2())) throw new Exception("pm2 not installed");
// npm install -g pm2
// sudo npm install -g pm2
// pm2 -version # expected 4.2.3
if (!file_exists(self::path_pm2UserWww())) throw new Exception("pm2 user folder not exists");
// FIX for Mac OS:
// $ sudo mkdir /Library/WebServer/.pm2/
// $ sudo chown _www /Library/WebServer/.pm2/
throw new Exception("Error pm2"); // unknown error
}
}
}