-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Skytells, Inc
committed
Dec 30, 2017
0 parents
commit 13be9dc
Showing
6 changed files
with
845 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
namespace Skytells\Events; | ||
|
||
use Skytells\Contracts\Queue\Job; | ||
use Skytells\Queue\InteractsWithQueue; | ||
use Skytells\Contracts\Container\Container; | ||
|
||
class CallQueuedHandler | ||
{ | ||
/** | ||
* The container instance. | ||
* | ||
* @var \Skytells\Contracts\Container\Container | ||
*/ | ||
protected $container; | ||
|
||
/** | ||
* Create a new job instance. | ||
* | ||
* @param \Skytells\Contracts\Container\Container $container | ||
* @return void | ||
*/ | ||
public function __construct(Container $container) | ||
{ | ||
$this->container = $container; | ||
} | ||
|
||
/** | ||
* Handle the queued job. | ||
* | ||
* @param \Skytells\Contracts\Queue\Job $job | ||
* @param array $data | ||
* @return void | ||
*/ | ||
public function call(Job $job, array $data) | ||
{ | ||
$handler = $this->setJobInstanceIfNecessary( | ||
$job, $this->container->make($data['class']) | ||
); | ||
|
||
call_user_func_array( | ||
[$handler, $data['method']], unserialize($data['data']) | ||
); | ||
|
||
if (! $job->isDeletedOrReleased()) { | ||
$job->delete(); | ||
} | ||
} | ||
|
||
/** | ||
* Set the job instance of the given class if necessary. | ||
* | ||
* @param \Skytells\Contracts\Queue\Job $job | ||
* @param mixed $instance | ||
* @return mixed | ||
*/ | ||
protected function setJobInstanceIfNecessary(Job $job, $instance) | ||
{ | ||
if (in_array(InteractsWithQueue::class, class_uses_recursive(get_class($instance)))) { | ||
$instance->setJob($job); | ||
} | ||
|
||
return $instance; | ||
} | ||
|
||
/** | ||
* Call the failed method on the job instance. | ||
* | ||
* The event instance and the exception will be passed. | ||
* | ||
* @param array $data | ||
* @param \Exception $e | ||
* @return void | ||
*/ | ||
public function failed(array $data, $e) | ||
{ | ||
$handler = $this->container->make($data['class']); | ||
|
||
$parameters = array_merge(unserialize($data['data']), [$e]); | ||
|
||
if (method_exists($handler, 'failed')) { | ||
call_user_func_array([$handler, 'failed'], $parameters); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<?php | ||
|
||
namespace Skytells\Events; | ||
|
||
use Skytells\Container\Container; | ||
use Skytells\Contracts\Queue\Job; | ||
use Skytells\Queue\InteractsWithQueue; | ||
use Skytells\Contracts\Queue\ShouldQueue; | ||
|
||
class CallQueuedListener implements ShouldQueue | ||
{ | ||
use InteractsWithQueue; | ||
|
||
/** | ||
* The listener class name. | ||
* | ||
* @var string | ||
*/ | ||
public $class; | ||
|
||
/** | ||
* The listener method. | ||
* | ||
* @var string | ||
*/ | ||
public $method; | ||
|
||
/** | ||
* The data to be passed to the listener. | ||
* | ||
* @var array | ||
*/ | ||
public $data; | ||
|
||
/** | ||
* The number of times the job may be attempted. | ||
* | ||
* @var int | ||
*/ | ||
public $tries; | ||
|
||
/** | ||
* The number of seconds the job can run before timing out. | ||
* | ||
* @var int | ||
*/ | ||
public $timeout; | ||
|
||
/** | ||
* Create a new job instance. | ||
* | ||
* @param string $class | ||
* @param string $method | ||
* @param array $data | ||
* @return void | ||
*/ | ||
public function __construct($class, $method, $data) | ||
{ | ||
$this->data = $data; | ||
$this->class = $class; | ||
$this->method = $method; | ||
} | ||
|
||
/** | ||
* Handle the queued job. | ||
* | ||
* @param \Skytells\Container\Container $container | ||
* @return void | ||
*/ | ||
public function handle(Container $container) | ||
{ | ||
$this->prepareData(); | ||
|
||
$handler = $this->setJobInstanceIfNecessary( | ||
$this->job, $container->make($this->class) | ||
); | ||
|
||
call_user_func_array( | ||
[$handler, $this->method], $this->data | ||
); | ||
} | ||
|
||
/** | ||
* Set the job instance of the given class if necessary. | ||
* | ||
* @param \Skytells\Contracts\Queue\Job $job | ||
* @param mixed $instance | ||
* @return mixed | ||
*/ | ||
protected function setJobInstanceIfNecessary(Job $job, $instance) | ||
{ | ||
if (in_array(InteractsWithQueue::class, class_uses_recursive(get_class($instance)))) { | ||
$instance->setJob($job); | ||
} | ||
|
||
return $instance; | ||
} | ||
|
||
/** | ||
* Call the failed method on the job instance. | ||
* | ||
* The event instance and the exception will be passed. | ||
* | ||
* @param \Exception $e | ||
* @return void | ||
*/ | ||
public function failed($e) | ||
{ | ||
$this->prepareData(); | ||
|
||
$handler = Container::getInstance()->make($this->class); | ||
|
||
$parameters = array_merge($this->data, [$e]); | ||
|
||
if (method_exists($handler, 'failed')) { | ||
call_user_func_array([$handler, 'failed'], $parameters); | ||
} | ||
} | ||
|
||
/** | ||
* Unserialize the data if needed. | ||
* | ||
* @return void | ||
*/ | ||
protected function prepareData() | ||
{ | ||
if (is_string($this->data)) { | ||
$this->data = unserialize($this->data); | ||
} | ||
} | ||
|
||
/** | ||
* Get the display name for the queued job. | ||
* | ||
* @return string | ||
*/ | ||
public function displayName() | ||
{ | ||
return $this->class; | ||
} | ||
} |
Oops, something went wrong.