feat: 优化 Task2 类的用户在线状态处理,增加异常捕获和日志记录
This commit is contained in:
parent
df54ad41aa
commit
73d6a5f190
|
@ -16,67 +16,60 @@ class Task2
|
|||
{
|
||||
public function onWorkerStart()
|
||||
{
|
||||
|
||||
// // 每5秒执行一次
|
||||
// 每5秒执行一次
|
||||
new Crontab('0 */1 * * * *', function () {
|
||||
$phones = UserPhone::all();
|
||||
$phones = UserPhone::where('status' ,'!=', 2)->get();
|
||||
$start_time = time();
|
||||
|
||||
foreach ($phones as $key => $value) {
|
||||
$currentTimestamp = time();
|
||||
$status = SendCode::get_ws_status($value->phone);
|
||||
// 如果是挂机状态
|
||||
if ($status == 0) {
|
||||
// 如果在线时长超过 3600 秒
|
||||
if ($value->time > 3600) {
|
||||
// 计算超过 1 小时的完整小时数
|
||||
$hours = intdiv($value->time, 3600);
|
||||
|
||||
// 根据每小时 20 分积分计算总积分
|
||||
$score = $hours * 20;
|
||||
|
||||
// 计算剩余的秒数
|
||||
$remainingTime = $value->time % 3600;
|
||||
// var_dump($remainingTime);
|
||||
// 更新用户的积分
|
||||
$value->score += $score;
|
||||
$value->day_score += $score;
|
||||
// 更新用户的在线时长为剩余时间
|
||||
$value->time = $remainingTime;
|
||||
|
||||
// 保存用户收益到 UserReward 表
|
||||
UserRewardDao::Onhookincome($value->user_id, $score,$value->phone);
|
||||
|
||||
try {
|
||||
// 获取用户的在线状态
|
||||
$status = SendCode::get_ws_status($value->phone);
|
||||
$currentTimestamp = time();
|
||||
switch ($status) {
|
||||
case 0: // 在线状态
|
||||
if ($value->time > 3600) {
|
||||
// 计算超过 1 小时的积分
|
||||
$hours = intdiv($value->time, 3600);
|
||||
$score = $hours * 20;
|
||||
$remainingTime = $value->time % 3600;
|
||||
|
||||
// 更新用户积分和时长
|
||||
$value->score += $score;
|
||||
$value->day_score += $score;
|
||||
$value->time = $remainingTime;
|
||||
|
||||
// 保存用户收益到 UserReward 表
|
||||
UserRewardDao::Onhookincome($value->user_id, $score, $value->phone);
|
||||
// 保存在线时长记录
|
||||
UserPhoneLogDao::setOnlineTimeByPhone($value->phone, $status, $currentTimestamp);
|
||||
}
|
||||
|
||||
// 更新在线时长
|
||||
$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->save();
|
||||
|
||||
// 更新状态为在线
|
||||
$value->status = 1;
|
||||
$value->save();
|
||||
UserPhoneLogDao::setOnlineTimeByPhone($value->phone, $status, $currentTimestamp);
|
||||
|
||||
|
||||
|
||||
} catch (\Exception $e) {
|
||||
// 记录错误日志,避免任务中断
|
||||
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