-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: create queuable services (#5834)
- Loading branch information
Showing
9 changed files
with
274 additions
and
12 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
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
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,74 @@ | ||
<?php | ||
|
||
namespace App\Jobs; | ||
|
||
use Throwable; | ||
use App\Services\BaseService; | ||
use Illuminate\Bus\Queueable; | ||
use App\Services\QueuableService; | ||
use Illuminate\Queue\SerializesModels; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
|
||
class ServiceQueue implements ShouldQueue | ||
{ | ||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
/** | ||
* The service to queue. | ||
* | ||
* @var QueuableService | ||
*/ | ||
public $service; | ||
|
||
/** | ||
* The data to run service. | ||
* | ||
* @var array | ||
*/ | ||
public $data; | ||
|
||
/** | ||
* The number of times the job may be attempted. | ||
* | ||
* @var int | ||
*/ | ||
public $tries = 1; | ||
|
||
/** | ||
* Create a new job instance. | ||
* | ||
* @param BaseService $service | ||
* @param array|null $data | ||
*/ | ||
public function __construct(BaseService $service, array $data = null) | ||
{ | ||
if (! $service instanceof QueuableService) { | ||
throw new \Exception('Service is not queuable'); | ||
} | ||
$this->service = $service; | ||
$this->data = $data; | ||
} | ||
|
||
/** | ||
* Execute the job. | ||
* | ||
* @return void | ||
*/ | ||
public function handle(): void | ||
{ | ||
$this->service->handle($this->data); | ||
} | ||
|
||
/** | ||
* Handle a job failure. | ||
* | ||
* @param \Throwable $exception | ||
* @return void | ||
*/ | ||
public function failed(Throwable $exception): void | ||
{ | ||
$this->service->failed($exception); | ||
} | ||
} |
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
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,50 @@ | ||
<?php | ||
|
||
namespace App\Services; | ||
|
||
use Throwable; | ||
use App\Jobs\ServiceQueue; | ||
use Illuminate\Foundation\Bus\PendingDispatch; | ||
|
||
/** | ||
* This trait helps dispatch a QueuableService. | ||
*/ | ||
trait DispatchableService | ||
{ | ||
/** | ||
* Dispatch the service with the given arguments. | ||
* | ||
* @param mixed ...$arguments | ||
* @return \Illuminate\Foundation\Bus\PendingDispatch | ||
*/ | ||
public static function dispatch(...$arguments): PendingDispatch | ||
{ | ||
/** @var QueuableService $service */ | ||
$service = new self(); | ||
|
||
return ServiceQueue::dispatch($service, ...$arguments); | ||
} | ||
|
||
/** | ||
* Dispatch the service with the given arguments on the sync queue. | ||
* | ||
* @param mixed ...$arguments | ||
* @return mixed | ||
*/ | ||
public static function dispatchSync(...$arguments) | ||
{ | ||
/** @var QueuableService $service */ | ||
$service = new self(); | ||
|
||
return ServiceQueue::dispatchSync($service, ...$arguments); | ||
} | ||
|
||
/** | ||
* Handle a job failure. | ||
* | ||
* @param \Throwable $exception | ||
*/ | ||
public function failed(Throwable $exception): void | ||
{ | ||
} | ||
} |
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,27 @@ | ||
<?php | ||
|
||
namespace App\Services; | ||
|
||
use Throwable; | ||
|
||
/** | ||
* Makes a BaseService queuable using the generic ServiceQueue job. | ||
*/ | ||
interface QueuableService | ||
{ | ||
/** | ||
* Execute the service. | ||
* | ||
* @param array $data | ||
* @return void | ||
*/ | ||
public function handle(array $data): void; | ||
|
||
/** | ||
* Handle a job failure. | ||
* | ||
* @param \Throwable $exception | ||
* @return void | ||
*/ | ||
public function failed(Throwable $exception): void; | ||
} |
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,54 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\Jobs; | ||
|
||
use Tests\TestCase; | ||
use Illuminate\Foundation\Testing\DatabaseTransactions; | ||
|
||
class ServiceQueueTest extends TestCase | ||
{ | ||
use DatabaseTransactions; | ||
|
||
/** @test */ | ||
public function it_run_a_service_ok(): void | ||
{ | ||
config(['queue.default' => 'sync']); | ||
|
||
ServiceQueueTester::dispatch(); | ||
|
||
$this->assertTrue(ServiceQueueTester::$executed); | ||
$this->assertFalse(ServiceQueueTester::$failed); | ||
} | ||
|
||
/** @test */ | ||
public function it_run_a_service_sync(): void | ||
{ | ||
ServiceQueueTester::dispatchSync(); | ||
|
||
$this->assertTrue(ServiceQueueTester::$executed); | ||
$this->assertFalse(ServiceQueueTester::$failed); | ||
} | ||
|
||
/** @test */ | ||
public function it_run_a_service_which_failed(): void | ||
{ | ||
$this->expectException(\Exception::class); | ||
try { | ||
ServiceQueueTester::dispatchSync(['throw' => true]); | ||
} finally { | ||
$this->assertTrue(ServiceQueueTester::$executed); | ||
$this->assertTrue(ServiceQueueTester::$failed); | ||
} | ||
} | ||
|
||
/** @test */ | ||
public function service_is_not_run_if_queue_set(): void | ||
{ | ||
config(['queue.default' => 'database']); | ||
|
||
ServiceQueueTester::dispatch(['throw' => true]); | ||
|
||
$this->assertFalse(ServiceQueueTester::$executed); | ||
$this->assertFalse(ServiceQueueTester::$failed); | ||
} | ||
} |
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,55 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\Jobs; | ||
|
||
use Throwable; | ||
use App\Services\BaseService; | ||
use App\Services\QueuableService; | ||
use App\Services\DispatchableService; | ||
|
||
class ServiceQueueTester extends BaseService implements QueuableService | ||
{ | ||
use DispatchableService; | ||
|
||
public static bool $executed = false; | ||
public static bool $failed = false; | ||
|
||
public bool $object; | ||
|
||
/** | ||
* Initialize the service. | ||
* | ||
* @param array $data | ||
*/ | ||
public function __construct() | ||
{ | ||
self::$executed = false; | ||
self::$failed = false; | ||
} | ||
|
||
/** | ||
* Execute the service. | ||
*/ | ||
public function handle($data): void | ||
{ | ||
self::$executed = true; | ||
|
||
if ($data && $data['throw'] === true) { | ||
throw new \Exception(); | ||
} | ||
} | ||
|
||
/** | ||
* Handle a job failure. | ||
* | ||
* @param \Throwable $exception | ||
*/ | ||
public function failed(Throwable $exception): void | ||
{ | ||
self::$failed = true; | ||
|
||
if (isset($this->obj)) { | ||
// variable can be touch | ||
} | ||
} | ||
} |
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