PHP code example of williamjulianvicary / laravel-job-response

1. Go to this page and download the library: Download williamjulianvicary/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/ */

    

williamjulianvicary / laravel-job-response example snippets


use Williamjulianvicary\LaravelJobResponse\Facades\LaravelJobResponse;
[...]
LaravelJobResponse::throwExceptionsOnFailures(true);



namespace App\Services;
use Williamjulianvicary\LaravelJobResponse\Facades\LaravelJobResponse;
use Williamjulianvicary\LaravelJobResponse\Exceptions\JobFailedException;

class Service
{
    public function test()
    {
        $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($timeout = 10, $throwException = false);  

$job->respond($mixed); // Should be used within the handle() method of the job to respond appropriately.
$job->respondWithException(\Throwable); // If you override the failed() method, this method responds with an exception.

// Facade methods

// Await a response for the given job.
LaravelJobResponse::awaitResponse(JobCanRespond $job, $timeout=10);

// Await responses from the provided job array.
LaravelJobResponse::awaitResponses(array $jobs, $timeout=10);

// Change how exceptions are handled (see above).
LaravelJobResponse::throwExceptionOnFailure(false);
 php


namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Williamjulianvicary\LaravelJobResponse\CanRespond;
use Williamjulianvicary\LaravelJobResponse\Contracts\JobCanRespond;

class TestJob implements ShouldQueue, JobCanRespond
{
    use InteractsWithQueue, Queueable, Dispatchable, CanRespond;

    public function __construct()
    {

    }

    public function handle()
    {
        $this->respond('Success');
    }
}
 php


namespace App\Services;

class Service
{
    public function test()
    {
        $job = new TestJob();
        $response = $job->awaitResponse();
        
        // $response is an instance of Response or ExceptionResponse
        $data = $response->getData(); // 'Success'
        // or 
        $exception = $response; // JobFailedException
    }
}
 php


namespace App\Services;
namespace Williamjulianvicary\LaravelJobResponse\Facades\LaravelJobResponse;

class Service
{
    public function test()
    {
        $jobs = [new TestJob(), new TestJob()];
        $responses = LaravelJobResponse::awaitResponses($jobs); // ResponseCollection
        
        foreach ($responses as $response) {
            if ($response instanceof ExceptionResponse) {
                echo "Exception: " . $response->getMessage() . "\n";
            } else {
                echo "Response: " . $response->getData() . "\n";
            }
        }
    }
}