feat: 优化 Task2 类的用户在线状态处理,增加异常捕获和日志记录
This commit is contained in:
parent
df54ad41aa
commit
73d6a5f190
|
@ -16,67 +16,60 @@ class Task2
|
||||||
{
|
{
|
||||||
public function onWorkerStart()
|
public function onWorkerStart()
|
||||||
{
|
{
|
||||||
|
// 每5秒执行一次
|
||||||
// // 每5秒执行一次
|
|
||||||
new Crontab('0 */1 * * * *', function () {
|
new Crontab('0 */1 * * * *', function () {
|
||||||
$phones = UserPhone::all();
|
$phones = UserPhone::where('status' ,'!=', 2)->get();
|
||||||
|
$start_time = time();
|
||||||
|
|
||||||
foreach ($phones as $key => $value) {
|
foreach ($phones as $key => $value) {
|
||||||
$currentTimestamp = time();
|
|
||||||
$status = SendCode::get_ws_status($value->phone);
|
try {
|
||||||
// 如果是挂机状态
|
// 获取用户的在线状态
|
||||||
if ($status == 0) {
|
$status = SendCode::get_ws_status($value->phone);
|
||||||
// 如果在线时长超过 3600 秒
|
$currentTimestamp = time();
|
||||||
if ($value->time > 3600) {
|
switch ($status) {
|
||||||
// 计算超过 1 小时的完整小时数
|
case 0: // 在线状态
|
||||||
$hours = intdiv($value->time, 3600);
|
if ($value->time > 3600) {
|
||||||
|
// 计算超过 1 小时的积分
|
||||||
// 根据每小时 20 分积分计算总积分
|
$hours = intdiv($value->time, 3600);
|
||||||
$score = $hours * 20;
|
$score = $hours * 20;
|
||||||
|
$remainingTime = $value->time % 3600;
|
||||||
// 计算剩余的秒数
|
|
||||||
$remainingTime = $value->time % 3600;
|
// 更新用户积分和时长
|
||||||
// var_dump($remainingTime);
|
$value->score += $score;
|
||||||
// 更新用户的积分
|
$value->day_score += $score;
|
||||||
$value->score += $score;
|
$value->time = $remainingTime;
|
||||||
$value->day_score += $score;
|
|
||||||
// 更新用户的在线时长为剩余时间
|
// 保存用户收益到 UserReward 表
|
||||||
$value->time = $remainingTime;
|
UserRewardDao::Onhookincome($value->user_id, $score, $value->phone);
|
||||||
|
// 保存在线时长记录
|
||||||
// 保存用户收益到 UserReward 表
|
UserPhoneLogDao::setOnlineTimeByPhone($value->phone, $status, $currentTimestamp);
|
||||||
UserRewardDao::Onhookincome($value->user_id, $score,$value->phone);
|
}
|
||||||
|
|
||||||
|
// 更新在线时长
|
||||||
|
$value->status=1;
|
||||||
|
$value->time += $currentTimestamp - $value->last_time;
|
||||||
|
break;
|
||||||
|
case 1: // 不在线状态
|
||||||
|
$value->status = 0;
|
||||||
|
break;
|
||||||
|
case 3: // 账户不存在
|
||||||
|
$value->status = 2;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算新的在线时长
|
// 更新用户最后活动时间和状态
|
||||||
$value->time += $currentTimestamp - $value->last_time;
|
|
||||||
$value->last_time = $currentTimestamp;
|
$value->last_time = $currentTimestamp;
|
||||||
|
$value->save();
|
||||||
|
|
||||||
// 更新状态为在线
|
|
||||||
$value->status = 1;
|
} catch (\Exception $e) {
|
||||||
$value->save();
|
// 记录错误日志,避免任务中断
|
||||||
UserPhoneLogDao::setOnlineTimeByPhone($value->phone, $status, $currentTimestamp);
|
Log::error("Error processing phone {$value->phone}: " . $e->getMessage());
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
if ($status == 1) {
|
|
||||||
// 如果不在线,设置状态为 0
|
|
||||||
$value->status = 0;
|
|
||||||
$value->last_time = $currentTimestamp;
|
|
||||||
$value->save();
|
|
||||||
UserPhoneLogDao::setOnlineTimeByPhone($value->phone, $status, $currentTimestamp);
|
|
||||||
}
|
|
||||||
if ($status == 3) {
|
|
||||||
// 如果账户不存在直接删除
|
|
||||||
SendCode::send_code($value->phone);
|
|
||||||
// $value->delete();
|
|
||||||
$value->status = 0;
|
|
||||||
$value->last_time = $currentTimestamp;
|
|
||||||
$value->save();
|
|
||||||
}
|
|
||||||
// UserPhoneLogDao::setOnlineTimeByPhone($value->phone, $status, $currentTimestamp);
|
|
||||||
// $value->last_time = $currentTimestamp;
|
|
||||||
// // 最后保存数据
|
|
||||||
// $value->save();
|
|
||||||
}
|
}
|
||||||
|
$end_time = time();
|
||||||
|
Log::info("Task2: Processed " . count($phones) . " phones in " . ($end_time - $start_time) . " seconds");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue