ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Job(Queue)
    Laravel 2020. 6. 14. 02:01
    반응형
    SMALL

    1. 세팅

    @ .env

    QUEUE_CONNECTION=database

     

    2. Job 생성

    php artisan make:job CollectCommentsJob

    @ CollectCommentsJob

    <?php
    
    namespace App\Jobs;
    
    use App\Enums\StateType;
    use App\Target;
    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\Http;
    
    class CollectCommentsJob implements ShouldQueue
    {
        use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    
        protected $target;
        /**
         * Create a new job instance.
         *
         * @return void
         */
        public function __construct(Target $target)
        {
            $this->target = $target;
        }
    
        /**
         * Execute the job.
         *
         * @return void
         */
        public function handle()
        {
            try {
                $response = Http::get(config("services.google.domain")."/videos", [
                    "id" => $this->getYoutubeVideoId($this->target->url),
                    "part" => "statistics, snippet",
                    "key" => config("services.google.key")
                ]);
    
                $video = $response->json()["items"][0][1];
    
                $this->target->update([
                    "idx" => $video["id"],
                    "title" => $video["snippet"]["title"],
                    "body" => $video["snippet"]["description"],
                    "thumbnail" => $video["snippet"]["thumbnails"]["default"]["url"],
                    "state" => StateType::SUCCESS
                ]);
            }catch(\Exception $exception){
                $this->target->update([
                    "state" => StateType::FAIL
                ]);
            }
        }
    
        public function getYoutubeVideoId($url)
        {
            if (strpos($url, "watch") !== false)
                return str_replace("https://www.youtube.com/watch?v=", "", $url);
    
            return str_replace("https://youtu.be/", "", $url);
        }
    }
    

     

    3. Job 호출하기

    <?php
    
    namespace App\Http\Controllers\Api;
    
    use App\Http\Controllers\Controller;
    use App\Http\Resources\TargetResource;
    use App\Jobs\CollectCommentsJob;
    use App\Target;
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Http;
    
    class TargetController extends ApiController
    {
        public function store(Request $request)
        {
            $request->validate([
                "platform" => "required|string|max:500",
                "url" => "required|string|max:5000",
            ]);
    
            $target = auth()->user()->targets()->create($request->all());
    
            dispatch(new CollectCommentsJob($target));
    
            return $this->respondCreated(TargetResource::make($target));
        }
    }
    

     

    LIST

    'Laravel' 카테고리의 다른 글

    whereExists  (0) 2020.08.18
    Object array 유효성 검사하는법  (0) 2020.07.19
    ENUM  (0) 2020.05.31
    엑셀(Excel) 다루기  (0) 2020.04.27
    이미지 다루기 with 미디어 라이브러리(Media Library) + S3  (0) 2020.04.21

    댓글

Designed by Tistory.