【问题标题】:Laravel 5.8 How to get the job Id?Laravel 5.8 如何获得工作 ID?
【发布时间】:2019-09-21 15:52:04
【问题描述】:

我正在尝试在我的工作中获取工作 ID。我尝试$this->job->getJobId(),但它返回一个空字符串。

<?php

namespace App\Jobs\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Auth;

class SendNotification implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public function __construct($notification, $fireShutdown)
    {
        $this->notification = $notification;
        $this->fireShutdown = $fireShutdown;
    }

    public function handle()
    {
        dd($this->job->getJobId());

       // Some Code
    }
}

【问题讨论】:

    标签: php laravel laravel-5.6 laravel-5.7 laravel-5.8


    【解决方案1】:

    以下内容将允许您获取作业 ID。尝试复制下面的代码并使用简单的路由进行调度。

    class TestJob implements ShouldQueue
    {
        use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    
        /**
         * Execute the job.
         *
         * @return void
         */
        public function handle()
        {
            echo $this->job->getJobId();
        }
    }
    

    还有下面的路线来测试一下。

    Route::get('/trigger', function () {
        dd(dispatch(new \App\Jobs\TestJob()));
    });
    

    在您的终端中,您现在应该会看到以下内容,其中包含您给定工作的 ID。

    如果您的队列侦听器没有运行,您可以通过在终端中键入以下内容来启动它

    php artisan queue:work redis --tries=3
    

    如果您尝试将 id 返回到您的控制器/路由,则由于异步/排队的性质,您无法使用异步/排队作业执行此操作。

    【讨论】:

    • 您使用的是哪个队列驱动程序?您可以尝试在触发器中分派 10 多个 TestJob 吗?
    • 所有工作都一样。它对所有作业返回 0。
    • 修改代码后记得重启队列监听器。
    • @Kenneth 上面的代码没有错。您可以将其复制粘贴到新的 Laravel 存储库中,它会正确返回作业 ID。
    • @NicklasKevinFrank @kenneth Jeez!!刚刚找到this answer,试了一下,成功了!!这是我的行:dump(app(\Illuminate\Contracts\Bus\Dispatcher::class)-&gt;dispatch(new TestQueue($source))); 在浏览器中:#14
    【解决方案2】:

    刚找到this answer,好像在5.8上还是兼容的!

    路由文件

    Route::get('/queue/{count?}', function($count = 10) {
        $source = new stdClass;
        $source->count = $count;
    
        // dump(TestQueue::dispatch($source)->delay(now()->addSeconds(10)));
        dump(app(\Illuminate\Contracts\Bus\Dispatcher::class)->dispatch(new TestQueue($source)));
    
        return "Queued! Will loop {$source->count} times.";
    });
    

    TestQueue 类文件

    class TestQueue implements ShouldQueue
    {
        use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    
        protected $source;
    
        public function __construct(\stdClass $source)
        {
            $this->source = $source;
        }
    
        public function handle()
        {
            for ($i = 1; $i <= $this->source->count; $i++) {
                logger("Loop #{$i} of {$this->source->count}");
                sleep(1);
            }
        }
    }
    

    在浏览器中


    警告:看起来无法实现延迟。只要你调用它就会触发。

        dump(
            app(\Illuminate\Contracts\Bus\Dispatcher::class)
                ->dispatch(new TestQueue($source))
                ->delay(now()->addSeconds(10))
        );
    

    ERROR: Call to a member function delay() on integer {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Call to a member function delay() on integer at ...web.php:50)"}

    【讨论】:

      猜你喜欢
      • 2022-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-28
      • 2020-02-25
      • 1970-01-01
      • 2020-05-21
      • 1970-01-01
      相关资源
      最近更新 更多