WorkchatMessageSend.php
2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
namespace App\Console\Commands\Task;
use App\Mail\TextMail;
use App\Models\Subscribe\GroupSendTask;
use App\Models\Subscribe\GroupSendTaskLog;
use App\Models\Subscribe\Smtp;
use App\Models\Workchat\MessagePush;
use App\Services\Aside\ProjectAssociation\ProjectAssociationServices;
use Illuminate\Console\Command;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Mail;
/**
* Class WorkchatMessageSend
* @package App\Console\Commands\Task
*/
class WorkchatMessageSend extends Command
{
protected $signature = 'workchat_message_send';
protected $description = '企微消息推送任务';
public function handle()
{
while (true) {
$tasks = MessagePush::where('status', MessagePush::STATUS_PENDING)
->where(function ($query) {
$query->whereNull('send_time')->orWhere('send_time', '<=', date('Y-m-d H:i:s'));
})
->get();
if (!$tasks->count()) {
sleep(5);
}
foreach ($tasks as $task) {
$this->output('开始推送消息' . $task->id);
try {
//超过两小时 不推送了
if(time() - strtotime($task->send_time) > 7200){
throw new \Exception('超时两小时未推送');
}
ProjectAssociationServices::getInstance()->sendMessage($task->friend_id, $task->content, $task->content_type);
$this->output('推送消息' . $task->id . '成功');
$task->status = MessagePush::STATUS_SUCCESS;
}catch (ConnectionException $e){
$this->output('推送消息' . $task->id . '超时');
if ($task->retry < 3) {
$task->status = MessagePush::STATUS_PENDING;
$task->retry = $task->retry + 1;
$task->send_time = date('Y-m-d H:i:s', strtotime('+ ' . $task->retry . ' minute'));
} else {
$task->status = MessagePush::STATUS_ERROR;
$task->remark = '请求超时';
}
} catch (\Exception $e) {
$this->output('推送消息' . $task->id . '失败:' . $e->getLine() . ' - ' . $e->getMessage());
$task->status = MessagePush::STATUS_ERROR;
$task->remark = mb_substr($e->getMessage(), 0, 200);
}
$task->save();
}
}
}
/**
* 输出处理日志
*/
public function output($message): bool
{
echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
return true;
}
}