module Recordings
  class PublishToYoutubeJob
    include Sidekiq::Job
    sidekiq_options retry: 2, queue: "default"

    def perform(recording_id)
      recording = Recording.find_by(id: recording_id)
      return unless recording&.ready?

      Recordings::PublishToYoutube.new(recording).call
    rescue Recordings::PublishToYoutube::Error => e
      meta = recording.metadata.merge("youtube_publish_error" => e.message, "youtube_publish_failed_at" => Time.current.iso8601)
      recording.update!(metadata: meta)
      Rails.logger.warn("[Recordings::PublishToYoutubeJob] #{recording_id}: #{e.message}")
    end
  end
end
