diff --git a/src/helper/class-ee-site.php b/src/helper/class-ee-site.php index 738b70f7..f75eb279 100644 --- a/src/helper/class-ee-site.php +++ b/src/helper/class-ee-site.php @@ -506,16 +506,23 @@ public function restart( $args, $assoc_args, $whitelisted_containers = [] ) { $this->site_data = get_site_info( $args ); - chdir( $this->site_data['site_fs_path'] ); - if ( $all || $no_service_specified ) { $containers = $whitelisted_containers; } else { $containers = array_keys( $assoc_args ); } - foreach ( $containers as $container ) { - \EE\Site\Utils\run_compose_command( 'restart', $container ); + if ( in_array( 'nginx', $containers ) ) { + $nginx_test_command = "sh -c 'nginx -t'"; + if ( ! \EE_DOCKER::docker_compose_exec( $this->site_data['site_fs_path'], 'nginx', $nginx_test_command ) ) { + throw new \Exception( 'There was some error in docker-compose exec.' ); + } + } + + $all_containers = is_array( $containers ) ? implode( ' ', $containers ) : $containers; + + if ( ! \EE_DOCKER::docker_compose_restart( $this->site_data['site_fs_path'], $all_containers ) ) { + throw new \Exception( 'There was some error in docker-compose restart.' ); } \EE\Utils\delem_log( 'site restart stop' ); } @@ -625,36 +632,28 @@ public function reload( $args, $assoc_args, $whitelisted_containers = [], $reloa \EE\Utils\delem_log( 'site reload start' ); $args = auto_site_name( $args, 'site', __FUNCTION__ ); $all = \EE\Utils\get_flag_value( $assoc_args, 'all' ); - if ( ! array_key_exists( 'nginx', $reload_commands ) ) { - $reload_commands['nginx'] = 'nginx sh -c \'nginx -t && service openresty reload\''; - } + $reload_commands['php'] = "bash -c 'kill -USR2 1'"; + $reload_commands['nginx'] = "sh -c 'nginx -t && service openresty reload'"; $no_service_specified = count( $assoc_args ) === 0; $this->site_data = get_site_info( $args ); - chdir( $this->site_data['site_fs_path'] ); - if ( $all || $no_service_specified ) { - $this->reload_services( $whitelisted_containers, $reload_commands ); + foreach ( $whitelisted_containers as $container ) { + if ( ! \EE_DOCKER::docker_compose_exec( $this->site_data['site_fs_path'], $container, $reload_commands[ $container ] ) ) { + throw new \Exception( 'There was some error in docker-compose exec.' ); + } + } } else { - $this->reload_services( array_keys( $assoc_args ), $reload_commands ); + foreach ( array_keys( $assoc_args ) as $container ) { + if ( ! \EE_DOCKER::docker_compose_exec( $this->site_data['site_fs_path'], $container, $reload_commands[ $container ] ) ) { + throw new \Exception( 'There was some error in docker-compose exec.' ); + } + } } \EE\Utils\delem_log( 'site reload stop' ); } - /** - * Executes reload commands. It needs separate handling as commands to reload each service is different. - * - * @param array $services Services to reload. - * @param array $reload_commands Commands to reload the services. - */ - private function reload_services( $services, $reload_commands ) { - - foreach ( $services as $service ) { - \EE\Site\Utils\run_compose_command( 'exec', $reload_commands[ $service ], 'reload', $service ); - } - } - /** * Function to verify and check the global services dependent for given site. * Enables the dependent service if it is down. diff --git a/src/helper/site-utils.php b/src/helper/site-utils.php index 77c02b21..5b2c0b1c 100644 --- a/src/helper/site-utils.php +++ b/src/helper/site-utils.php @@ -420,7 +420,6 @@ function site_status_check( $site_url ) { */ function start_site_containers( $site_fs_path, $containers = [] ) { - chdir( $site_fs_path ); EE::log( 'Starting site\'s services.' ); if ( ! \EE_DOCKER::docker_compose_up( $site_fs_path, $containers ) ) { throw new \Exception( 'There was some error in docker-compose up.' ); @@ -435,9 +434,11 @@ function start_site_containers( $site_fs_path, $containers = [] ) { */ function restart_site_containers( $site_fs_path, $containers ) { - chdir( $site_fs_path ); + EE::log( 'Restarting site\'s services.' ); $all_containers = is_array( $containers ) ? implode( ' ', $containers ) : $containers; - EE::exec( "docker-compose restart $all_containers" ); + if ( ! \EE_DOCKER::docker_compose_restart( $site_fs_path, $all_containers ) ) { + throw new \Exception( 'There was some error in docker-compose restart.' ); + } } /** @@ -448,27 +449,13 @@ function restart_site_containers( $site_fs_path, $containers ) { */ function stop_site_containers( $site_fs_path, $containers ) { - chdir( $site_fs_path ); + EE::log( 'Stopping and removing site\'s services.' ); $all_containers = is_array( $containers ) ? implode( ' ', $containers ) : $containers; - EE::exec( "docker-compose stop $all_containers" ); - EE::exec( "docker-compose rm -f $all_containers" ); -} - -/** - * Generic function to run a docker compose command. Must be ran inside correct directory. - * - * @param string $action docker-compose action to run. - * @param string $container The container on which action has to be run. - * @param string $action_to_display The action message to be displayed. - * @param string $service_to_display The service message to be displayed. - */ -function run_compose_command( $action, $container, $action_to_display = null, $service_to_display = null ) { - - $display_action = $action_to_display ? $action_to_display : $action; - $display_service = $service_to_display ? $service_to_display : $container; - - EE::log( ucfirst( $display_action ) . 'ing ' . $display_service ); - EE::exec( "docker-compose $action $container", true, true ); + $stopped = \EE_DOCKER::docker_compose_stop( $site_fs_path, $all_containers ); + $forcermd = \EE_DOCKER::docker_compose_forcerm( $site_fs_path, $all_containers ); + if ( ! (stopped && forcermd) ) { + throw new \Exception( 'There was some error in stopping and removing containers.' ); + } } /**