no message

This commit is contained in:
kuaifan 2025-04-18 19:25:13 +08:00
parent b3b7589db3
commit 7957353c3f

View File

@ -22,11 +22,6 @@ class SyncUserMsgToZincSearch extends Command
protected $signature = 'zinc:sync-user-msg {--f} {--i} {--c} {--batch=1000}'; protected $signature = 'zinc:sync-user-msg {--f} {--i} {--c} {--batch=1000}';
protected $description = '同步聊天会话用户和消息到 ZincSearch'; protected $description = '同步聊天会话用户和消息到 ZincSearch';
/**
* 缓存锁实例
*/
protected $lock;
/** /**
* @return int * @return int
*/ */
@ -39,20 +34,23 @@ class SyncUserMsgToZincSearch extends Command
pcntl_signal(SIGTERM, [$this, 'handleSignal']); // kill pcntl_signal(SIGTERM, [$this, 'handleSignal']); // kill
} }
// 使用缓存锁确保一次只能运行一个实例 // 检查锁,如果已被占用则退出
$this->lock = Cache::lock('zinc:sync-user-msg', 3600 * 6); // 锁定6小时 $lockInfo = $this->getLock();
if (!$this->lock->get()) { if ($lockInfo) {
$this->error('命令已在运行中,请等待当前实例完成'); $this->error("命令已在运行中,开始时间: {$lockInfo['started_at']}");
return 1; return 1;
} }
// 设置锁
$this->setLock();
// 清除索引 // 清除索引
if ($this->option('c')) { if ($this->option('c')) {
$this->info('清除索引...'); $this->info('清除索引...');
ZincSearchKeyValue::clear(); ZincSearchKeyValue::clear();
ZincSearchDialogMsg::clear(); ZincSearchDialogMsg::clear();
$this->info("索引删除成功"); $this->info("索引删除成功");
$this->lock?->release(); $this->releaseLock();
return 0; return 0;
} }
@ -63,10 +61,42 @@ class SyncUserMsgToZincSearch extends Command
// 完成 // 完成
$this->info("\n同步完成"); $this->info("\n同步完成");
$this->lock?->release(); $this->releaseLock();
return 0; return 0;
} }
/**
* 获取锁信息
*
* @return array|null 如果锁存在返回锁信息否则返回null
*/
private function getLock(): ?array
{
$lockKey = md5($this->signature);
return Cache::has($lockKey) ? Cache::get($lockKey) : null;
}
/**
* 设置锁
*/
private function setLock(): void
{
$lockKey = md5($this->signature);
$lockInfo = [
'started_at' => date('Y-m-d H:i:s')
];
Cache::put($lockKey, $lockInfo, 300); // 5分钟
}
/**
* 释放锁
*/
private function releaseLock(): void
{
$lockKey = md5($this->signature);
Cache::forget($lockKey);
}
/** /**
* 处理终端信号 * 处理终端信号
* *
@ -76,9 +106,7 @@ class SyncUserMsgToZincSearch extends Command
public function handleSignal(int $signal): void public function handleSignal(int $signal): void
{ {
// 释放锁 // 释放锁
if ($this->lock) { $this->releaseLock();
$this->lock->release();
}
exit(0); exit(0);
} }
@ -124,6 +152,9 @@ class SyncUserMsgToZincSearch extends Command
} }
$this->info("{$num}/{$count} ({$progress}%) 正在同步消息ID {$dialogMsgs->first()->id} ~ {$dialogMsgs->last()->id} ({$total}|{$lastNum})"); $this->info("{$num}/{$count} ({$progress}%) 正在同步消息ID {$dialogMsgs->first()->id} ~ {$dialogMsgs->last()->id} ({$total}|{$lastNum})");
// 刷新锁
$this->setLock();
// 同步数据 // 同步数据
$lastNum = ZincSearchDialogMsg::batchSync($dialogMsgs); $lastNum = ZincSearchDialogMsg::batchSync($dialogMsgs);
$total += $lastNum; $total += $lastNum;