it-swarm-pt.tech

Laravel 5.8 Como obter o ID do trabalho?

Estou tentando obter o ID do trabalho dentro dos meus trabalhos. Eu tento $this->job->getJobId(), mas ele retorna uma string vazia.

<?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
    }
}
7
Kenneth

Acabei de encontrar esta resposta e parece ainda ser compatível com a versão 5.8!

Arquivo de rotas

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.";
});

Arquivo de classe 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);
        }
    }
}

No navegador

Real database ID column!


AVISO: Parece que não pode implementar atrasos. É acionado sempre que você chama.

    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)"}

0
fernandojmartin