Files
cunkebao_v3/Server/application/common/TaskServer.php

94 lines
2.2 KiB
PHP
Raw Normal View History

2025-05-22 15:50:52 +08:00
<?php
namespace app\common;
use think\Db;
use think\facade\Log;
use Workerman\Lib\Timer;
use think\worker\Server;
2025-05-28 18:01:24 +08:00
use WeChatDeviceApi\Adapters\ChuKeBao\Adapter as ChuKeBaoAdapter;
2025-05-22 15:50:52 +08:00
class TaskServer extends Server
{
2025-08-08 17:04:47 +08:00
const PROCESS_COUNT = 5;
2025-05-22 15:50:52 +08:00
protected $socket = 'text://0.0.0.0:2980';
protected $option = [
2025-08-13 17:02:36 +08:00
'count' => self::PROCESS_COUNT,
'name' => 'ckb_task_server'
2025-05-22 15:50:52 +08:00
];
/**
* 当客户端的连接上发生错误时触发
* @param $connection
* @param $code
* @param $msg
*/
public function onError($connection, $code, $msg)
{
Log::record("error $code $msg");
}
2025-08-13 17:02:36 +08:00
public function onMessage($connection, $data)
{
}
2025-05-22 15:50:52 +08:00
2025-08-13 17:02:36 +08:00
public function onClose($connection)
{
}
2025-05-22 15:50:52 +08:00
2025-08-13 17:02:36 +08:00
public function onConnect($connection)
{
}
2025-05-22 15:50:52 +08:00
public function onWorkerStart($worker)
{
$current_worker_id = $worker->id;
$process_count_for_status_0 = self::PROCESS_COUNT - 1;
2025-05-28 18:01:24 +08:00
$adapter = new ChuKeBaoAdapter();
2025-06-27 16:43:43 +08:00
Log::info('Workerman进程' . $current_worker_id);
2025-08-08 17:04:47 +08:00
// 在一个进程里处理获客任务新是数据
if ($current_worker_id == 4) {
2025-08-13 17:02:36 +08:00
Timer::add(60, function () use ($adapter) {
2025-08-08 17:04:47 +08:00
$adapter->handleCustomerTaskNewUser();
});
}
2025-05-29 16:30:47 +08:00
// 在一个进程里处理获客任务添加后的相关逻辑
2025-08-08 17:04:47 +08:00
if ($current_worker_id == 3) {
2025-08-13 17:02:36 +08:00
Timer::add(60, function () use ($adapter) {
2025-05-29 16:30:47 +08:00
$adapter->handleCustomerTaskWithStatusIsCreated();
2025-05-28 18:01:24 +08:00
});
}
2025-10-14 17:45:03 +08:00
// 进程处理获客新任务
if ($current_worker_id == 2) {
2025-05-28 18:01:24 +08:00
Timer::add(1, function () use ($current_worker_id, $process_count_for_status_0, $adapter) {
2025-05-29 16:30:47 +08:00
$adapter->handleCustomerTaskWithStatusIsNew($current_worker_id, $process_count_for_status_0);
2025-05-28 18:01:24 +08:00
});
}
2025-05-29 16:30:47 +08:00
2025-11-04 17:48:51 +08:00
// 在一个进程里处理自动问候任务
if ($current_worker_id == 1) {
// 每60秒检查一次自动问候规则
Timer::add(60, function () use ($adapter) {
2025-11-28 15:46:21 +08:00
//$adapter->handleAutoGreetings();
2025-11-04 17:48:51 +08:00
});
}
2025-05-29 16:30:47 +08:00
// 更多其他后台任务
// ......
2025-08-13 17:02:36 +08:00
2025-05-22 15:50:52 +08:00
}
}