Skip to content

Instantly share code, notes, and snippets.

@Insolita
Last active May 29, 2018 11:34
Show Gist options
  • Save Insolita/8252332dcdfa7183065d7b9886c38fc5 to your computer and use it in GitHub Desktop.
Save Insolita/8252332dcdfa7183065d7b9886c38fc5 to your computer and use it in GitHub Desktop.
Task App Article
<?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']
);
}
}
<?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