Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
assertchris committed Dec 6, 2015
1 parent ee6ffef commit 58737c0
Show file tree
Hide file tree
Showing 19 changed files with 129 additions and 129 deletions.
3 changes: 2 additions & 1 deletion src/Expires.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ interface Expires
public function getExpiresIn();

/**
* Checks whether a task or process should expire. This is called when a manager thinks a task or process should expire.
* Checks whether a task or process should expire. This is called when a manager thinks a task
* or process should expire.
*
* @param int $startedAt
*
Expand Down
2 changes: 1 addition & 1 deletion src/Handler/CallbackHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use AsyncPHP\Doorman\Handler;
use AsyncPHP\Doorman\Task;

class CallbackHandler implements Handler
final class CallbackHandler implements Handler
{
/**
* @inheritdoc
Expand Down
3 changes: 2 additions & 1 deletion src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ interface Manager
public function addTask(Task $task);

/**
* Executes a single processing cycle. This should be run repeatedly, and will return false when there are no more running or waiting processes.
* Executes a single processing cycle. This should be run repeatedly, and will return false
* when there are no more running or waiting processes.
*
* @return bool
*/
Expand Down
14 changes: 7 additions & 7 deletions src/Manager/GroupProcessManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
use AsyncPHP\Doorman\Manager;
use AsyncPHP\Doorman\Task;

class GroupProcessManager implements Manager
final class GroupProcessManager implements Manager
{
/**
* @var Manager
*/
protected $manager;
private $manager;

/**
* @var Task[][]
*/
protected $waiting = array();
private $waiting = [];

/**
* @var Task[][]
*/
protected $queuing = array();
private $queuing = [];

/**
* @param Manager $manager
Expand All @@ -39,7 +39,7 @@ public function __construct(Manager $manager)
*/
public function addTask(Task $task)
{
array_push($this->waiting, array($task));
array_push($this->waiting, [$task]);

return $this;
}
Expand Down Expand Up @@ -90,12 +90,12 @@ public function tick()
* Passes missing method calls to the decorated manager.
*
* @param string $method
* @param array $parameters
* @param array $parameters
*
* @return mixed
*/
public function __call($method, array $parameters)
{
return call_user_func_array(array($this->manager, $method), $parameters);
return call_user_func_array([$this->manager, $method], $parameters);
}
}
88 changes: 45 additions & 43 deletions src/Manager/ProcessManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,32 @@
use AsyncPHP\Doorman\Task;
use SplObjectStorage;

