Skip to content

Commit

Permalink
Avoid creation of callable in loop
Browse files Browse the repository at this point in the history
  • Loading branch information
Benoît Burnichon committed Mar 11, 2016
1 parent 69fdd4a commit 2b199e8
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions src/Alchemy/TaskManager/Job/AbstractJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,14 @@ public function getStatus()
*/
final public function run(JobDataInterface $data = null, $callback = null)
{
$data = $data ?: new NullJobData();
$this->dispatcher->dispatch(JobEvents::START, new JobEvent($this, $data));
$this->setup($data);
$this->prepareRun($callback, $data);
while (static::STATUS_STARTED === $this->status) {
$this->doRunOrCleanup($data, $callback);
$this->pause($this->getPauseDuration());
}
$this->dispatcher->dispatch(JobEvents::STOP, new JobEvent($this, $data));
$this->cleanRun($data);

return $this->cleanup();
return $this;
}

/**
Expand All @@ -149,12 +147,9 @@ final public function run(JobDataInterface $data = null, $callback = null)
*/
final public function singleRun(JobDataInterface $data = null, $callback = null)
{
$data = $data ?: new NullJobData();
$this->dispatcher->dispatch(JobEvents::START, new JobEvent($this, $data));
$this->setup($data);
$this->prepareRun($callback, $data);
$this->doRunOrCleanup($data, $callback);
$this->dispatcher->dispatch(JobEvents::STOP, new JobEvent($this, $data));
$this->cleanup();
$this->cleanRun($data);

return $this;
}
Expand Down Expand Up @@ -264,16 +259,16 @@ private function cleanup()
* and forward the exception.
*
* @param JobDataInterface $data The data to pass to the doRun method.
* @param null|callable $callback A callback
* @param callable $callback A callback
*
* @return JobInterface
*
* @throws Exception The caught exception is forwarded in case it occured.
* @throws \Exception The caught exception is forwarded in case it occured.
*/
private function doRunOrCleanup(JobDataInterface $data, $callback = null)
private function doRunOrCleanup(JobDataInterface $data, $callback)
{
try {
call_user_func($this->createCallback($callback), $this, $this->doRun($data));
call_user_func($callback, $this, $this->doRun($data));
} catch (\Exception $e) {
$this->cleanup();
$this->log('error', sprintf('Error while running %s : %s', (string) $data, $e->getMessage()), array('exception' => $e));
Expand All @@ -299,4 +294,26 @@ private function createCallback($callback)

return function () {};
}

/**
* @param null|callable $callback
* @param null|JobDataInterface $data
*/
private function prepareRun(&$callback, JobDataInterface &$data = null)
{
$data = $data ?: new NullJobData();
$callback = $this->createCallback($callback);

$this->dispatcher->dispatch(JobEvents::START, new JobEvent($this, $data));
$this->setup($data);
}

/**
* @param JobDataInterface $data
*/
private function cleanRun(JobDataInterface $data)
{
$this->dispatcher->dispatch(JobEvents::STOP, new JobEvent($this, $data));
$this->cleanup();
}
}

0 comments on commit 2b199e8

Please sign in to comment.