Laravel 5.8 如何获取工作ID?
Laravel 5.8 How to get the job Id?
我正在尝试在我的作业中获取作业 ID。我尝试 $this->job->getJobId()
但它 returns 是一个空字符串。
<?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
}
}
以下将允许您获取作业 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
如果您正在尝试 return 您的 controller/route 的 ID,由于 async/queued 的性质,您无法使用 async/queued 作业执行此操作。
刚刚找到 ,它似乎仍然兼容 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.";
});
测试队列class文件
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)"}
我正在尝试在我的作业中获取作业 ID。我尝试 $this->job->getJobId()
但它 returns 是一个空字符串。
<?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
}
}
以下将允许您获取作业 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
如果您正在尝试 return 您的 controller/route 的 ID,由于 async/queued 的性质,您无法使用 async/queued 作业执行此操作。
刚刚找到
路线文件
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.";
});
测试队列class文件
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)"}