Skip to content

Commit b3b7589

Browse files
committed
no message
1 parent 5aed9ce commit b3b7589

File tree

8 files changed

+72
-36
lines changed

8 files changed

+72
-36
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ vars.yaml
2929
laravels.conf
3030
laravels.pid
3131
README_LOCAL.md
32+
dootask.lock

app/Console/Commands/SyncUserMsgToZincSearch.php

+59-24
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,64 @@ class SyncUserMsgToZincSearch extends Command
2222
protected $signature = 'zinc:sync-user-msg {--f} {--i} {--c} {--batch=1000}';
2323
protected $description = '同步聊天会话用户和消息到 ZincSearch';
2424

25+
/**
26+
* 缓存锁实例
27+
*/
28+
protected $lock;
29+
2530
/**
2631
* @return int
2732
*/
2833
public function handle(): int
2934
{
35+
// 注册信号处理器(仅在支持pcntl扩展的环境下)
36+
if (extension_loaded('pcntl')) {
37+
pcntl_async_signals(true); // 启用异步信号处理
38+
pcntl_signal(SIGINT, [$this, 'handleSignal']); // Ctrl+C
39+
pcntl_signal(SIGTERM, [$this, 'handleSignal']); // kill
40+
}
41+
3042
// 使用缓存锁确保一次只能运行一个实例
31-
$lock = Cache::lock('zinc:sync-user-msg', 3600 * 6); // 锁定6小时
32-
if (!$lock->get()) {
43+
$this->lock = Cache::lock('zinc:sync-user-msg', 3600 * 6); // 锁定6小时
44+
if (!$this->lock->get()) {
3345
$this->error('命令已在运行中,请等待当前实例完成');
3446
return 1;
3547
}
3648

37-
try {
38-
// 清除索引
39-
if ($this->option('c')) {
40-
$this->info('清除索引...');
41-
ZincSearchKeyValue::clear();
42-
ZincSearchDialogMsg::clear();
43-
$this->info("索引删除成功");
44-
return 0;
45-
}
49+
// 清除索引
50+
if ($this->option('c')) {
51+
$this->info('清除索引...');
52+
ZincSearchKeyValue::clear();
53+
ZincSearchDialogMsg::clear();
54+
$this->info("索引删除成功");
55+
$this->lock?->release();
56+
return 0;
57+
}
4658

47-
$this->info('开始同步聊天数据...');
59+
$this->info('开始同步聊天数据...');
4860

49-
// 同步消息数据
50-
$this->syncDialogMsgs();
61+
// 同步消息数据
62+
$this->syncDialogMsgs();
5163

52-
// 完成
53-
$this->info("\n同步完成");
54-
return 0;
55-
} finally {
56-
// 确保无论如何都会释放锁
57-
$lock->release();
64+
// 完成
65+
$this->info("\n同步完成");
66+
$this->lock?->release();
67+
return 0;
68+
}
69+
70+
/**
71+
* 处理终端信号
72+
*
73+
* @param int $signal
74+
* @return void
75+
*/
76+
public function handleSignal(int $signal): void
77+
{
78+
// 释放锁
79+
if ($this->lock) {
80+
$this->lock->release();
5881
}
82+
exit(0);
5983
}
6084

6185
/**
@@ -65,16 +89,23 @@ public function handle(): int
6589
*/
6690
private function syncDialogMsgs(): void
6791
{
68-
$this->info("\n同步消息数据...");
69-
7092
// 获取上次同步的最后ID
7193
$lastKey = "sync:dialogUserMsgLastId";
7294
$lastId = $this->option('i') ? intval(ZincSearchKeyValue::get($lastKey, 0)) : 0;
7395

96+
if ($lastId > 0) {
97+
$this->info("\n同步消息数据({$lastId})...");
98+
} else {
99+
$this->info("\n同步消息数据...");
100+
}
101+
74102
$num = 0;
75103
$count = WebSocketDialogMsg::where('id', '>', $lastId)->count();
76104
$batchSize = $this->option('batch');
77105

106+
$total = 0;
107+
$lastNum = 0;
108+
78109
do {
79110
// 获取一批
80111
$dialogMsgs = WebSocketDialogMsg::where('id', '>', $lastId)
@@ -88,10 +119,14 @@ private function syncDialogMsgs(): void
88119

89120
$num += count($dialogMsgs);
90121
$progress = round($num / $count * 100, 2);
91-
$this->info("{$num}/{$count} ({$progress}%) 正在同步消息ID {$lastId} ~ {$dialogMsgs->last()->id}");
122+
if ($progress < 100) {
123+
$progress = number_format($progress, 2);
124+
}
125+
$this->info("{$num}/{$count} ({$progress}%) 正在同步消息ID {$dialogMsgs->first()->id} ~ {$dialogMsgs->last()->id} ({$total}|{$lastNum})");
92126

93127
// 同步数据
94-
ZincSearchDialogMsg::batchSync($dialogMsgs);
128+
$lastNum = ZincSearchDialogMsg::batchSync($dialogMsgs);
129+
$total += $lastNum;
95130

96131
// 更新最后ID
97132
$lastId = $dialogMsgs->last()->id;

app/Http/Controllers/IndexController.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
use App\Tasks\DeleteBotMsgTask;
2424
use App\Tasks\CheckinRemindTask;
2525
use App\Tasks\CloseMeetingRoomTask;
26-
use App\Tasks\ElasticSearchSyncTask;
26+
use App\Tasks\ZincSearchSyncTask;
2727
use App\Tasks\UnclaimedTaskRemindTask;
2828
use Hhxsv5\LaravelS\Swoole\Task\Task;
2929
use Laravolt\Avatar\Avatar;
@@ -259,8 +259,8 @@ public function crontab()
259259
Task::deliver(new UnclaimedTaskRemindTask());
260260
// 关闭会议室
261261
Task::deliver(new CloseMeetingRoomTask());
262-
// ElasticSearch 同步
263-
Task::deliver(new ElasticSearchSyncTask());
262+
// ZincSearch 同步
263+
Task::deliver(new ZincSearchSyncTask());
264264

265265
return "success";
266266
}

app/Module/ZincSearch/ZincSearchDialogMsg.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ public static function batchSync($dialogMsgs): int
387387
}
388388
if ($dialogMsg->bot) {
389389
// 如果是机器人消息,跳过
390-
return true;
390+
continue;
391391
}
392392
/** @var WebSocketDialogUser $dialogUser */
393393
foreach ($userDialogs[$dialogMsg->dialog_id] as $dialogUser) {

cmd

+2-4
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,6 @@ if [ $# -gt 0 ]; then
457457
sleep 3
458458
fi
459459
done
460-
# 设置ES索引后缀
461-
env_set ES_INDEX_SUFFIX "$(rand_string 6)"
462460
# 启动容器
463461
[[ "$(arg_get port)" -gt 0 ]] && env_set APP_PORT "$(arg_get port)"
464462
$COMPOSE up php -d
@@ -479,7 +477,7 @@ if [ $# -gt 0 ]; then
479477
# 数据库迁移
480478
run_exec php "php artisan migrate --seed"
481479
# 启动其他容器
482-
$COMPOSE up -d
480+
$COMPOSE up -d --remove-orphans
483481
success "安装完成"
484482
info "地址: http://${GreenBG}127.0.0.1:$(env_get APP_PORT)${Font}"
485483
# 设置初始化密码
@@ -504,7 +502,7 @@ if [ $# -gt 0 ]; then
504502
run_exec php "php artisan migrate"
505503
run_exec nginx "nginx -s reload"
506504
restart_php
507-
$COMPOSE up -d
505+
$COMPOSE up -d --remove-orphans
508506
elif [[ "$1" == "uninstall" ]]; then
509507
shift 1
510508
read -rp "确定要卸载(含:删除容器、数据库、日志)吗?(Y/n): " uninstall

docker-compose.yml

+6
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ services:
111111
KK_OFFICE_PREVIEW_SWITCH_DISABLED: true
112112
KK_FILE_UPLOAD_ENABLED: true
113113
KK_MEDIA: "mp3,wav,mp4,mov,avi,wmv"
114+
ES_JAVA_OPTS: "-Xmx1g"
114115
networks:
115116
extnetwork:
116117
ipv4_address: "${APP_IPPR}.7"
@@ -229,6 +230,11 @@ services:
229230
ZINC_DATA_PATH: "/data"
230231
ZINC_FIRST_ADMIN_USER: "${DB_USERNAME}"
231232
ZINC_FIRST_ADMIN_PASSWORD: "${DB_PASSWORD}"
233+
deploy:
234+
resources:
235+
limits:
236+
cpus: '1'
237+
memory: 1G
232238
networks:
233239
extnetwork:
234240
ipv4_address: "${APP_IPPR}.15"

docker/es/config/elasticsearch.yml

-2
This file was deleted.

docker/es/data/.gitignore

-2
This file was deleted.

0 commit comments

Comments
 (0)