Così overflow Hetzner e VOD YouTube restano in archivio dopo lo spegnimento del nodo, e la colonna ingest non si svuota. Co-authored-by: Cursor <cursoragent@cursor.com>
95 lines
2.7 KiB
Ruby
95 lines
2.7 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Recordings
|
|
class FinalizeSession
|
|
def initialize(session)
|
|
@session = session
|
|
end
|
|
|
|
def call
|
|
policy = Recordings::StoragePolicy.call(@session)
|
|
return if policy == Recordings::StoragePolicy::NONE
|
|
|
|
team = @session.match.team
|
|
attrs = attributes_for(policy, team)
|
|
|
|
recording = Recording.find_or_initialize_by(stream_session: @session)
|
|
if skip_reinitialize?(recording)
|
|
Rails.logger.info(
|
|
"[Recordings::FinalizeSession] skip already-finalized " \
|
|
"recording=#{recording.id} status=#{recording.status}"
|
|
)
|
|
return recording
|
|
end
|
|
recording.assign_attributes(attrs)
|
|
recording.save!
|
|
|
|
Rails.logger.info(
|
|
"[Recordings::FinalizeSession] session=#{@session.id} recording=#{recording.id} " \
|
|
"storage_policy=#{policy} expires_at=#{recording.expires_at.inspect} " \
|
|
"temp_expires_at=#{recording.temp_expires_at.inspect}"
|
|
)
|
|
recording
|
|
end
|
|
|
|
private
|
|
|
|
def skip_reinitialize?(recording)
|
|
return false unless recording.persisted?
|
|
|
|
recording.ready? ||
|
|
recording.status == "processing" ||
|
|
recording.storage_key.present?
|
|
end
|
|
|
|
def attributes_for(policy, team)
|
|
{
|
|
team: team,
|
|
status: "processing",
|
|
title: default_title,
|
|
privacy_status: privacy_from_session,
|
|
storage_path: @session.mediamtx_path_name,
|
|
recorded_at: @session.ended_at || Time.current,
|
|
storage_policy: policy,
|
|
expires_at: archive_expires_at(policy, team),
|
|
temp_expires_at: temp_expires_at_for(policy),
|
|
error_message: nil,
|
|
metadata: initial_metadata(policy, team)
|
|
}
|
|
end
|
|
|
|
def archive_expires_at(policy, team)
|
|
return nil if policy == Recordings::StoragePolicy::TEMPORARY
|
|
|
|
retention_days = team.entitlements.recording_retention_days
|
|
retention_days.positive? ? retention_days.days.from_now : nil
|
|
end
|
|
|
|
def temp_expires_at_for(policy)
|
|
return nil unless policy == Recordings::StoragePolicy::TEMPORARY
|
|
|
|
MatchLiveTv.youtube_temp_replay_retention_hours.hours.from_now
|
|
end
|
|
|
|
def default_title
|
|
match = @session.match
|
|
"#{match.team.name} vs #{match.opponent_name}"
|
|
end
|
|
|
|
def privacy_from_session
|
|
@session.privacy_status == "public" ? "public" : "unlisted"
|
|
end
|
|
|
|
def initial_metadata(policy, team)
|
|
{
|
|
"source_platform" => @session.platform,
|
|
"session_privacy" => @session.privacy_status,
|
|
# Re-upload automatico disabilitato: le live YouTube usano VerifyYoutubeReplay.
|
|
"auto_publish_youtube" => false,
|
|
"storage_policy" => policy,
|
|
"ai" => {}
|
|
}
|
|
end
|
|
end
|
|
end
|