class ProcessManager implements Manager
final class ProcessManager implements Manager
{
/**
* @var Task[]
*/
protected $waiting = array();
private $waiting = [];

/**
* @var Task[]
*/
protected $running = array();
private $running = [];

/**
* @var null|SplObjectStorage
*/
protected $timings = null;
private $timings = null;

/**
* @var null|string
*/
protected $logPath;
private $logPath;

/**
* @var null|Rules
*/
protected $rules;
private $rules;

/**
* @var null|Shell
Expand All @@ -51,12 +51,12 @@ class ProcessManager implements Manager
/**
* @var null|string
*/
protected $binary;
private $binary;

/**
* @var null|string
*/
protected $worker;
private $worker;

/**
* @inheritdoc
Expand All @@ -83,14 +83,14 @@ public function tick()
$this->timings = new SplObjectStorage();
}

$waiting = array();
$running = array();
$waiting = [];
$running = [];

foreach ($this->waiting as $task) {
if ($this->isTaskCancelled($task)) {
continue;
}

if (!$this->canRunTask($task)) {
$waiting[] = $task;
continue;
Expand All @@ -109,9 +109,9 @@ public function tick()
$this->timings[$task] = time();
}

$output = $this->getShell()->exec("{$binary} {$worker} %s {$stdout} {$stderr} & echo $!", array(
$output = $this->getShell()->exec("{$binary} {$worker} %s {$stdout} {$stderr} & echo $!", [
$this->getTaskString($task),
));
]);

if ($task instanceof Process) {
$task->setId($output[0]);
Expand Down Expand Up @@ -139,15 +139,15 @@ public function tick()
*
* @return $this
*/
protected function stopSiblingTasks(Task $task)
private function stopSiblingTasks(Task $task)
{
$handler = $task->getHandler();

foreach ($this->running as $task) {
if ($task->getHandler() === $handler && $task instanceof Process) {
$this->getShell()->exec("kill -9 %s", array(
$this->getShell()->exec("kill -9 %s", [
$task->getId(),
));
]);
}
}

Expand All @@ -161,9 +161,9 @@ protected function stopSiblingTasks(Task $task)
*
* @return bool
*/
protected function canRunTask(Task $task)
private function canRunTask(Task $task)
{
if(!$task->canRunTask()) {
if (!$task->canRunTask()) {
return false;
}

Expand All @@ -187,12 +187,12 @@ protected function canRunTask(Task $task)
/**
* Gets the load profile related to a task.
*
* @param Task $task
* @param Task $task
* @param array $processes
*
* @return Profile
*/
protected function getProfileForProcesses(Task $task, array $processes)
private function getProfileForProcesses(Task $task, array $processes)
{
$stats = $this->getStatsForProcesses($processes);

Expand Down Expand Up @@ -222,14 +222,14 @@ protected function getProfileForProcesses(Task $task, array $processes)
*
* @return array
*/
protected function getStatsForProcesses(array $processes)
private function getStatsForProcesses(array $processes)
{
$stats = array();
$stats = [];

foreach ($processes as $process) {
$output = $this->getShell()->exec("ps -o pid,%%cpu,%%mem,state,start -p %s | sed 1d", array(
$output = $this->getShell()->exec("ps -o pid,%%cpu,%%mem,state,start -p %s | sed 1d", [
$process->getId(),
));
]);

if (count($output) < 1) {
continue;
Expand Down Expand Up @@ -286,7 +286,7 @@ public function setShell(Shell $shell)
*
* @return Shell
*/
protected function newShell()
private function newShell()
{
return new BashShell();
}
Expand All @@ -296,7 +296,7 @@ protected function newShell()
*
* @return Profile
*/
protected function newProfile()
private function newProfile()
{
return new InMemoryProfile();
}
Expand Down Expand Up @@ -332,7 +332,7 @@ public function setRules(Rules $rules)
*
* @return Rules
*/
protected function newRules()
private function newRules()
{
return new InMemoryRules();
}
Expand All @@ -357,7 +357,7 @@ public function setBinary($binary)
public function getBinary()
{
if ($this->binary === null) {
$this->binary = PHP_BINDIR."/php";
$this->binary = PHP_BINDIR . "/php";
}

return $this->binary;
Expand All @@ -383,7 +383,7 @@ public function setWorker($worker)
public function getWorker()
{
if ($this->worker === null) {
$this->worker = realpath(__DIR__."/../../bin/worker.php");
$this->worker = realpath(__DIR__ . "/../../bin/worker.php");
}

return $this->worker;
Expand All @@ -394,10 +394,10 @@ public function getWorker()
*
* @return string
*/
protected function getStdOut()
private function getStdOut()
{
if ($this->getLogPath() !== null) {
return ">> ".$this->getLogPath()."/stdout.log";
return ">> " . $this->getLogPath() . "/stdout.log";
}

return "> /dev/null";
Expand Down Expand Up @@ -428,10 +428,10 @@ public function setLogPath($logPath)
*
* @return string
*/
protected function getStdErr()
private function getStdErr()
{
if ($this->getLogPath() !== null) {
return "2>> ".$this->getLogPath()."/stderr.log";
return "2>> " . $this->getLogPath() . "/stderr.log";
}

return "2> /dev/null";
Expand All @@ -444,7 +444,7 @@ protected function getStdErr()
*
* @return string
*/
protected function getTaskString(Task $task)
private function getTaskString(Task $task)
{
return base64_encode(serialize($task));
}
Expand All @@ -456,12 +456,12 @@ protected function getTaskString(Task $task)
*
* @return bool
*/
protected function canRemoveTask(Task $task)
private function canRemoveTask(Task $task)
{
if (!$task instanceof Process) {
return true;
}

if ($this->isTaskExpired($task) || $this->isTaskCancelled($task)) {
$this->killTask($task);
return true;
Expand Down Expand Up @@ -491,14 +491,16 @@ protected function canRemoveTask(Task $task)
* Check if the given task is expired
*
* @param Task $task
*
* @return boolean
*/
protected function isTaskExpired(Task $task) {
private function isTaskExpired(Task $task)
{
if ($task instanceof Expires) {
$expiresIn = $task->getExpiresIn();
$startedAt = $this->timings[$task];

if($expiresIn > 0 && (time() - $startedAt) >= $expiresIn) {
if ($expiresIn > 0 && (time() - $startedAt) >= $expiresIn) {
return $task->shouldExpire($startedAt);
}
}
Expand All @@ -508,12 +510,12 @@ protected function isTaskExpired(Task $task) {

/**
* Check if the given task is cancelled.
*
*
* @param Task $task
*
* @return bool
*/
protected function isTaskCancelled(Task $task)
private function isTaskCancelled(Task $task)
{
if ($task instanceof Cancellable) {
return $task->isCancelled();
Expand All @@ -529,12 +531,12 @@ protected function isTaskCancelled(Task $task)
*
* @return bool
*/
protected function killTask(Task $task)
private function killTask(Task $task)
{
if ($task instanceof Process) {
$this->getShell()->exec("kill -9 %s", array(
$this->getShell()->exec("kill -9 %s", [
$task->getId(),
));
]);

return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Manager/SynchronousManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
use AsyncPHP\Doorman\Manager;
use AsyncPHP\Doorman\Task;

class SynchronousManager implements Manager
final class SynchronousManager implements Manager
{
/**
* @var Task[]
*/
protected $waiting = array();
private $waiting = [];

/**
* @inheritdoc
Expand Down
Loading

0 comments on commit 58737c0

Please sign in to comment.