Last active
May 29, 2018 11:34
-
-
Save Insolita/8252332dcdfa7183065d7b9886c38fc5 to your computer and use it in GitHub Desktop.
Task App Article
This file contains hidden or 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
<?php | |
namespace App\Jobs; | |
class DummyJob implements ShouldQueue | |
{ | |
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | |
private $params; | |
public function __construct(array $params = []) | |
{ | |
$this->params = $params; | |
} | |
public function handle(DummyService $service) | |
{ | |
$service->dummyJobLogic( | |
$this->params['loop'], $this->params['delay'] | |
); | |
} | |
} |
This file contains hidden or 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
<?php | |
namespace App\Services; | |
use Exception; | |
use Illuminate\Support\Carbon; | |
use Psr\Log\LoggerInterface; | |
use function array_random; | |
use function sleep; | |
class DummyService | |
{ | |
/** | |
* @var \Psr\Log\LoggerInterface | |
*/ | |
private $logger; | |
public function __construct(LoggerInterface $logger) | |
{ | |
$this->logger = $logger; | |
} | |
/** | |
* Simulate complex logic with element of surprise | |
* | |
* @param int $loop | |
* @param int $delay | |
* | |
* @throws \Exception | |
*/ | |
public function dummyJobLogic(int $loop = 10, int $delay = 1) | |
{ | |
for ($i = 0; $i < $loop; $i++) { | |
$this->logger->info('Iteration #' . $i . '/' . $loop); | |
sleep($delay); | |
if (Carbon::now()->timestamp % 10 === 0) { | |
throw new Exception('Ooops! Random Fail'); | |
} | |
} | |
$this->logger->info(__METHOD__ . ' complete'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment