1. Go to this page and download the library: Download ngmy/laravel-job-response library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ngmy / laravel-job-response example snippets
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Williamjulianvicary\LaravelJobResponse\CanRespond;
use Williamjulianvicary\LaravelJobResponse\Contracts\JobCanRespond;
class TestJob implements ShouldQueue, JobCanRespond
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, CanRespond;
public function handle(): void
{
$this->respond('Success');
}
}
namespace App\Services;
use App\Jobs\TestJob;
use Williamjulianvicary\LaravelJobResponse\ExceptionResponse;
use Williamjulianvicary\LaravelJobResponse\Response;
class Service
{
public function test(): void
{
$job = new TestJob();
/** @var ExceptionResponse|Response $response */
$response = $job->awaitResponse();
if ($response instanceof ExceptionResponse) {
echo $response->getMessage().PHP_EOL; // The exception message string thrown by the job.
} else {
echo $response->getData().PHP_EOL; // 'Success'
}
}
}
namespace App\Services;
use App\Jobs\TestJob;
use Williamjulianvicary\LaravelJobResponse\ExceptionResponse;
use Williamjulianvicary\LaravelJobResponse\Facades\LaravelJobResponse;
use Williamjulianvicary\LaravelJobResponse\Response;
use Williamjulianvicary\LaravelJobResponse\ResponseCollection;
class Service
{
public function test(): void
{
$jobs = [new TestJob(), new TestJob()];
/** @var ResponseCollection<array-key, ExceptionResponse|Response> $responses */
$responses = LaravelJobResponse::awaitResponses($jobs);
foreach ($responses as $response) {
if ($response instanceof ExceptionResponse) {
echo $response->getMessage().PHP_EOL;
} else {
echo $response->getData().PHP_EOL;
}
}
}
}
use Williamjulianvicary\LaravelJobResponse\Facades\LaravelJobResponse;
LaravelJobResponse::throwExceptionOnFailure(true);
namespace App\Services;
use App\Jobs\TestJob;
use Williamjulianvicary\LaravelJobResponse\Exceptions\JobFailedException;
use Williamjulianvicary\LaravelJobResponse\Facades\LaravelJobResponse;
class Service
{
public function test(): void
{
$jobs = [new TestJob(), new TestJob()];
try {
$responses = LaravelJobResponse::awaitResponses($jobs);
} catch (JobFailedException $exception) {
// One of the jobs failed.
$exception->getTrace(); // The exception trace string thrown by the job.
}
}
}
// Methods available on your jobs
// Await a response for this job, optionally accepts a timeout and bool whether a exception should be raised if the job fails.
// Responds with either Response or ExceptionResponse objects.
$job->awaitResponse(int $timeout = 10, bool $throwException = false): ExceptionResponse|Response;
// Should be used within the handle() method of the job to respond appropriately.
$job->respond(mixed $data): void;
// If you override the failed() method, this method responds with an exception.
$job->respondWithException(?\Throwable $exception = null): void;
// Facade methods
// Await a response for the given job.
LaravelJobResponse::awaitResponse(JobCanRespond $job, int $timeout = 10): ExceptionResponse|Response;
// Await responses from the provided job array.
LaravelJobResponse::awaitResponses(JobCanRespond[] $jobs, int $timeout = 10): ResponseCollection<array-key, ExceptionResponse|Response>;
// Change how exceptions are handled (see above).
LaravelJobResponse::throwExceptionOnFailure(bool $flag = false): self;
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.