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

Runs the job.

Parameters:

  • video_id (Integer)

    the video id

  • privacy_status (String) (defaults to: 'private')

    the privacy status

Returns:

  • (Object)

    the result



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/workers/youtube_upload_worker.rb', line 19

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}")

  # A freshly inserted video sits in youtube_upload_status 'processing'.
  # Re-pull after YouTube has had time to transcode so SyncService can
  # reconcile the status (see YouTube::SyncService#reconcile_upload_status).
  YouTubeSyncWorker.perform_in(10.minutes, video.id)
rescue YouTube::OauthService::TokenRefreshError => e
  Rails.logger.error("[YouTubeUploadWorker] OAuth error for video #{video_id}: #{e.message}")
  record_failure(video_id, "OAuth error: #{e.message}")
rescue YouTube::ApiClient::QuotaExceededError => e
  Rails.logger.error("[YouTubeUploadWorker] Quota exceeded for video #{video_id}: #{e.message}")
  record_failure(video_id, "Quota exceeded: #{e.message}")
rescue YouTube::ApiClient::ApiError => e
  Rails.logger.error("[YouTubeUploadWorker] API error for video #{video_id}: #{e.message}")
  record_failure(video_id, "API error: #{e.message}")
end