Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PM2 Fix (again): add pm2_env.unique_id checking and cleaning #3626

Merged
merged 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ _This release is scheduled to be released on 2025-01-01._

### Fixed

- [updatenotification] Fix pm2 using detection when pm2 script is inside or outside MagicMirror root folder (#3576) (#3605)
- [updatenotification] Fix pm2 using detection when pm2 script is inside or outside MagicMirror root folder (#3576) (#3605) (#3626)
- [core] Fix loading node_helper of modules: avoid black screen, display errors and continue loading with next module (#3578)
- [weather] Changed default value for weatherEndpoint of provider openweathermap to "/onecall" (#3574)
- [tests] Fix electron tests with mock dates, the mock on server side was missing (#3597)
Expand Down
24 changes: 16 additions & 8 deletions modules/default/updatenotification/update_helper.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const Exec = require("node:child_process").exec;
const Spawn = require("node:child_process").spawn;
const fs = require("node:fs");

const pm2 = require("pm2");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not happy with moving this to top level.

Problem: With this statement pm2 starts already process(es) in the background so people not using pm2 have this unneeded and unwanted processes running, e.g. in a docker container.

So this should only called after Checking PM2 using as before.

const Log = require("logger");

/*
Expand Down Expand Up @@ -142,7 +142,6 @@ class Updater {
// restart MagicMiror with "pm2"
pm2Restart () {
Log.info("updatenotification: PM2 will restarting MagicMirror...");
const pm2 = require("pm2");
pm2.restart(this.PM2, (err, proc) => {
if (err) {
Log.error("updatenotification:[PM2] restart Error", err);
Expand Down Expand Up @@ -171,7 +170,15 @@ class Updater {
return;
}

const pm2 = require("pm2");
if (process.env.unique_id === undefined) {
Log.info("updatenotification: [PM2] You are not using pm2");
this.usePM2 = false;
resolve(false);
return;
}

Log.debug(`updatenotification: [PM2] Search for pm2 id: ${process.env.pm_id} -- name: ${process.env.name} -- unique_id: ${process.env.unique_id}`);

pm2.connect((err) => {
if (err) {
Log.error("updatenotification: [PM2]", err);
Expand All @@ -187,13 +194,14 @@ class Updater {
return;
}
list.forEach((pm) => {
Log.debug(`[PM2] pm2 name: ${pm.name} -- in process env: ${process.env.name}`)
Log.debug(`[PM2] pm2 pm_id: ${pm.pm_id} -- in process env: ${process.env.pm_id}`)
if (pm.pm2_env.status === "online" && process.env.name === pm.name && +process.env.pm_id === +pm.pm_id) {
this.PM2 = pm.name;
Log.debug(`updatenotification: [PM2] found pm2 process id: ${pm.pm_id} -- name: ${pm.name} -- unique_id: ${pm.pm2_env.unique_id}`);
if (pm.pm2_env.status === "online" && process.env.name === pm.name && +process.env.pm_id === +pm.pm_id && process.env.unique_id === pm.pm2_env.unique_id) {
this.PM2 = pm.pm_id;
this.usePM2 = true;
Log.info("updatenotification: [PM2] You are using pm2 with", this.PM2);
Log.info(`updatenotification: [PM2] You are using pm2 with id: ${this.PM2} (${pm.name})`);
resolve(true);
} else {
Log.debug(`updatenotification: [PM2] pm2 process id: ${pm.pm_id} don't match...`);
}
});
pm2.disconnect();
Expand Down