-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move recalculation of content lengths to background job
- Loading branch information
Showing
5 changed files
with
78 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters