You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
159 lines
3.8 KiB
159 lines
3.8 KiB
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Dictionary\SendMessage\Type as MessageType;
|
|
use App\Dictionary\SendMessage\Status as MessageStatus;
|
|
use App\Jobs\Message\SubscriptionMessage;
|
|
use App\Models\SendMessageJob;
|
|
use Exception;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Cache\Repository;
|
|
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Throwable;
|
|
|
|
class SendWeChatMessageJob implements ShouldQueue, ShouldBeUnique
|
|
{
|
|
use Dispatchable;
|
|
use InteractsWithQueue;
|
|
use Queueable;
|
|
use SerializesModels;
|
|
|
|
/**
|
|
* PushWechatMessage Model.
|
|
*
|
|
* @var SendMessageJob
|
|
*/
|
|
protected SendMessageJob $message;
|
|
|
|
/**
|
|
* Message Type.
|
|
*
|
|
* @var MessageType
|
|
*/
|
|
protected MessageType $message_type;
|
|
|
|
/**
|
|
* 任务可尝试次数.
|
|
*
|
|
* @var int
|
|
*/
|
|
public int $tries = 3;
|
|
|
|
/**
|
|
* 任务的唯一 ID。
|
|
*
|
|
* @return string
|
|
*/
|
|
public function uniqueId(): string
|
|
{
|
|
return 'Queue:SendWechatMessage:'. $this->message->id;
|
|
}
|
|
|
|
/**
|
|
* 获取唯一任务锁的缓存驱动程序。
|
|
*
|
|
* @return Repository
|
|
*/
|
|
public function uniqueVia(): Repository
|
|
{
|
|
return Cache::driver('redis');
|
|
}
|
|
|
|
/**
|
|
* Send WeChat Message Job Construct.
|
|
*
|
|
* @param SendMessageJob $message
|
|
*/
|
|
public function __construct(SendMessageJob $message)
|
|
{
|
|
$this->message = $message->withoutRelations();
|
|
$this->message_type = MessageType::from($this->message->type);
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*/
|
|
public function handle(): void
|
|
{
|
|
$content = json_decode($this->message->content, true);
|
|
|
|
try {
|
|
$response = match ($this->message_type) {
|
|
MessageType::TEMPLATE,
|
|
MessageType::SINGLE_SUBSCRIBE,
|
|
MessageType::SUBSCRIBE,
|
|
MessageType::CUSTOM => (new SubscriptionMessage())->send($this->message_type, $content),
|
|
};
|
|
|
|
$data = $response->toArray(false);
|
|
|
|
if ($response->isSuccessful()) {
|
|
$this->successful((string)$data['errcode']);
|
|
} else {
|
|
$this->retry((string)$data['errmsg']);
|
|
}
|
|
|
|
} catch (Exception|Throwable $e) {
|
|
$message = $e->getMessage().' in '.$e->getFile().':'.$e->getLine();
|
|
Log::channel('SendWeChatMessage')->info('Push WeChat Message Error', [$this->message->id, $message]);
|
|
|
|
$this->retry($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Handle a job successful.
|
|
*
|
|
* @param string $msg_id
|
|
*
|
|
* @return void
|
|
*/
|
|
public function successful(string $msg_id): void
|
|
{
|
|
$this->message->status = MessageStatus::SUCCESS->value;
|
|
$this->message->msg_id = $msg_id;
|
|
$this->message->sent_at = date('Y-m-d H:i:s');
|
|
$this->message->save();
|
|
}
|
|
|
|
/**
|
|
* Handle a job retry.
|
|
*
|
|
* @param string $fail_reason
|
|
* @return void
|
|
*/
|
|
public function retry(string $fail_reason): void
|
|
{
|
|
if ($this->message->number < 3) {
|
|
++$this->message->number;
|
|
$this->message->save();
|
|
|
|
$this->release(10);
|
|
} else {
|
|
$this->failed($fail_reason);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Handle a job failure.
|
|
*
|
|
* @param string $fail_reason
|
|
*
|
|
* @return void
|
|
*/
|
|
public function failed(string $fail_reason): void
|
|
{
|
|
$this->message->status = MessageStatus::FAILURE->value;
|
|
$this->message->fail_reason = $fail_reason;
|
|
$this->message->save();
|
|
}
|
|
}
|
|
|