Commit 7743eaf9 authored by Ludovic Ganée's avatar Ludovic Ganée

qualité du code

parent 98994c79
Pipeline #9201 failed with stage
in 15 seconds
......@@ -17,6 +17,9 @@ namespace Beanstalk\Model\Entity;
use Beanstalk\Utility\Beanstalk;
use Cake\Core\Configure;
use Cake\ORM\Entity;
use Exception;
use Pheanstalk\Exception\ServerException;
use Pheanstalk\Response;
/**
* Classe de l'entité
......@@ -54,7 +57,7 @@ class BeanstalkJob extends Entity
/**
* Récupère les informations beanstalk sur un job en fonction de son id
* @return array
* @throws \Exception
* @throws Exception
*/
public function beanstalkStats()
{
......@@ -71,8 +74,8 @@ class BeanstalkJob extends Entity
/**
* Demande le statut du serveur Beanstalkd
* @return array|object|\Pheanstalk\Response
* @throws \Exception
* @return array|object|Response
* @throws Exception
*/
public function getServerBeanstalkStatus()
{
......@@ -95,7 +98,7 @@ class BeanstalkJob extends Entity
/**
* Champ virtuel
* @return string
* @throws \Exception
* @throws Exception
*/
protected function _getState()
{
......@@ -106,7 +109,7 @@ class BeanstalkJob extends Entity
/**
* Champ virtuel
* @return string
* @throws \Exception
* @throws Exception
*/
protected function _getAge()
{
......@@ -117,7 +120,7 @@ class BeanstalkJob extends Entity
/**
* Champ virtuel
* @return mixed
* @throws \Exception
* @throws Exception
*/
protected function _getData()
{
......@@ -126,7 +129,7 @@ class BeanstalkJob extends Entity
$Beanstalk->selectJob($this->_properties['jobid']);
try {
return $Beanstalk->getData();
} catch (\Pheanstalk\Exception\ServerException $e) {
} catch (ServerException $e) {
return false;
}
} else {
......@@ -137,7 +140,7 @@ class BeanstalkJob extends Entity
/**
* Champ virtuel
* @return string
* @throws \Exception
* @throws Exception
*/
protected function _getStatetrad()
{
......@@ -157,7 +160,7 @@ class BeanstalkJob extends Entity
/**
* Champ virtuel
* @return string
* @throws \Exception
* @throws Exception
*/
protected function _getRelatedId()
{
......
......@@ -75,7 +75,6 @@ class BeanstalkWorkersTable extends Table
*/
public function sync()
{
$hostname = gethostname();
$query = $this->find()->select(['id', 'hostname', 'pid']);
/** @var BeanstalkWorker $entity */
foreach ($query as $entity) {
......
......@@ -14,7 +14,10 @@ use Cake\Database\Connection;
use Cake\Datasource\ConnectionManager;
use Cake\Log\Log;
use Exception;
use Pheanstalk\Exception\ServerException;
use ReflectionClass;
use ReflectionException;
use Throwable;
/**
* Squelette d'un worker
......@@ -71,7 +74,7 @@ abstract class AbstractWorker implements WorkerInterface
* @param Beanstalk $Beanstalk
* @param boolean $keepAlive
* @param array $params
* @throws \ReflectionException
* @throws ReflectionException
*/
public function __construct(
Beanstalk $Beanstalk,
......@@ -153,7 +156,7 @@ abstract class AbstractWorker implements WorkerInterface
'jobs' => $stats['jobs'],
]
);
$this->log(Debugger::printVar($sta));
$this->log(Debugger::exportVar($sta));
continue;
} elseif (isset($data['command']) && isset($data['beanstalk_worker_id'])) {
if (isset($this->params['id'])
......@@ -167,7 +170,7 @@ abstract class AbstractWorker implements WorkerInterface
$this->log(sprintf("The command for worker %d was not executed", $data['beanstalk_worker_id']));
$this->Worker->done();
} else {
$sleepTime = isset($params['sleep_time']) ? $params['sleep_time'] : 1;
$sleepTime = isset($this->params['sleep_time']) ? $this->params['sleep_time'] : 1;
$this->log(
sprintf(
"Worker command sent for id=%d (sleeping for %d second(s))",
......@@ -196,7 +199,7 @@ abstract class AbstractWorker implements WorkerInterface
$this->log("Shutdown triggered");
$this->Worker->getPheanstalk()->getConnection()->disconnect();
return $e->getCode();
} catch (\Throwable $ex) {
} catch (Throwable $ex) {
$this->resetConnection();// rollback avant d'éffacer le job
$this->fail($ex, $data);
$this->Worker->fail((string)$ex);
......@@ -218,7 +221,7 @@ abstract class AbstractWorker implements WorkerInterface
* Arret du chrono de durée du job
* @param array $stats
* @param string $type
* @return int
* @return string
*/
private function chronoStop(array &$stats, string $type): string
{
......@@ -260,6 +263,8 @@ abstract class AbstractWorker implements WorkerInterface
/**
* Lancé après work()
* @param mixed $data
* @throws Exception
* @noinspection PhpUnusedParameterInspection
*/
protected function afterWork($data)
{
......@@ -289,6 +294,7 @@ abstract class AbstractWorker implements WorkerInterface
* {@inheritdoc}
*
* @param array $params
* @throws ServerException
*/
public function touch($params = array())
{
......@@ -313,11 +319,11 @@ abstract class AbstractWorker implements WorkerInterface
/**
* {@inheritdoc}
*
* @param \Throwable $ex Exception envoyé depui le bloc
* start()
* @param mixed $data Data obtenu par Beanstalkd
* @param Throwable $ex Exception envoyé depui le bloc
* start()
* @param mixed $data Data obtenu par Beanstalkd
*/
public function fail(\Throwable $ex, $data)
public function fail(Throwable $ex, $data)
{
$this->log(
sprintf(
......@@ -368,7 +374,7 @@ abstract class AbstractWorker implements WorkerInterface
/**
* Assure que le job ne soit plus réservé lors de la coupure du worker
* @throws \Pheanstalk\Exception\ServerException
* @throws ServerException
*/
public function __destruct()
{
......
......@@ -16,7 +16,7 @@ namespace Beanstalk\Shell;
use Beanstalk\Exception\CantWorkException;
use Beanstalk\Utility\Beanstalk;
use Exception;
use Throwable;
/**
* Interface d'un worker
......@@ -75,11 +75,11 @@ interface WorkerInterface
/**
* Callback utilisé en cas d'Exception dans le bloc d'execution du worker
*
* @param \Throwable $ex Exception envoyé depui le bloc
* start()
* @param mixed $data Data obtenu par Beanstalkd
* @param Throwable $ex Exception envoyé depui le bloc
* start()
* @param mixed $data Data obtenu par Beanstalkd
*/
public function fail(\Throwable $ex, $data);
public function fail(Throwable $ex, $data);
/**
* Met fin à l'execution du worker
......
......@@ -19,9 +19,12 @@ use Cake\Console\ConsoleOptionParser;
use Cake\Console\Shell;
use Cake\Core\Configure;
use Cake\Datasource\EntityInterface;
use Cake\I18n\Time;
use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector;
use Exception;
use ReflectionClass;
use ReflectionException;
/**
* Classe du shell
......@@ -106,7 +109,7 @@ class WorkerShell extends Shell
$parser->addOption(
'log-file',
[
'help' => __("Fichier log du worker <default: logs/worker_<tube>.log>"),
'help' => __("Fichier log du worker default: logs/worker_<tube>.log>"),
]
);
$parser->setDescription(
......@@ -165,7 +168,7 @@ class WorkerShell extends Shell
'tube' => $tube,
'path' => $className,
'pid' => getmypid(),
'last_launch' => new \Cake\I18n\Time,
'last_launch' => new Time,
'hostname' => $hostname,
'logfile' => $this->param('log-file')
?: Configure::read('App.paths.logs', LOGS).'worker_'.$tube.'.log',
......@@ -218,7 +221,7 @@ class WorkerShell extends Shell
* @param string|array $dirs
* @param string $suffix
* @return array[\ReflectionClass]
* @throws \ReflectionException
* @throws ReflectionException
*/
public static function availablesWorkers($dirs = [], $suffix = 'Worker')
{
......@@ -253,7 +256,7 @@ class WorkerShell extends Shell
* @param string $filename
* @param array $workers
* @param string $suffix
* @throws \ReflectionException
* @throws ReflectionException
*/
protected static function parseWorkerClassname($filename, &$workers, $suffix = 'Worker')
{
......@@ -282,7 +285,7 @@ class WorkerShell extends Shell
$classname .= $token[1];
} elseif ($inClassname) {
$classname = ltrim($classname.'\\'.$token[1], '\\');
$reflection = new \ReflectionClass($classname);
$reflection = new ReflectionClass($classname);
if ($reflection->implementsInterface(WorkerInterface::class) && $reflection->isInstantiable()) {
$workers[$workername] = $classname;
}
......
......@@ -6,7 +6,11 @@
namespace Beanstalk\Utility;
use Cake\Core\Configure;
use Cake\ORM\Query;
use Cake\ORM\TableRegistry;
use DateTime;
use Error;
use Exception;
use Pheanstalk\Exception\ServerException;
use Pheanstalk\Job;
use Pheanstalk\Pheanstalk;
......@@ -82,7 +86,7 @@ class Beanstalk
$this->params['persistant']
);
$this->Pheanstalk->useTube($tube)->watchOnly($tube);
} catch (\Exception $e) {
} catch (Exception $e) {
}
}
......@@ -129,7 +133,7 @@ class Beanstalk
$classname = Configure::read('Beanstalk.PheanstalkClassname', Pheanstalk::class);
try {
$this->Pheanstalk = new $classname($host, $port, $connectTimeout, $connectPersistent);
} catch (\Error $e) {
} catch (Error $e) {
$this->Pheanstalk = null;
}
return $this;
......@@ -198,7 +202,7 @@ class Beanstalk
if ($pheanstalk && ($conn = $pheanstalk->getConnection()) && $conn->isServiceListening()) {
$conn->disconnect();
}
} catch (\Exception $e) {
} catch (Exception $e) {
}
}
......@@ -267,11 +271,13 @@ class Beanstalk
/**
* Supprime le job
* @return self
* @throws ServerException
* @throws ServerException
*/
public function done()
{
$this->checkTtr();
$jobid = $this->getJob()->getId(true);
$jobid = $this->getJob()->getId();
$this->getPheanstalk()->delete($this->getJob(true));
$Jobs = TableRegistry::getTableLocator()->get($this->params['table_jobs']);
$Jobs->deleteAll(['jobid' => $jobid]);
......@@ -339,6 +345,7 @@ class Beanstalk
* Récupère le job suivant
* @param null $timeout
* @return Beanstalk
* @throws ServerException
*/
public function getNext($timeout = null)
{
......@@ -479,18 +486,20 @@ class Beanstalk
{
if ($tube && $this->getTube() !== $tube) {
$this->setTube($tube);
} elseif (!$tube) {
$tube = $this->getTube();
}
/** @var Job $job */
while ($this->getPheanstalk()->statsTube($tubeName)->{'current-jobs-buried'}) {
$job = $this->getPheanstalk()->peekBuried($tubeName);
while ($this->getPheanstalk()->statsTube($tube)->{'current-jobs-buried'}) {
$job = $this->getPheanstalk()->peekBuried($tube);
$this->getPheanstalk()->delete($job);
}
while ($this->getPheanstalk()->statsTube($tubeName)->{'current-jobs-delayed'}) {
$job = $this->getPheanstalk()->peekDelayed($tubeName);
while ($this->getPheanstalk()->statsTube($tube)->{'current-jobs-delayed'}) {
$job = $this->getPheanstalk()->peekDelayed($tube);
$this->getPheanstalk()->delete($job);
}
while ($this->getPheanstalk()->statsTube($tubeName)->{'current-jobs-ready'}) {
$job = $this->getPheanstalk()->peekReady($tubeName);
while ($this->getPheanstalk()->statsTube($tube)->{'current-jobs-ready'}) {
$job = $this->getPheanstalk()->peekReady($tube);
$this->getPheanstalk()->delete($job);
}
$Jobs = TableRegistry::getTableLocator()->get($this->params['table_jobs']);
......@@ -511,7 +520,7 @@ class Beanstalk
if ($this->job && $this->statsJob()->state === 'reserved') {
$this->cancel();
}
} catch (\Exception $e) {
} catch (Exception $e) {
}
$this->job = null;
......@@ -529,7 +538,7 @@ class Beanstalk
/**
* Donne la liste des jobs enregistrés en base
* @return \Cake\ORM\Query
* @return Query
*/
public function find()
{
......@@ -547,11 +556,10 @@ class Beanstalk
}
$duration = (int)(microtime(true) - $this->lastTouch);
$expired = $duration > 60
? (new \DateTime('@'.$duration))->format('H:i:s')
? (new DateTime('@'.$duration))->format('H:i:s')
: $duration.' seconds';
if ($duration > $this->ttr) {
throw new \Exception;
trigger_error('maximum runtime exceeded '.$expired);
throw new Exception('maximum runtime exceeded '.$expired);
}
return $expired;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment