Elimina i path MediaMTX orfani dopo ogni diretta.

Corregge il cleanup dei path live/match_{uuid}, rimuove sempre il path a fine sessione e lo esegue ogni ora via cron.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-06-16 20:42:29 +02:00
parent 594853ed04
commit 827abf80cb
8 changed files with 221 additions and 30 deletions

View File

@@ -16,5 +16,8 @@ namespace :recordings do
result = Recordings::CleanupLocal.new.call
puts "Pulizia segmenti locali: rimossi=#{result.removed} " \
"liberati=#{result.freed_bytes} byte saltati=#{result.skipped}"
orphan = Mediamtx::CleanupOrphanPaths.new.call
puts "Path MediaMTX orfani rimossi: #{orphan.removed} (saltati: #{orphan.skipped})"
end
end

View File

@@ -19,32 +19,14 @@ namespace :streams do
puts " ended #{session.id} (fallback: #{e.message})"
end
removed = cleanup_orphan_mediamtx_paths(client, online)
puts "Path MediaMTX rimossi: #{removed}"
result = Mediamtx::CleanupOrphanPaths.new(client: client).call
puts "Path MediaMTX rimossi: #{result.removed} (saltati: #{result.skipped})"
puts "Fatto."
end
end
def cleanup_orphan_mediamtx_paths(client, online_paths)
removed = 0
client.list_paths.each do |item|
name = item["name"]
next unless name&.start_with?("match_")
session_id = name.delete_prefix("match_")
session = StreamSession.find_by(id: session_id)
should_remove = !online_paths.include?(name) &&
(session.nil? || %w[ended error].include?(session.status))
next unless should_remove
client.delete_path_name(name)
removed += 1
puts " deleted path #{name}"
rescue Mediamtx::Client::Error, Faraday::Error => e
puts " skip #{name}: #{e.message}"
desc "Rimuove path MediaMTX orfani (dirette già terminate)"
task cleanup_orphan_paths: :environment do
result = Mediamtx::CleanupOrphanPaths.new.call
puts "Path MediaMTX rimossi: #{result.removed} (saltati: #{result.skipped})"
end
removed
end