Skip to content

Commit 86f1974

Browse files
committed
Move recalculation of content lengths to background job
1 parent 3245c60 commit 86f1974

5 files changed

+74
-70
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class RecalculateContentLengthsJob < ApplicationJob
2+
queue_as :within_5_minutes
3+
4+
def perform
5+
AudioFile.find_each do |af|
6+
next unless Rails.application.config.recalculate_content_length_if.call af
7+
8+
CodecConversion.find_each do |cc|
9+
CalculateContentLengthJob.set(queue: :whenever).perform_later(af, cc)
10+
end
11+
end
12+
end
13+
end

app/models/content_length.rb

-12
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,4 @@ class ContentLength < ApplicationRecord
1414

1515
validates :audio_file, uniqueness: { scope: :codec_conversion }
1616
validates :length, presence: true
17-
18-
def self.destroy_all_and_recalculate
19-
destroy_all
20-
21-
AudioFile.find_each do |af|
22-
next unless Rails.application.config.recalculate_content_length_if.call af
23-
24-
CodecConversion.find_each do |cc|
25-
CalculateContentLengthJob.set(queue: :whenever).perform_later(af, cc)
26-
end
27-
end
28-
end
2917
end

lib/tasks/ffmpeg.rake

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ namespace :ffmpeg do
1818

1919
exit if prev_version == new_version
2020

21-
ContentLength.destroy_all_and_recalculate
21+
ContentLength.destroy_all
22+
RecalculateContentLengthsJob.perform_later
2223

2324
File.write(path, new_version)
2425
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
require 'test_helper'
2+
3+
class RecalculateContentLengthsJobTest < ActiveJob::TestCase
4+
def setup
5+
# ContentLengths are automatically created when we create an AudioFile and CodecConversion. Manually creating one would result in an error due to uniqueness contraints.
6+
io = StringIO.new Rails.root.join('test/files/base.flac').read
7+
AudioFile.any_instance.stubs(:convert).returns(io)
8+
@audio_file = create(:audio_file)
9+
@codec_conversion = create(:codec_conversion)
10+
11+
ContentLength.destroy_all
12+
end
13+
14+
test 'should not enqueue job if audio is longer than config and track is older than config' do
15+
@track = create(:track, audio_file: @audio_file)
16+
17+
# rubocop:disable Rails/SkipsModelValidations
18+
# We want to avoid our own before_save callbacks to manually set audio_file length and track age
19+
@audio_file.update_column(:length, 1)
20+
@audio_file.track.update_column(:created_at, 1.year.ago)
21+
# rubocop:enable Rails/SkipsModelValidations
22+
23+
assert_no_enqueued_jobs do
24+
RecalculateContentLengthsJob.perform_now
25+
end
26+
end
27+
28+
test 'should enqueue job if audio is longer than config' do
29+
io = StringIO.new Rails.root.join('test/files/base.flac').read
30+
AudioFile.any_instance.stubs(:convert).returns(io)
31+
@track = create(:track, audio_file: @audio_file)
32+
33+
# rubocop:disable Rails/SkipsModelValidations
34+
# We want to avoid our own before_save callbacks to manually set the audio_file length and track age
35+
@audio_file.update_column(:length, 1000)
36+
@audio_file.track.update_column(:created_at, 1.year.ago)
37+
# rubocop:enable Rails/SkipsModelValidations
38+
39+
assert_enqueued_jobs 1 do
40+
RecalculateContentLengthsJob.perform_now
41+
end
42+
end
43+
44+
test 'should enqueue job if track is newer than config' do
45+
io = StringIO.new Rails.root.join('test/files/base.flac').read
46+
AudioFile.any_instance.stubs(:convert).returns(io)
47+
@track = create(:track, audio_file: @audio_file)
48+
49+
# rubocop:disable Rails/SkipsModelValidations
50+
# We want to avoid our own before_save callbacks to manually set the audio_file length and track age
51+
@audio_file.update_column(:length, 1)
52+
@audio_file.track.update_column(:created_at, 1.day.ago)
53+
# rubocop:enable Rails/SkipsModelValidations
54+
55+
assert_enqueued_jobs 1 do
56+
RecalculateContentLengthsJob.perform_now
57+
end
58+
end
59+
end

test/models/content_length_test.rb

-57
Original file line numberDiff line numberDiff line change
@@ -11,61 +11,4 @@
1111
require 'test_helper'
1212

1313
class ContentLengthTest < ActiveSupport::TestCase
14-
def setup
15-
# ContentLengths are automatically created when we create an AudioFile and CodecConversion. Manually creating one would result in an error due to uniqueness contraints.
16-
io = StringIO.new Rails.root.join('test/files/base.flac').read
17-
AudioFile.any_instance.stubs(:convert).returns(io)
18-
@audio_file = create(:audio_file)
19-
@codec_conversion = create(:codec_conversion)
20-
end
21-
22-
test 'should not create new ContentLength if audio is longer than config and track is older than config' do
23-
@track = create(:track, audio_file: @audio_file)
24-
25-
# rubocop:disable Rails/SkipsModelValidations
26-
# We want to avoid our own before_save callbacks to manually set audio_file length and track age
27-
@audio_file.update_column(:length, 1)
28-
@audio_file.track.update_column(:created_at, 1.year.ago)
29-
# rubocop:enable Rails/SkipsModelValidations
30-
31-
assert_difference('ContentLength.count', -1) do
32-
ContentLength.destroy_all_and_recalculate
33-
end
34-
end
35-
36-
test 'should create new ContentLength if audio is longer than config' do
37-
io = StringIO.new Rails.root.join('test/files/base.flac').read
38-
AudioFile.any_instance.stubs(:convert).returns(io)
39-
@track = create(:track, audio_file: @audio_file)
40-
41-
# rubocop:disable Rails/SkipsModelValidations
42-
# We want to avoid our own before_save callbacks to manually set the audio_file length and track age
43-
@audio_file.update_column(:length, 1000)
44-
@audio_file.track.update_column(:created_at, 1.year.ago)
45-
# rubocop:enable Rails/SkipsModelValidations
46-
47-
assert_difference('ContentLength.count', 0) do
48-
ContentLength.destroy_all_and_recalculate
49-
end
50-
end
51-
52-
test 'should create new ContentLength if track is newer than config' do
53-
io = StringIO.new Rails.root.join('test/files/base.flac').read
54-
AudioFile.any_instance.stubs(:convert).returns(io)
55-
@track = create(:track, audio_file: @audio_file)
56-
57-
# rubocop:disable Rails/SkipsModelValidations
58-
# We want to avoid our own before_save callbacks to manually set the audio_file length and track age
59-
@audio_file.update_column(:length, 1)
60-
@audio_file.track.update_column(:created_at, 1.day.ago)
61-
# rubocop:enable Rails/SkipsModelValidations
62-
63-
assert_difference('ContentLength.count', 0) do
64-
ContentLength.destroy_all_and_recalculate
65-
end
66-
67-
@audio_file.reload
68-
69-
assert_equal 1, @audio_file.content_lengths.length
70-
end
7114
end

0 commit comments

Comments
 (0)