PHP Backend Application(6)Retry Function, PHPUNIT ENV and Async Redis and Large

aikudeyu 2016-07-01

PHPBackendApplication(6)RetryFunction,PHPUNITENVandAsyncRedisandLargeFile

1RetryFunctionsinPHP

/**

*postjsonparamstoprediction

*@paramstring$path

*@paramarray$params,formatwillbe['key1'=>'value1','key2'=>'value2',]

*@returnresponse

*/

publicfunctionpost2Prediction($path,$params)

{

return$this->retry(function()use($path,$params){

$response=$this->predictionClient->request('POST',$path,[

'json'=>$params

]);

return$response;

},10,3);

}

/**

*retrymanytimes

*@paramfunction$f

*@paramnumber$delay

*@paramnumber$retryies

*/

privatefunctionretry($f,$delay=10,$retryies=3){

$logger=$this->ioc->getService("logger");

try{

return$f();

}catch(Exception$e){

if($retryies>0){

sleep($delay);

returnretry($f,$delay,$retryies-1);

}else{

$logger->error(\GuzzleHttp\Psr7\str($e->getRequest()));

if($e->hasResponse()){

$logger->error(\GuzzleHttp\Psr7\str($e->getResponse()));

}

}

}

}

2phpunitRunningEnv

http://elnur.pro/using-environment-variables-to-add-flexibility-to-phpunit-tests/

Addtheenvironmentparametersinphpunit.xml,itworksinEclipseenv

<phpunitbootstrap="tests/bootstrap_test.php">

<php>

<envname="RUNNING_ENV"value="test"/>

</php>

<testsuites>

<testsuitename="unitsuite">

<directory>tests</directory>

</testsuite>

</testsuites>

</phpunit>

Inthecommandline,

RUNNING_ENV=testphpunit--bootstrapvendor/autoload.phptests/JobProducerPHP/JobsXMLParserTest

RUNNING_ENV=testphpsrc/SuperStarProducer.php12001'/Users/carl/company/code/jobs-producer/data/12001.xml'

3ObjectInheritanceinPHP

http://stackoverflow.com/questions/11237511/multiple-ways-of-calling-parent-method-in-php

http://php.net/manual/en/language.oop5.inheritance.php

AbstractBaseclass

<?php

namespaceJobProducerPHP;

require__DIR__.'/../../vendor/autoload.php';

abstractclassImport

{

protected$ioc=null;

publicfunction__construct($ioc)

{

$this->ioc=$ioc;

$logger=$this->ioc->getService("logger");

$config=$this->ioc->getService("config");

}

publicfunctionprintInfo(){

$logger=$this->ioc->getService("logger");

$logger->debug("callingmethodinparentclass.");

}

publicfunctionprintInfo2(){

$logger=$this->ioc->getService("logger");

$logger->debug("callingmethod2inparentclass.");

}

}

?>

SubClasshadtheactuallyimplementation

<?php

namespaceJobProducerPHP;

require__DIR__.'/../../vendor/autoload.php';

classImportIndeedFmtextendsImport

{

publicfunctionprintInfo2(){

$logger=$this->ioc->getService("logger");

$logger->debug("beforecallingparent");

$this->printInfo();

parent::printInfo2();

$logger->debug("aftercallingparent");

}

}

4Predis-Async

https://github.com/nrk/predis-async/blob/master/examples/execute_commands.php

dependency

"predis/predis-async":"dev-master"

AsyncRedisClient,itworksprettywell

<?php

namespaceJobProducerPHP;

require__DIR__.'/../../vendor/autoload.php';

use\Predis\Async\Client;

classRedisClient

{

private$client=null;

private$ioc=null;

publicfunction__construct($ioc)

{

$this->ioc=$ioc;

$logger=$this->ioc->getService("logger");

$config=$this->ioc->getService("config");

$logger->info("==============Redisconfigstart==============");

$logger->info("redisHost=".$config['redisHost']);

$logger->info("redisPort=".$config['redisPort']);

$logger->info("===============================================");

try

{

$this->client=newClient('tcp://'.$config['redisHost'].":".$config['redisPort']);

$logger->debug("SuccessfullysetupRedis");

}

catch(Exception$e)

{

$logger->error("Couldn'tconnectedtoRedis");

$logger->error($e->getMessage());

}

}

/**

*

*@paramarray$messagesformateg:array("key"=>"value",...)

*/

publicfunctionsetMessageBatch($messages)

{

$client=$this->client;

$logger=$this->ioc->getService("logger");

$queueClient=$this->ioc->getService("queueClient");

//sendtoredis

foreach($messagesas$key=>$value){

$client->connect(function($client)use($key,$value,$logger){

$logger->debug("connecttoRedissuccess.001");

$logger->debug("sendingmessagefor{$key}=>{$value}002");

$client->set($key,$value,function($response,$client)use($logger,$key){

$logger->debug("responsefromserver".$response."003");

$client->disconnect();

});

});

$client->getEventLoop()->run();

$logger->debug("mainthreadrunshere004");

}

$logger->debug("mainthreadrunshere005");

//sendtoSQS

$keys=array_keys($messages);

$queueClient->sendMessageBatch($keys);

}

}

?>

5PHPhandleLargeFile

publicfunctionregexMatch($line){

returnpreg_match('</job>',$line);

}

publicfunctionsplitLargeFile($filePath,$sourceID)

{

try{

$outputSize=0;

$fileCount=0;

$outputFile=null;

foreach(newSplFileObject('/data/1052.xml')as$line){

if($outputSize===0){

$outputFilename="/data/1052_split/{$fileCount}.xml";

$outputFile=fopen($outputFileName,"w");

}

$outputSize+=strlen($line);

fwrite($outputFile,$line.PHP_EOL);

if($this->regexMatch($line)&&$outputSize>=100000000){

fclose($outputFile);

$outputSize=0;

$fileCount++;

}

}

if($outputSize>0){

fclose($outputFile);

}

}catch(Exception$e){

echo$e->getMessage();

}

}

PHPRegexUtil

publicfunctionregexMatchJob($line){

returnpreg_match('/<\/job>/i',$line);

}

publicfunctionextractReferenceNumber($line){

$result='';

preg_match('/<[Rr]eferencenumber>(.*?)<\/[Rr]eferencenumber>/i',$line,$matches);

if(!empty($matches)&&count($matches)>1){

$result=$matches[1];

$result=preg_replace('/\]\]>/i','',$result);

$result=preg_replace('/<!\[CDATA\[/i','',$result);

}

return$result;

}

References:

retrysystem

https://gist.github.com/mudge/5948769

SOLRjoin

http://stackoverflow.com/questions/12665797/is-solr-4-0-capable-of-using-join-for-multiple-core

PRedis

https://github.com/phpredis/phpredis

Redis

http://redis.io/commands#sorted_set

相关推荐