ジョブ内でジョブ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
}
}
ただ見つかった この答え で、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)"}