EventServiceProvider.php 2.71 KB
<?php

namespace App\Providers;

use AlibabaCloud\SDK\Mts\V20140618\Models\QueryJobListRequest;
use App\Helpers\UploadHelper;
use App\Models\Activity;
use App\Models\GroupHasMember;
use App\Models\ProjectDynamic;
use App\Models\UserCertify;
use App\Models\UserDynamic;
use App\Observers\ActivityObserver;
use App\Observers\GroupMemberObserver;
use App\Observers\ProjectDynamicObserver;
use App\Observers\UserCertifyObserver;
use App\Observers\UserDynamicObserver;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Redis;
use Laravel\Octane\Facades\Octane;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event to listener mappings for the application.
     *
     * @var array<class-string, array<int, class-string>>
     */
    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
    ];

    /**
     * Register any events for your application.
     *
     * @return void
     */
    public function boot(): void
    {
        UserDynamic::observe(UserDynamicObserver::class);
        UserCertify::observe(UserCertifyObserver::class);
        ProjectDynamic::observe(ProjectDynamicObserver::class);
        Activity::observe(ActivityObserver::class);
        GroupHasMember::observe(GroupMemberObserver::class);

        Octane::tick('MediaTranscodeQuery', static function () {
            $jobIds = Redis::client()->sRandMember('media_transcode', 10);


            if (count($jobIds) !== 0) {
                $queryJobListRequest = new QueryJobListRequest(["jobIds" => implode(',', $jobIds)]);

                $data = UploadHelper::getMtsClient()->queryJobList($queryJobListRequest)->body;

                if ($data->nonExistJobIds) {
                    Redis::client()->sRem('media_transcode', $data->nonExistJobIds->string);
                }

                foreach ($data->jobList->job as $item) {
                    if ($item->state === 'TranscodeSuccess') {
                        Redis::client()->sRem('media_transcode', $item->jobId);
                    }

                    if ($data = json_decode($item->output->userData, true, 512, JSON_THROW_ON_ERROR)) {
                        $class = '\\' . ltrim('App\\Models\\' . $data['event_type'], '\\');
                        (new $class)->newQuery()->whereKey($data['event_id'])->update([
                            $data['event_attribute'] => UploadHelper::getOssDomain() . '/' . $item->output->outputFile->object
                        ]);
                    }
                }
            }
        })->seconds(10)->immediate();
    }
}