it-swarm.dev

Laravel 5.8 Comment obtenir l'ID du travail?

J'essaie d'obtenir l'ID de l'emploi dans mes emplois. J'essaie $this->job->getJobId() mais il retourne une chaîne vide.

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

Je viens de trouver cette réponse et il semble toujours compatible sur 5.8!

Fichier de routes

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

Fichier 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);
        }
    }
}

Dans le navigateur

Real database ID column!


AVERTISSEMENT: Il semble impossible d'implémenter des retards. Il se déclenche à chaque fois que vous l'appelez.

    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