11const Exec = require ( "node:child_process" ) . exec ;
22const Spawn = require ( "node:child_process" ) . spawn ;
33const fs = require ( "node:fs" ) ;
4- const pm2 = require ( "pm2" ) ;
4+
55const Log = require ( "logger" ) ;
66
77/*
@@ -47,8 +47,8 @@ class Updater {
4747 this . autoRestart = config . updateAutorestart ;
4848 this . moduleList = { } ;
4949 this . updating = false ;
50- this . usePM2 = false ;
51- this . PM2 = null ;
50+ this . usePM2 = false ; // don't use pm2 by default
51+ this . PM2Id = null ; // pm2 process number
5252 this . version = global . version ;
5353 this . root_path = global . root_path ;
5454 Log . info ( "updatenotification: Updater Class Loaded!" ) ;
@@ -139,10 +139,11 @@ class Updater {
139139 else this . npmRestart ( ) ;
140140 }
141141
142- // restart MagicMiror with "pm2"
142+ // restart MagicMiror with "pm2": use PM2Id for restart it
143143 pm2Restart ( ) {
144144 Log . info ( "updatenotification: PM2 will restarting MagicMirror..." ) ;
145- pm2 . restart ( this . PM2 , ( err , proc ) => {
145+ const pm2 = require ( "pm2" ) ;
146+ pm2 . restart ( this . PM2Id , ( err , proc ) => {
146147 if ( err ) {
147148 Log . error ( "updatenotification:[PM2] restart Error" , err ) ;
148149 }
@@ -155,7 +156,7 @@ class Updater {
155156 const out = process . stdout ;
156157 const err = process . stderr ;
157158 const subprocess = Spawn ( "npm start" , { cwd : this . root_path , shell : true , detached : true , stdio : [ "ignore" , out , err ] } ) ;
158- subprocess . unref ( ) ;
159+ subprocess . unref ( ) ; // detach the newly launched process from the master process
159160 process . exit ( ) ;
160161 }
161162
@@ -165,49 +166,45 @@ class Updater {
165166 return new Promise ( ( resolve ) => {
166167 if ( fs . existsSync ( "/.dockerenv" ) ) {
167168 Log . info ( "updatenotification: Running in docker container, not using PM2 ..." ) ;
168- this . usePM2 = false ;
169169 resolve ( false ) ;
170170 return ;
171171 }
172172
173173 if ( process . env . unique_id === undefined ) {
174174 Log . info ( "updatenotification: [PM2] You are not using pm2" ) ;
175- this . usePM2 = false ;
176175 resolve ( false ) ;
177176 return ;
178177 }
179178
180179 Log . debug ( `updatenotification: [PM2] Search for pm2 id: ${ process . env . pm_id } -- name: ${ process . env . name } -- unique_id: ${ process . env . unique_id } ` ) ;
181180
181+ const pm2 = require ( "pm2" ) ;
182182 pm2 . connect ( ( err ) => {
183183 if ( err ) {
184184 Log . error ( "updatenotification: [PM2]" , err ) ;
185- this . usePM2 = false ;
186185 resolve ( false ) ;
187186 return ;
188187 }
189188 pm2 . list ( ( err , list ) => {
190189 if ( err ) {
191190 Log . error ( "updatenotification: [PM2] Can't get process List!" ) ;
192- this . usePM2 = false ;
193191 resolve ( false ) ;
194192 return ;
195193 }
196194 list . forEach ( ( pm ) => {
197195 Log . debug ( `updatenotification: [PM2] found pm2 process id: ${ pm . pm_id } -- name: ${ pm . name } -- unique_id: ${ pm . pm2_env . unique_id } ` ) ;
198196 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 ) {
199- this . PM2 = pm . pm_id ;
197+ this . PM2Id = pm . pm_id ;
200198 this . usePM2 = true ;
201- Log . info ( `updatenotification: [PM2] You are using pm2 with id: ${ this . PM2 } (${ pm . name } )` ) ;
199+ Log . info ( `updatenotification: [PM2] You are using pm2 with id: ${ this . PM2Id } (${ pm . name } )` ) ;
202200 resolve ( true ) ;
203201 } else {
204202 Log . debug ( `updatenotification: [PM2] pm2 process id: ${ pm . pm_id } don't match...` ) ;
205203 }
206204 } ) ;
207205 pm2 . disconnect ( ) ;
208- if ( ! this . PM2 ) {
206+ if ( ! this . usePM2 ) {
209207 Log . info ( "updatenotification: [PM2] You are not using pm2" ) ;
210- this . usePM2 = false ;
211208 resolve ( false ) ;
212209 }
213210 } ) ;
0 commit comments