Skip to content

Commit

Permalink
Move recalculation of content lengths to background job
Browse files Browse the repository at this point in the history
  • Loading branch information
robbevp committed Oct 27, 2023
1 parent 3245c60 commit b62f4f6
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 70 deletions.
13 changes: 13 additions & 0 deletions app/jobs/recalculate_content_lengths_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class RecalculateContentLengthsJob < ApplicationJob
queue_as :within_5_minutes

def perform
AudioFile.find_each do |af|
next unless Rails.application.config.recalculate_content_length_if.call af

CodecConversion.find_each do |cc|
CalculateContentLengthJob.set(queue: :whenever).perform_later(af, cc)
end
end
end
end
12 changes: 0 additions & 12 deletions app/models/content_length.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,4 @@ class ContentLength < ApplicationRecord

validates :audio_file, uniqueness: { scope: :codec_conversion }
validates :length, presence: true

def self.destroy_all_and_recalculate
destroy_all

AudioFile.find_each do |af|
next unless Rails.application.config.recalculate_content_length_if.call af

CodecConversion.find_each do |cc|
CalculateContentLengthJob.set(queue: :whenever).perform_later(af, cc)
end
end
end
end
3 changes: 2 additions & 1 deletion lib/tasks/ffmpeg.rake
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ namespace :ffmpeg do

exit if prev_version == new_version

ContentLength.destroy_all_and_recalculate
ContentLength.destroy_all
RecalculateContentLengthsJob.perform_later

File.write(path, new_version)
end
Expand Down
63 changes: 63 additions & 0 deletions test/jobs/recalculate_content_lengths_job_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require 'test_helper'

class RecalculateContentLengthsJobTest < ActiveJob::TestCase
def setup
# ContentLengths are automatically created when we create an AudioFile and CodecConversion. Manually creating one would result in an error due to uniqueness contraints.
io = StringIO.new Rails.root.join('test/files/base.flac').read
AudioFile.any_instance.stubs(:convert).returns(io)
@audio_file = create(:audio_file)
@codec_conversion = create(:codec_conversion)

ContentLength.destroy_all
end

test 'should not shedule job if audio is longer than config and track is older than config' do
@track = create(:track, audio_file: @audio_file)

# rubocop:disable Rails/SkipsModelValidations
# We want to avoid our own before_save callbacks to manually set audio_file length and track age
@audio_file.update_column(:length, 1)
@audio_file.track.update_column(:created_at, 1.year.ago)
# rubocop:enable Rails/SkipsModelValidations

assert_no_enqueued_jobs do
RecalculateContentLengthsJob.perform_now
end
end

test 'should enqueue job if audio is longer than config' do
io = StringIO.new Rails.root.join('test/files/base.flac').read
AudioFile.any_instance.stubs(:convert).returns(io)
@track = create(:track, audio_file: @audio_file)

# rubocop:disable Rails/SkipsModelValidations
# We want to avoid our own before_save callbacks to manually set the audio_file length and track age
@audio_file.update_column(:length, 1000)
@audio_file.track.update_column(:created_at, 1.year.ago)
# rubocop:enable Rails/SkipsModelValidations

assert_enqueued_jobs 1 do
RecalculateContentLengthsJob.perform_now
end
end

test 'should enqueue job if track is newer than config' do
io = StringIO.new Rails.root.join('test/files/base.flac').read
AudioFile.any_instance.stubs(:convert).returns(io)
@track = create(:track, audio_file: @audio_file)

# rubocop:disable Rails/SkipsModelValidations
# We want to avoid our own before_save callbacks to manually set the audio_file length and track age
@audio_file.update_column(:length, 1)
@audio_file.track.update_column(:created_at, 1.day.ago)
# rubocop:enable Rails/SkipsModelValidations

assert_enqueued_jobs 1 do
RecalculateContentLengthsJob.perform_now
end

@audio_file.reload

assert_equal 1, @audio_file.content_lengths.length
end
end
57 changes: 0 additions & 57 deletions test/models/content_length_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,61 +11,4 @@
require 'test_helper'

class ContentLengthTest < ActiveSupport::TestCase
def setup
# ContentLengths are automatically created when we create an AudioFile and CodecConversion. Manually creating one would result in an error due to uniqueness contraints.
io = StringIO.new Rails.root.join('test/files/base.flac').read
AudioFile.any_instance.stubs(:convert).returns(io)
@audio_file = create(:audio_file)
@codec_conversion = create(:codec_conversion)
end

test 'should not create new ContentLength if audio is longer than config and track is older than config' do
@track = create(:track, audio_file: @audio_file)

# rubocop:disable Rails/SkipsModelValidations
# We want to avoid our own before_save callbacks to manually set audio_file length and track age
@audio_file.update_column(:length, 1)
@audio_file.track.update_column(:created_at, 1.year.ago)
# rubocop:enable Rails/SkipsModelValidations

assert_difference('ContentLength.count', -1) do
ContentLength.destroy_all_and_recalculate
end
end

test 'should create new ContentLength if audio is longer than config' do
io = StringIO.new Rails.root.join('test/files/base.flac').read
AudioFile.any_instance.stubs(:convert).returns(io)
@track = create(:track, audio_file: @audio_file)

# rubocop:disable Rails/SkipsModelValidations
# We want to avoid our own before_save callbacks to manually set the audio_file length and track age
@audio_file.update_column(:length, 1000)
@audio_file.track.update_column(:created_at, 1.year.ago)
# rubocop:enable Rails/SkipsModelValidations

assert_difference('ContentLength.count', 0) do
ContentLength.destroy_all_and_recalculate
end
end

test 'should create new ContentLength if track is newer than config' do
io = StringIO.new Rails.root.join('test/files/base.flac').read
AudioFile.any_instance.stubs(:convert).returns(io)
@track = create(:track, audio_file: @audio_file)

# rubocop:disable Rails/SkipsModelValidations
# We want to avoid our own before_save callbacks to manually set the audio_file length and track age
@audio_file.update_column(:length, 1)
@audio_file.track.update_column(:created_at, 1.day.ago)
# rubocop:enable Rails/SkipsModelValidations

assert_difference('ContentLength.count', 0) do
ContentLength.destroy_all_and_recalculate
end

@audio_file.reload

assert_equal 1, @audio_file.content_lengths.length
end
end

0 comments on commit b62f4f6

Please sign in to comment.