美文网首页
laravel队列获取阿里云视频点播服务视频时长报错Undefi

laravel队列获取阿里云视频点播服务视频时长报错Undefi

作者: 甘伍觉莫 | 来源:发表于2018-03-30 21:05 被阅读357次

    在laravel项目中集成阿里云的视频点播服务时, 需要上传视频后获取视频时长,由于视频时长并不是能马上获取到的参数(应该是在阿里云视频转码之后才能得到), 什么时候能获取到并不确定, 因此考虑使用laravel的队列服务来延迟获取.

    <?php
    
    namespace App\Jobs;
    
    use App\Models\Video;
    use App\Service\AliVod;
    use Carbon\Carbon;
    use Illuminate\Bus\Queueable;
    
    /**
     * 上传后获取视频长度
     * Class GetVideoDuration
     * @package App\Jobs
     */
    class GetVideoDuration extends Job
    {
        use Queueable;
        protected $video;
    
        public function __construct($id)
        {
            $this->video = Video::find($id);
        }
    
        public function handle()
        {
            try {
                $video_info = AliVod::get_video_info($this->video->video_id);
                $duration = $video_info['Video']->Duration;
                if (0 != $duration) {
                    $this->video->update([
                        'duration' => $duration
                    ]);
                    \Log::info('获取视频时间成功, 视频表id  ==> ' . $this->video->id);
                } else {
                    \Log::info('视频时间为 0 , 视频表id  ==> ' . $this->video->id. ', 重新分发');
                    dispatch((new GetVideoDuration($this->video->id))->delay(Carbon::now()->addMinutes(3)));
                }
            } catch (\Exception $e) {
                \Log::info('获取视频时间失败, 视频表id  ==> ' . $this->video->id . ', 原因 : '. $e->getMessage() . ', 重新分发');
                dispatch((new GetVideoDuration($this->video->id))->delay(Carbon::now()->addMinutes(3)));
            }
        }
    
    }
    
    

    期间遇到的问题是队列第一次能正常运行, 之后再次分发的话, 会一直报下面这个错误:

    ErrorException: Undefined index: cn-shanghai#vod in /workspace/dev6/ctjy_api_server/app/Service/VodServer/aliyun-php-sdk-core/Regions/LocationService.php:82
    

    找到报错的阿里云的sdk的位置, 在这个方法前面加上代码对这个变量进行判断

    
    private function checkCacheIsExpire($key)
        {
            if(!self::$lastClearTimePerProduct) return false;   // 加上代码
            $lastClearTime = self::$lastClearTimePerProduct[$key];
            if ($lastClearTime == null)
            {
                $lastClearTime = time();
                self::$lastClearTimePerProduct[$key] = $lastClearTime;
            }
    
            $now = time();
            $elapsedTime = $now - $lastClearTime;
    
            if ($elapsedTime > CACHE_EXPIRE_TIME)
            {
                $lastClearTime = time();
                self::$lastClearTimePerProduct[$key] = $lastClearTime;
                return true;
            }
    
            return false;
        }
    

    相关文章

      网友评论

          本文标题:laravel队列获取阿里云视频点播服务视频时长报错Undefi

          本文链接:https://www.haomeiwen.com/subject/wthwcftx.html