php - Laravel - 将函数的一部分转换为排队语句

标签 php laravel laravel-5 task-queue

我想在我的 Controller 中对部分功能进行排队,主要是因为它访问第 3 方 API 并根据所述请求计算某些信息。我也想通过这个来增加对队列的了解!

我要排队的代码是:

唯一需要使用此 if 语句 推送的变量是 $postcode$clinic ID(在语句上方计算) ).

if($clinic->postcode != $postcode)
    {

     $client = new Client([ 'base_uri' => 'https://api.postcodes.io/','timeout' => 2.0, 'verify' => false ]);
     $response = $client->get('postcodes/'.$postcode)->getBody();

     $input = json_decode($response);

     $clinic->latitude   = $input->result->latitude;
     $clinic->longitude  = $input->result->longitude;
     $clinic->save();
}

到目前为止,我已经创建了 queue 表并进行了迁移。

然后我运行命令:php artisan make:job GetClinicLatAndLongPoints --queued

我的问题是,如何将此函数放入 GetClinicLatAndLongPoints 中,包括将两个变量传递过来?

我到目前为止:

public function handle(Clinic $clinic, $postcode)
    {

    }

但我不确定如何安排事情!任何指导将不胜感激。

最佳答案

您可以将 Clinic 模型的实例和邮政编码传递给作业的构造函数,这可能类似于

namespace App\Jobs;

use App\Clinic;
use App\Jobs\Job;
use GuzzleHttp\Client;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;

class GetClinicLatAndLongPoints extends Job implements SelfHandling, ShouldQueue
{
    use InteractsWithQueue, SerializesModels;

    private $clinic;
    private $postcode;

    public function __construct(Clinic $clinic, $postcode)
    {
        $this->clinic = $clinic; // Laravel will automatically deserialize the model instance
        $this->postcode = $postcode;
    }

    public function handle()
    {
        $coordinates = $this->getCoordinates($this->postcode);

        if (! is_null($coordinates)) {
            // You may want to add error handling
            $this->clinic->latitude   = $coordinates['latitude'];
            $this->clinic->longitude  = $coordinates['longitude'];
            $this->clinic->save();
        }
    }

    private function getCoordinates($postcode)
    {
        $client = new Client(['base_uri' => 'https://api.postcodes.io/','timeout' => 2.0, 'verify' => false]);
        $response = json_decode($client->get('postcodes/'.$postcode)->getBody()->getContents());
        if (! $response || json_last_error() !== JSON_ERROR_NONE) {
            return null;
        }
        if ($response->status == 200 &&
            isset($response->result->latitude) &&
            isset($response->result->longitude)) {
            return [
                'latitude' => $response->result->latitude,
                'longitude' => $response->result->longitude
            ];
        }
        return null;
    }
}

在你的 Controller 中你调度你的工作

if ($clinic->postcode != $postcode) {
    $this->dispatch(new GetClinicLatAndLongPoints($clinic, $postcode));
}

旁注:尽管 Laravel 附带了数据库队列驱动程序,但在生产环境中使用它并不是一个好主意。最好使用 job queues 之一即 beanstalkd .

关于php - Laravel - 将函数的一部分转换为排队语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30988955/

相关文章:

php - 插入表单时如何删除 Word 标记废话?

php - 更新一对多关系条目而不触及未受影响的行

php - Laravel:动态查询按 api 排序

php - Blade 模板 : Can unprocessed tags have php echo inside?

mysql - 为什么我的 SQL 插入重复

php - 如何通过许可证 key 保护我的 php 应用程序?

php - 是否可以在 Laravel 的 HTML 电子邮件中包含图像

mysql - Laravel 5 查询生成器 - 将 "ON DUPLICATE KEY UPDATE"附加到 insert() 调用

Laravel Blade 自定义指令包含部分

php - 为什么会出现此mysql错误?