Class: YouTubeUploadWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job
Defined in:
app/workers/youtube_upload_worker.rb

Overview

Uploads a video to YouTube in the background.

Downloads the MP4 from Cloudflare (or local attachment) and uploads
via the YouTube Data API. This can be a long-running operation for
large videos.

Instance Method Summary collapse

Instance Method Details

#perform(video_id, privacy_status = 'private') ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/workers/youtube_upload_worker.rb', line 14

def perform(video_id, privacy_status = 'private')
  video = Video.find(video_id)

  if video.youtube_id.present?
    Rails.logger.info("[YouTubeUploadWorker] Video #{video_id} already has YouTube ID #{video.youtube_id}, skipping")
    return
  end

  Rails.logger.info("[YouTubeUploadWorker] Starting upload for video #{video_id}")

  service = YouTube::UploadService.new
  service.upload(video, privacy_status: privacy_status)

  Rails.logger.info("[YouTubeUploadWorker] Upload complete for video #{video_id}")
rescue YouTube::OauthService::TokenRefreshError => e
  Rails.logger.error("[YouTubeUploadWorker] OAuth error for video #{video_id}: #{e.message}")
  Video.where(id: video_id).update_all(youtube_upload_status: 'failed')
rescue YouTube::ApiClient::QuotaExceededError => e
  Rails.logger.error("[YouTubeUploadWorker] Quota exceeded for video #{video_id}: #{e.message}")
  Video.where(id: video_id).update_all(youtube_upload_status: 'failed')
rescue YouTube::ApiClient::ApiError => e
  Rails.logger.error("[YouTubeUploadWorker] API error for video #{video_id}: #{e.message}")
  Video.where(id: video_id).update_all(youtube_upload_status: 'failed')
end