Skip to content

Commit 8e0cf0c

Browse files
committed
wip
1 parent 8fe5383 commit 8e0cf0c

1 file changed

Lines changed: 77 additions & 68 deletions

File tree

nodeapp.php

Lines changed: 77 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,77 @@ public function __construct() {
7777
$hcpp->add_custom_page( 'nodeapplog', __DIR__ . '/pages/nodeapplog.php' );
7878
}
7979

80+
/**
81+
* Do maintenance will shutdown all running apps, across all users that are
82+
* using the specified major version of NodeJS and invoke the given callback.
83+
* Lastly, after the callback has returned, the apps will be restarted.
84+
*
85+
* @param array $majors The list of major versions to perform maintenance on; leave empty to shutdown all running apps.
86+
* @param callable $cb_maintenance The callback to invoke after all apps have been shutdown, a [user=>apps] array will be passed of stopped apps.
87+
*/
88+
public function do_maintenance( $majors = [], $cb_maintenance = null ) {
89+
90+
// Check if majors array is empty and get all majors as default
91+
global $hcpp;
92+
$hcpp->log( 'nodeapp do_maintenance' );
93+
$hcpp->log( $majors );
94+
95+
if ( count( $majors ) == 0 ) {
96+
$versions = $this->get_versions();
97+
foreach( $versions as $v ) {
98+
$majors[] = $hcpp->getLeftMost( $v['installed'], '.' );
99+
}
100+
}
101+
102+
// Get user list of PM2 (bash) capable users
103+
global $hcpp;
104+
$all = $hcpp->run( 'v-list-users json' );
105+
$users = [];
106+
foreach( $all as $user => $details ) {
107+
if ( $details['SHELL'] == 'bash' ) {
108+
$users[] = $user;
109+
}
110+
}
111+
112+
// Find any running apps using the major version across all users
113+
$pm2_list = [];
114+
foreach( $users as $user) {
115+
$pm2 = $this->get_pm2_list( $user );
116+
foreach( $pm2 as $p ) {
117+
if ( $p['pm2_env']['status'] == 'online' ) {
118+
$version = $p['pm2_env']['exec_interpreter'];
119+
$version = $hcpp->getRightMost( $version, '/v' );
120+
$version = $hcpp->getLeftMost( $version, '/' );
121+
$major = $hcpp->getLeftMost( $version, '.' );
122+
if ( in_array( $major, $majors ) ) {
123+
$pm2_list[$user][] = $p['pm_id'];
124+
}
125+
}
126+
}
127+
}
128+
$pm2_list = $hcpp->do_action( 'nodeapp_maintenance_start', $pm2_list );
129+
130+
// Shutdown all apps by id for each user
131+
foreach( $pm2_list as $user => $app_ids ) {
132+
if ( count( $app_ids ) > 0 ) {
133+
$this->stop_pm2_ids( $app_ids, $user );
134+
}
135+
}
136+
137+
// Invoke the maintenance callback
138+
if ( is_callable( $cb_maintenance ) ) {
139+
$cb_maintenance( $pm2_list );
140+
}
141+
142+
// Restart the stopped apps by id for each user
143+
foreach( $pm2_list as $user => $app_ids ) {
144+
if ( count( $app_ids ) > 0 ) {
145+
$this->restart_pm2_ids( $app_ids, $user );
146+
}
147+
}
148+
$pm2_list = $hcpp->do_action( 'nodeapp_maintenance_end', $pm2_list );
149+
}
150+
80151
/**
81152
* Throw the nodeapp_nginx_modified event to allow other plugins to modify the nginx config files
82153
*/
@@ -325,9 +396,11 @@ public function hcpp_add_webapp_xpath( $xpath ) {
325396
*/
326397
public function hcpp_invoke_plugin( $args ) {
327398
global $hcpp;
399+
if ( count( $args ) < 2 ) return $args;
328400
$username = preg_replace( "/[^a-zA-Z0-9-_]+/", "", $args[1] ); // Sanitized username
329401
switch ( $args[0] ) {
330402
case 'nodeapp_get_versions':
403+
$hcpp->log( 'nodeapp_get_versions' );
331404
echo json_encode( $this->get_versions() );
332405
break;
333406
case 'nodeapp_pm2_jlist':
@@ -591,6 +664,8 @@ public function restart_pm2_ids( $pm2_ids, $user = '""' ) {
591664
}
592665
}
593666
global $hcpp;
667+
$hcpp->log( 'nodeapp restart_pm2_ids for user: ' . $user );
668+
$hcpp->log( $pm2_ids );
594669
$pm2_ids = escapeshellarg( json_encode( $pm2_ids ) );
595670
$hcpp->run("v-invoke-plugin nodeapp_restart_pm2_ids " . $user . ' ' . $pm2_ids );
596671
}
@@ -646,6 +721,8 @@ public function stop_pm2_ids( $pm2_ids, $user = '""' ) {
646721
}
647722
}
648723
global $hcpp;
724+
$hcpp->log( 'nodeapp stop_pm2_ids for user: ' . $user );
725+
$hcpp->log( $pm2_ids );
649726
$pm2_ids = escapeshellarg( json_encode( $pm2_ids ) );
650727
$hcpp->run("v-invoke-plugin nodeapp_stop_pm2_ids " . $user . ' ' . $pm2_ids );
651728
}
@@ -726,74 +803,6 @@ public function update_all() {
726803
});
727804
}
728805

