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

version 1.0.16 was released #38

Merged
merged 3 commits into from
Nov 3, 2016
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
Binary file modified ladep.phar
Binary file not shown.
6 changes: 6 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,11 @@
"sha1" : "c7ef21d971caaf078eb410b6e0ec3ae2a34c3882",
"url" : "https://github.com/dekuan/ladep/raw/1.0.15/ladep.phar",
"version" : "1.0.15"
},
{
"name" : "ladep.phar",
"sha1" : "df71adabaf487112fab8640acfdeea8ca7da29fe",
"url" : "https://github.com/dekuan/ladep/raw/1.0.16/ladep.phar",
"version" : "1.0.16"
}
]
204 changes: 20 additions & 184 deletions src/ladep/models/CCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

use dekuan\vdata;

use dekuan\ladep\libs;
use dekuan\ladep\models\classes;

Expand All @@ -21,11 +21,6 @@
*/
class CCheck
{
// ...
const CONST_CHECK_SERVER_NAME = 'LADEP CHECK WEB SERVICE';
const CONST_CHECK_SERVER_HOST = '127.0.0.1'; // server host
const CONST_CHECK_SERVER_PORT = 9916; // server port

// ...
private $m_cProject = null;

Expand All @@ -44,6 +39,7 @@ public function Run( $arrParameter, callable $pfnCbFunc )
$sErrorDesc = '';
$sErrorPath = '';
$cGit = new classes\CGit();
$cMiniSrv = classes\CMiniServer::GetInstance();


if ( ! is_callable( $pfnCbFunc ) )
Expand Down Expand Up @@ -97,43 +93,37 @@ public function Run( $arrParameter, callable $pfnCbFunc )

if ( is_string( $sReleaseDir ) && is_dir( $sReleaseDir ) )
{
//
// try to stop mini http server
//
$pfnCbFunc( 'info',
sprintf( "MINI-SERVER\t: Try to launch a mini-server on tcp port %d to check your website", $cMiniSrv->GetServerPort() ) );
$cMiniSrv->SafeRestart( $sReleaseDir, $pfnCbFunc );

// ...
if ( $this->_IsCheckServerListening() )
{
$this->_StopCheckService( $pfnCbFunc );
}
usleep( 1000 );

if ( ! $this->_IsCheckServerListening() )
if ( $cMiniSrv->IsListening() )
{
if ( $this->_StartCheckService( $sReleaseDir, $pfnCbFunc ) )
{
sleep( 3 );

$nStatusCode = 0;
if ( $this->_IsWebsiteAvailable( $nStatusCode ) )
{
$nRet = 0;
$pfnCbFunc( 'sinfo', sprintf( "CHECKING\t: %s", $cMiniSrv->GetServerUrl() ) );

// ...
$pfnCbFunc( 'info', "\t\t: Website works well!" );
}
else
{
$pfnCbFunc( 'comment', sprintf( "\t\t# Website is not available, status code is %d.", $nStatusCode ) );
}
$nStatusCode = 0;
if ( $cMiniSrv->IsWebAvailable( $nStatusCode ) )
{
$nRet = 0;

// ...
sleep( 1 );
$this->_StopCheckService( $pfnCbFunc );
$pfnCbFunc( 'sinfo', "\t\t\t\t\t\t[WORKS WELL]" );
}
else
{
$pfnCbFunc( 'error', "\t\t# Failed to start check web service." );
$pfnCbFunc( 'scomment', sprintf( "\t\t\t\t\t\t[NOT AVAILABLE] http code %d.", $nStatusCode ) );
}
$pfnCbFunc( 'info', "" );
}
else
{
$pfnCbFunc( 'error', sprintf( "\t\t# Port %d already in use.", self::CONST_CHECK_SERVER_PORT ) );
$pfnCbFunc( 'error', sprintf( "\t\t# Failed to start %s", $cMiniSrv->GetServerName() ) );
}
}
else
Expand Down Expand Up @@ -162,159 +152,5 @@ public function Run( $arrParameter, callable $pfnCbFunc )
////////////////////////////////////////////////////////////////////////////////
// Private
//
private function _IsCheckServerListening()
{
$bRet = false;

// ...
$nErrorId = -1;
$sErrorStr = '';
$nTimeout = 3;

try
{
$fp = fsockopen( self::CONST_CHECK_SERVER_HOST, self::CONST_CHECK_SERVER_PORT, $nErrorId, $sErrorStr, $nTimeout );
if ( false !== $fp )
{
$bRet = true;

fclose( $fp );
}
}
catch ( \Exception $e )
{}

// ...
return $bRet;
}

private function _IsWebsiteAvailable( & $nStatusCode = 0 )
{
$cRequest = vdata\CRequest::GetInstance();

// ...
$bRet = false;

// ...
$arrResp = null;
$sUrl = $this->_GetCheckServiceUrl();

$nCall = $cRequest->HttpRaw
(
[
'method' => 'GET',
'url' => $sUrl,
],
$arrResp
);
if ( vdata\CConst::ERROR_SUCCESS == $nCall &&
$cRequest->IsValidRawResponse( $arrResp ) )
{
// ...
$nStatusCode = $arrResp['status'];

// ...
if ( 200 == $arrResp['status'] )
{
$bRet = true;
}
}

return $bRet;
}

private function _GetCheckServiceUrl()
{
return sprintf( "http://%s:%d", self::CONST_CHECK_SERVER_HOST, self::CONST_CHECK_SERVER_PORT );
}

private function _StartCheckService( $sReleaseDir, callable $pfnCbFunc )
{
if ( ! is_callable( $pfnCbFunc ) )
{
throw new \RuntimeException( sprintf( "%s::%s pfnCbFunc is not callable function.", __CLASS__, __FUNCTION__ ) );
}
if ( ! is_string( $sReleaseDir ) || ! is_dir( $sReleaseDir ) )
{
$pfnCbFunc( 'error', "\t\t# Error in parameter [sReleaseDir] in " . __FUNCTION__ );
return null;
}

// ...
$bRet = false;

// ...
$sCmdLine = sprintf
(
"php -S %s:%d -t \"%s/public\" > /dev/null 2>&1 &",
self::CONST_CHECK_SERVER_HOST,
self::CONST_CHECK_SERVER_PORT,
libs\Lib::RTrimPath( $sReleaseDir )
);

// ...
$cProcess = new Process\Process( $sCmdLine );
$cProcess
->setTimeout( libs\Config::Get( 'cmd_timeout' ) )
->enableOutput()
->run()
;

if ( $cProcess->isSuccessful() )
{
$bRet = true;

if ( is_callable( $pfnCbFunc ) )
{
$pfnCbFunc( 'info', "\t\t: Start [" . self::CONST_CHECK_SERVER_NAME . "] successfully." );
}
}
else
{
if ( is_callable( $pfnCbFunc ) )
{
$pfnCbFunc( 'error', "\t\t: Failed to start [" . self::CONST_CHECK_SERVER_NAME . "]." );
$pfnCbFunc( 'error', $cProcess->getErrorOutput() );
}
}

return $bRet;
}

private function _StopCheckService( callable $pfnCbFunc )
{
$bRet = false;

if ( $this->_IsCheckServerListening() )
{
// ...
$sCmdLine = sprintf( "kill -9 $(lsof -t -i:%d) > /dev/null 2>&1", self::CONST_CHECK_SERVER_PORT );

// ...
$cProcess = new Process\Process( $sCmdLine );
$cProcess
->setTimeout( libs\Config::Get( 'cmd_timeout' ) )
->enableOutput()
->run()
;

if ( $cProcess->isSuccessful() )
{
$bRet = true;

$pfnCbFunc( 'info', "\t\t: Stop [" . self::CONST_CHECK_SERVER_NAME . "] successfully." );
}
else
{
$pfnCbFunc( 'error', "\t\t: Failed to stop [" . self::CONST_CHECK_SERVER_NAME . "]." );
$pfnCbFunc( 'error', $cProcess->getErrorOutput() );
}
}
else
{
$pfnCbFunc( 'info', "\t\t: [" . self::CONST_CHECK_SERVER_NAME . "] already stopped." );
}

return $bRet;
}
}
Loading