BrokerPushLevelRecordItemRollBackJob.php 1.72 KB
<?php

namespace App\Jobs;

use App\Enums\BrokerPushLevelRecordStatusEnum;
use App\Helpers\IMHelper;
use App\Models\BrokerPushLevelRecordItem;
use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Redis;
use Throwable;

class BrokerPushLevelRecordItemRollBackJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Batchable;

    public BrokerPushLevelRecordItem $recordItem;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(BrokerPushLevelRecordItem $recordItem)
    {
        $this->recordItem = $recordItem;
        $this->queue = 'im_rollback';
    }

    /**
     * Execute the job.
     *
     * @return void
     * @throws \Exception
     */
    public function handle(): void
    {
        Redis::throttle('im-rollback')->allow(200)->every(1)->then(function () {
            $this->recordItem->update(['status' => BrokerPushLevelRecordStatusEnum::PROCESSING]);
            $result = IMHelper::rollbackChatMessage(IMHelper::SYSTEM_USER, $this->recordItem->getAttribute('user_id'), $this->recordItem->getAttribute('message_id'))->json('ActionStatus');
            $this->recordItem->update(['status' => $result === 'OK' ? BrokerPushLevelRecordStatusEnum::ROLLBACK : BrokerPushLevelRecordStatusEnum::SUCCESS]);
        }, function () {
            $this->release(1);
        });
    }

    public function failed(Throwable $exception): void
    {
        $this->recordItem->update(['status' => BrokerPushLevelRecordStatusEnum::SUCCESS]);
    }
}