server/app/Job/SendChannelJob.php

51 lines
1.5 KiB
PHP

<?php
namespace App\Job;
use App\Service\Message\ChannelFactory;
use App\Utils\Log;
use Hyperf\AsyncQueue\Job;
use App\Model\Message as mModel;
use App\Model\Account as aModel;
use App\Model\MessageChannel as mchModel;
use function Hyperf\Support\make;
class SendChannelJob extends Job
{
protected int $maxAttempts = 2;
protected ChannelFactory $factory;
protected int $userId;
protected int $messageId;
protected string $channel;
public function __construct($params)
{
$this->userId = $params['userId'];
$this->messageId = $params['messageId'];
$this->channel = $params['channel'];
}
public function handle()
{
try {
$channel = make(ChannelFactory::class)->get($this->channel);
$account = aModel::find($this->userId);
$message = mModel::find($this->messageId);
$channel->send($account, $message);
mchModel::query()
->where('message_id', $this->messageId)
->where('account_id', $this->userId)
->where('channel', $this->channel)
->update(['status' => 1]);
} catch (\Exception $e) {
Log::record("消息投递失败:" . $e->getMessage(), "SendChannelJob");
mchModel::query()
->where('message_id', $this->messageId)
->where('account_id', $this->userId)
->where('channel', $this->channel)
->update(['status' => 2, 'fail_reason' => $e->getMessage()]);
}
}
}