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

updatenotification: update_helper.js recode with pm2 library (v2.27.x) #3332

Merged
merged 9 commits into from
Jan 20, 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: 2 additions & 0 deletions .github/workflows/depsreview.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ jobs:
uses: actions/checkout@v4
- name: "Dependency Review"
uses: actions/dependency-review-action@v3
with:
allow-ghsas: GHSA-wf5p-g6vw-rhxx
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ _This release is scheduled to be released on 2024-04-01._

### Updated

- Update updatenotification (update_helper.js): Recode with pm2 library (#3332)
- Removing lodash dependency by replacing merge by spread operator (#3339)
- Use node prefix for build-in modules (#3340)
- Rework logging colors (#3350)
Expand Down
53 changes: 18 additions & 35 deletions modules/default/updatenotification/update_helper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Exec = require("node:child_process").exec;
const Spawn = require("node:child_process").spawn;
const commandExists = require("command-exists");
const pm2 = require("pm2");

const Log = require("logger");

/* class Updater
Expand Down Expand Up @@ -138,7 +139,7 @@ class Updater {
// restart MagicMiror with "pm2"
pm2Restart () {
Log.info("updatenotification: PM2 will restarting MagicMirror...");
Exec(`pm2 restart ${this.PM2}`, (err, std, sde) => {
pm2.restart(this.PM2, (err, proc) => {
if (err) {
Log.error("updatenotification:[PM2] restart Error", err);
}
Expand All @@ -159,53 +160,35 @@ class Updater {
check_PM2_Process () {
Log.info("updatenotification: Checking PM2 using...");
return new Promise((resolve) => {
commandExists("pm2")
.then(async () => {
var PM2_List = await this.PM2_GetList();
if (!PM2_List) {
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;
}
PM2_List.forEach((pm) => {
if (pm.pm2_env.version === this.version && pm.pm2_env.status === "online" && pm.pm2_env.PWD.includes(this.root_path)) {
list.forEach((pm) => {
if (pm.pm2_env.version === this.version && pm.pm2_env.status === "online" && pm.pm2_env.pm_cwd.includes(`${this.root_path}/`)) {
this.PM2 = pm.name;
this.usePM2 = true;
Log.info("updatenotification: You are using pm2 with", this.PM2);
Log.info("updatenotification: [PM2] You are using pm2 with", this.PM2);
resolve(true);
}
});
pm2.disconnect();
if (!this.PM2) {
Log.info("updatenotification: You are not using pm2");
Log.info("updatenotification: [PM2] You are not using pm2");
this.usePM2 = false;
resolve(false);
}
})
.catch(() => {
Log.info("updatenotification: You are not using pm2");
this.usePM2 = false;
resolve(false);
});
});
}

// Get the list of pm2 process
PM2_GetList () {
return new Promise((resolve) => {
Exec("pm2 jlist", (err, std, sde) => {
if (err) {
resolve(null);
return;
}
try {
let result = JSON.parse(std);
resolve(result);
} catch (e) {
Log.error("updatenotification: [PM2] can't GetList!");
Log.debug("updatenotification: [PM2] GetList is not an JSON format", e);
resolve(null);
}
});
});
}
Expand All @@ -218,7 +201,7 @@ class Updater {

// search update module command
applyCommand (module) {
if (this.isMagicMirror(module.module)) return null;
if (this.isMagicMirror(module.module) || !this.updates.length) return null;
let command = null;
this.updates.forEach((updater) => {
if (updater[module]) command = updater[module];
Expand Down
2 changes: 1 addition & 1 deletion modules/default/updatenotification/updatenotification.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Module.register("updatenotification", {
suspended: false,
moduleList: {},
needRestart: false,
updates: {},
updates: [],

start () {
Log.info(`Starting module: ${this.name}`);
Expand Down
Loading