ps -fe | grep ‘artisan queue’ |grep -v ‘grep’ | wc -l
ps -fe | grep ‘artisan queue’ |grep -v ‘grep’
grep -v grep 代表在查询的最终结果中去掉grep命令本身
wc -l 标示统计查询到的结果数量
在使用队列的时候,
使用
$this->release(15)
指的是
*将任务释放回队列。
*接受以秒为单位指定的延迟。
上面的代码就是延迟了15秒。
出现死锁情况的方案,可能是排它锁的原因,解决方案
https://learnku.com/laravel/t/46331
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class MyJob implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
public function handle()
{
// 处理队列任务
}
public function delete()
{
// 重写 delete 方法,执行自定义的删除逻辑
}
}
添加缓存锁的逻辑代码
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class MyJob implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
public function handle()
{
$jobId = $this->job->getJobId();
$cacheKey = "job_lock_{$jobId}";
// 判断缓存中是否存在该任务的锁
if (Cache::has($cacheKey)) {
// 如果存在锁,说明任务正在被处理,直接退出
return;
}
// 在缓存中设置该任务的锁,有效期为 60 秒
Cache::put($cacheKey, true, 60);
try {
// 处理队列任务
} finally {
// 在任务执行完成后,删除该任务的锁
Cache::forget($cacheKey);
}
}
}
共通类的使用方法
use Illuminate\Cache\CacheManager;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class BaseJob implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
protected $cache;
public function __construct(CacheManager $cache)
{
$this->cache = $cache;
}
public function handle()
{
$jobId = $this->job->getJobId();
$cacheKey = "job_lock_{$jobId}";
// 判断缓存中是否存在该任务的锁
if ($this->cache->has($cacheKey)) {
// 如果存在锁,说明任务正在被处理,直接退出
return;
}
// 在缓存中设置该任务的锁,有效期为 60 秒
$this->cache->put($cacheKey, true, 60);
try {
// 处理队列任务
$this->handleJob();
} finally {
// 在任务执行完成后,删除该任务的锁
$this->cache->forget($cacheKey);
}
}
protected function handleJob()
{
// 子类继承该方法来实现具体的任务逻辑
}
}
参考资料:
队列的laravel 文档