729-
/**
730-
* Do maintenance will shutdown all running apps, across all users that are
731-
* using the specified major version of NodeJS and invoke the given callback.
732-
* Lastly, after the callback has returned, the apps will be restarted.
733-
*
734-
* @param array $majors The list of major versions to perform maintenance on; leave empty to shutdown all running apps.
735-
* @param callable $cb_maintenance The callback to invoke after all apps have been shutdown, a [user=>apps] array will be passed of stopped apps.
736-
*/
737-
public function do_maintenance( $majors = [], $cb_maintenance = null ) {
738-
739-
// Check if majors array is empty and get all majors as default
740-
global $hcpp;
741-
if ( count( $majors ) == 0 ) {
742-
$versions = $this->get_versions();
743-
foreach( $versions as $v ) {
744-
$majors[] = $hcpp->getLeftMost( $v['installed'], '.' );
745-
}
746-
}
747-
748-
// Get user list of PM2 (bash) capable users
749-
global $hcpp;
750-
$all = $hcpp->run( 'v-list-users json' );
751-
$users = [];
752-
foreach( $all as $user => $details ) {
753-
if ( $details['SHELL'] == 'bash' ) {
754-
$users[] = $user;
755-
}
756-
}
757-
758-
// Find any running apps using the major version across all users
759-
$pm2_list = [];
760-
foreach( $users as $user) {
761-
$pm2 = $this->get_pm2_list( $user );
762-
foreach( $pm2 as $p ) {
763-
if ( $p['pm2_env']['status'] == 'online' ) {
764-
$version = $p['pm2_env']['exec_interpreter'];
765-
$version = $hcpp->getRightMost( $version, '/v' );
766-
$version = $hcpp->getLeftMost( $version, '/' );
767-
$major = $hcpp->getLeftMost( $version, '.' );
768-
if ( in_array( $major, $majors ) ) {
769-
$pm2_list[$user][] = $p['pm_id'];
770-
}
771-
}
772-
}
773-
}
774-
$pm2_list = $hcpp->do_action( 'nodeapp_maintenance_start', $pm2_list );
775-
776-
// Shutdown all apps by id for each user
777-
foreach( $pm2_list as $user => $app_ids ) {
778-
if ( count( $app_ids ) > 0 ) {
779-
$this->stop_pm2_ids( $app_ids, $user );
780-
}
781-
}
782-
783-
// Invoke the maintenance callback
784-
if ( is_callable( $cb_maintenance ) ) {
785-
$cb_maintenance( $pm2_list );
786-
}
787-
788-
// Restart the stopped apps by id for each user
789-
foreach( $pm2_list as $user => $app_ids ) {
790-
if ( count( $app_ids ) > 0 ) {
791-
$this->restart_pm2_ids( $app_ids, $user );
792-
}
793-
}
794-
$pm2_list = $hcpp->do_action( 'nodeapp_maintenance_end', $pm2_list );
795-
}
796-
797806
/**
798807
* On proxy template change, copy basic nodeapp, allocate ports, and start apps
799808
*/

0 commit comments

Comments
 (0)