mirror of
https://github.com/kuaifan/dootask.git
synced 2026-02-27 20:30:32 +00:00
feat: 添加 ManticoreSyncTask 的去重功能,优化任务投递逻辑
This commit is contained in:
parent
fa84f92577
commit
a95f22bf42
@ -3,17 +3,46 @@
|
||||
namespace App\Observers;
|
||||
|
||||
use Hhxsv5\LaravelS\Swoole\Task\Task;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class AbstractObserver
|
||||
{
|
||||
/**
|
||||
* 任务去重窗口时间(秒)
|
||||
* 同一个 action+id 在此时间内只投递一次
|
||||
*/
|
||||
private const DEDUP_WINDOW = 10;
|
||||
|
||||
/**
|
||||
* 投递异步任务(带去重)
|
||||
*
|
||||
* @param $task
|
||||
* @return void
|
||||
*/
|
||||
public static function taskDeliver($task)
|
||||
{
|
||||
if (app()->bound('swoole')) {
|
||||
Task::deliver($task);
|
||||
if (!app()->bound('swoole')) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 对 ManticoreSyncTask 进行去重
|
||||
if ($task instanceof \App\Tasks\ManticoreSyncTask) {
|
||||
$action = $task->getAction();
|
||||
$dataId = $task->getDataId();
|
||||
|
||||
if ($action && $dataId) {
|
||||
$cacheKey = "manticore_task:{$action}:{$dataId}";
|
||||
|
||||
// 如果已有相同任务在等待,跳过本次投递
|
||||
if (Cache::has($cacheKey)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 标记任务已投递,? 秒后过期
|
||||
Cache::put($cacheKey, true, self::DEDUP_WINDOW);
|
||||
}
|
||||
}
|
||||
|
||||
Task::deliver($task);
|
||||
}
|
||||
}
|
||||
|
||||
@ -36,6 +36,38 @@ class ManticoreSyncTask extends AbstractTask
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取任务动作类型(用于去重)
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getAction(): ?string
|
||||
{
|
||||
return $this->action;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据ID(用于去重)
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public function getDataId(): ?int
|
||||
{
|
||||
if (!is_array($this->data)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 根据不同的 action 类型提取对应的 ID
|
||||
return $this->data['id']
|
||||
?? $this->data['userid']
|
||||
?? $this->data['file_id']
|
||||
?? $this->data['project_id']
|
||||
?? $this->data['task_id']
|
||||
?? $this->data['msg_id']
|
||||
?? $this->data['dialog_id']
|
||||
?? null;
|
||||
}
|
||||
|
||||
public function start()
|
||||
{
|
||||
if (!Apps::isInstalled("manticore")) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user