Skip to content

Commit

Permalink
updatenotification: some fixes (MagicMirrorOrg#3628)
Browse files Browse the repository at this point in the history
continue from MagicMirrorOrg#3626 

Is it ok for you ?
  • Loading branch information
bugsounet authored Nov 9, 2024
1 parent cd6f10c commit 4259d7c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
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) (#3626)
- [updatenotification] Fix pm2 using detection when pm2 script is inside or outside MagicMirror root folder (#3576) (#3605) (#3626) (#3628)
- [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
25 changes: 11 additions & 14 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");

const Log = require("logger");

/*
Expand Down Expand Up @@ -47,8 +47,8 @@ class Updater {
this.autoRestart = config.updateAutorestart;
this.moduleList = {};
this.updating = false;
this.usePM2 = false;
this.PM2 = null;
this.usePM2 = false; // don't use pm2 by default
this.PM2Id = null; // pm2 process number
this.version = global.version;
this.root_path = global.root_path;
Log.info("updatenotification: Updater Class Loaded!");
Expand Down Expand Up @@ -139,10 +139,11 @@ class Updater {
else this.npmRestart();
}

// restart MagicMiror with "pm2"
// restart MagicMiror with "pm2": use PM2Id for restart it
pm2Restart () {
Log.info("updatenotification: PM2 will restarting MagicMirror...");
pm2.restart(this.PM2, (err, proc) => {
const pm2 = require("pm2");
pm2.restart(this.PM2Id, (err, proc) => {
if (err) {
Log.error("updatenotification:[PM2] restart Error", err);
}
Expand All @@ -155,7 +156,7 @@ class Updater {
const out = process.stdout;
const err = process.stderr;
const subprocess = Spawn("npm start", { cwd: this.root_path, shell: true, detached: true, stdio: ["ignore", out, err] });
subprocess.unref();
subprocess.unref(); // detach the newly launched process from the master process
process.exit();
}

Expand All @@ -165,49 +166,45 @@ class Updater {
return new Promise((resolve) => {
if (fs.existsSync("/.dockerenv")) {
Log.info("updatenotification: Running in docker container, not using PM2 ...");
this.usePM2 = false;
resolve(false);
return;
}

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}`);

const pm2 = require("pm2");
pm2.connect((err) => {
if (err) {
Log.error("updatenotification: [PM2]", err);
this.usePM2 = false;
resolve(false);
return;
}
pm2.list((err, list) => {
if (err) {
Log.error("updatenotification: [PM2] Can't get process List!");
this.usePM2 = false;
resolve(false);
return;
}
list.forEach((pm) => {
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.PM2Id = pm.pm_id;
this.usePM2 = true;
Log.info(`updatenotification: [PM2] You are using pm2 with id: ${this.PM2} (${pm.name})`);
Log.info(`updatenotification: [PM2] You are using pm2 with id: ${this.PM2Id} (${pm.name})`);
resolve(true);
} else {
Log.debug(`updatenotification: [PM2] pm2 process id: ${pm.pm_id} don't match...`);
}
});
pm2.disconnect();
if (!this.PM2) {
if (!this.usePM2) {
Log.info("updatenotification: [PM2] You are not using pm2");
this.usePM2 = false;
resolve(false);
}
});
Expand Down

0 comments on commit 4259d7c

Please sign in to comment.