williamjulianvicary/laravel-job-response

为Laravel作业添加响应 - 允许您的应用程序等待已分发的作业的响应。

0.1.3 2020-06-11 16:58 UTC

This package is auto-updated.

Last update: 2024-09-12 02:27:57 UTC


README

Latest Version on Packagist Build Status Total Downloads

您是否需要运行一个Laravel作业(或多个作业),等待响应,然后使用该响应?这个包正是为此提供功能。

安装

您可以通过composer安装此包

composer require williamjulianvicary/laravel-job-response

要求

  • PHP >= 7.4
  • Laravel >= 7.0(虽然未在早期版本上测试,但可能可以工作)

使用方法

在您的Job中使用CanRespond特质并实现JobCanRespond合约。

<?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');
    }
}

然后在您的Service/Controller/elsewhere,等待作业的响应。

<?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";
            }
        }
    }
}   

响应

默认情况下,该包以三种方式响应

  • ResponseCollection - 当期望多个响应时,将返回包含Response和/或ExceptionResponse对象的ResponseCollection。
  • Response - 成功的响应对象。
  • ExceptionResponse - 当作业失败时,捕获异常并将其返回。

(可选) 异常处理

默认情况下,会创建一个包含$exceptionResponse->getException()方法的ExceptionResponse对象,允许您查看作业抛出的异常。然而,这可能导致一些额外的样板代码来处理这个问题,因此我们提供了一个可选的方法,可以重新抛出这些异常。

要启用此功能,请使用外观更新throwExceptionsOnFailures标志

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

现在,每次发出await时,如果作业遇到异常,将引发一个JobFailedException

<?php

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.
        }       
       
    }
}  

方法

<?php
// 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);

故障排除

在使用此包时,您可能会遇到Laravel的一些怪癖。

  • 当使用sync驱动程序运行时,异常将不会被捕获 - 这是因为Laravel本身不使用Sync驱动程序来捕获它们,并且我们的包无法捕获它们。如果您需要使用此驱动程序处理异常,请使用$job->fail($exception);代替。

测试

composer test

变更日志

请参阅CHANGELOG以获取有关最近更改的更多信息。

贡献

请参阅CONTRIBUTING以获取详细信息。

致谢

许可证

MIT许可证(MIT)。请参阅许可证文件以获取更多信息。