Skip to content

Commit

Permalink
Transcode to a temporary file which is only moved when finished
Browse files Browse the repository at this point in the history
  • Loading branch information
chvp committed Oct 10, 2024
1 parent 3b0f6d5 commit 3e0c12a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/jobs/create_transcoded_item_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def perform(audio_file, codec_conversion)
uuid = TranscodedItem.uuid_for(codec_conversion)
path = TranscodedItem.path_for(codec_conversion, uuid)
FileUtils.mkdir_p Pathname.new(path).parent
audio_file.convert(codec_conversion, path)
audio_file.convert_and_move(codec_conversion, path)

TranscodedItem.transaction do
# Check that the transcoded item was not created while we were executing
Expand Down
8 changes: 8 additions & 0 deletions app/models/audio_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ def check_self
false
end

def convert_and_move(codec_conversion, out_file_name)
tmp_path = File.join(Dir.tmpdir, "accentor_transcode-#{id}-#{codec_conversion.id}-#{SecureRandom.uuid}")
convert(codec_conversion, tmp_path)
FileUtils.mv tmp_path, out_file_name
ensure
FileUtils.rm_f tmp_path
end

def convert(codec_conversion, out_file_name)
parameters = codec_conversion.ffmpeg_params.split
_stdout, status = Open3.capture2(
Expand Down
5 changes: 3 additions & 2 deletions test/jobs/create_transcoded_item_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ class CreateTranscodedItemJobTest < ActiveJob::TestCase
test 'should abort when TranscodedItem was created while converting' do
audio_file = @audio_file
codec_conversion = @codec_conversion
AudioFile.define_method :convert, lambda { |_codec_conversion, _out_file_name|
AudioFile.define_method :convert, lambda { |_codec_conversion, out_file_name|
FileUtils.touch out_file_name
TranscodedItem.create!(audio_file:, codec_conversion:, uuid: '0000-0000-0000')
}

FileUtils.stubs(:rm_f).once
FileUtils.stubs(:rm_f).twice

CreateTranscodedItemJob.perform_now(@audio_file, @codec_conversion)
end
Expand Down

0 comments on commit 3e0c12a

Please sign in to comment.