@@ -1022,37 +1022,53 @@ module.exports = NodeHelper.create({
10221022 } ,
10231023
10241024 controlPm2 ( res , query ) {
1025- try { require ( "pm2" ) ; } catch ( err ) {
1026- this . sendResponse ( res , err , { reason : "PM2 not installed or unlinked" } ) ;
1025+ const actionName = query . action . toLowerCase ( ) ;
1026+
1027+ // Check if PM2 is available
1028+ try {
1029+ require . resolve ( "pm2" ) ;
1030+ } catch {
1031+ // PM2 not installed
1032+ const message = `MagicMirror² is not running under PM2. Please ${ actionName } manually.` ;
1033+ Log . log ( `[MMM-Remote-Control] ${ message } ` ) ;
1034+ this . sendResponse ( res , undefined , { action : actionName , info : message , status : "info" } ) ;
10271035 return ;
10281036 }
1037+
10291038 const pm2 = require ( "pm2" ) ;
10301039 const processName = query . processName || this . thisConfig . pm2ProcessName || "mm" ;
10311040
10321041 pm2 . connect ( ( err ) => {
10331042 if ( err ) {
1034- this . sendResponse ( res , err ) ;
1043+ pm2 . disconnect ( ) ;
1044+ const message = `MagicMirror² is not running under PM2. Please ${ actionName } manually.` ;
1045+ Log . log ( `[MMM-Remote-Control] ${ message } ` ) ;
1046+ this . sendResponse ( res , undefined , { action : actionName , info : message , status : "info" } ) ;
10351047 return ;
10361048 }
10371049
1038- const actionName = query . action . toLowerCase ( ) ;
1039- Log . log ( `[MMM-Remote-Control] PM2 process: ${ actionName } ${ processName } ` ) ;
1050+ // Check if process is running in PM2
1051+ pm2 . list ( ( err , list ) => {
1052+ if ( err || ! list . find ( ( proc ) => proc . name === processName && proc . pm2_env . status === "online" ) ) {
1053+ pm2 . disconnect ( ) ;
1054+ const message = `MagicMirror² is not running under PM2. Please ${ actionName } manually.` ;
1055+ Log . log ( `[MMM-Remote-Control] ${ message } ` ) ;
1056+ this . sendResponse ( res , undefined , { action : actionName , info : message , status : "info" } ) ;
1057+ return ;
1058+ }
10401059
1041- switch ( actionName ) {
1042- case "restart" :
1043- pm2 . restart ( processName , ( err ) => {
1044- this . sendResponse ( res , undefined , { action : actionName , processName} ) ;
1045- if ( err ) { this . sendResponse ( res , err ) ; }
1046- } ) ;
1047- break ;
1048- case "stop" :
1049- pm2 . stop ( processName , ( err ) => {
1060+ // Process is running in PM2, perform action
1061+ pm2 [ actionName ] ( processName , ( err ) => {
1062+ pm2 . disconnect ( ) ;
1063+ if ( err ) {
1064+ Log . error ( `[MMM-Remote-Control] PM2 ${ actionName } error:` , err ) ;
1065+ this . sendResponse ( res , err ) ;
1066+ } else {
1067+ Log . log ( `[MMM-Remote-Control] PM2 ${ actionName } : ${ processName } ` ) ;
10501068 this . sendResponse ( res , undefined , { action : actionName , processName} ) ;
1051- pm2 . disconnect ( ) ;
1052- if ( err ) { this . sendResponse ( res , err ) ; }
1053- } ) ;
1054- break ;
1055- }
1069+ }
1070+ } ) ;
1071+ } ) ;
10561072 } ) ;
10571073 } ,
10581074
0 commit comments