Skip to content

Commit ed81601

Browse files
gmcgibbongeorgeclaghorn
authored andcommitted
Only enqueue analysis jobs when blob is analyzable
1 parent 5d2789b commit ed81601

File tree

5 files changed

+32
-2
lines changed

5 files changed

+32
-2
lines changed

activestorage/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Only enqueue analysis jobs for blobs with non-null analyzer classes.
2+
3+
*Gannon McGibbon*
4+
15
* Previews are created on the same service as the original blob.
26

37
*Peter Zhu*

activestorage/app/models/active_storage/blob/analyzable.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,16 @@ def analyze
2929
update! metadata: metadata.merge(extract_metadata_via_analyzer)
3030
end
3131

32-
# Enqueues an ActiveStorage::AnalyzeJob which calls #analyze.
32+
# Enqueues an ActiveStorage::AnalyzeJob which calls #analyze, or calls #analyze inline based on analyzer class configuration.
3333
#
3434
# This method is automatically called for a blob when it's attached for the first time. You can call it to analyze a blob
3535
# again (e.g. if you add a new analyzer or modify an existing one).
3636
def analyze_later
37-
ActiveStorage::AnalyzeJob.perform_later(self)
37+
if analyzer_class.analyze_later?
38+
ActiveStorage::AnalyzeJob.perform_later(self)
39+
else
40+
analyze
41+
end
3842
end
3943

4044
# Returns true if the blob has been analyzed.

activestorage/lib/active_storage/analyzer.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ def self.accept?(blob)
1212
false
1313
end
1414

15+
# Implement this method in concrete subclasses. It will determine if blob anlysis
16+
# should be done in a job or performed inine. By default, analysis is enqueued in a job.
17+
def self.analyze_later?
18+
true
19+
end
20+
1521
def initialize(blob)
1622
@blob = blob
1723
end

activestorage/lib/active_storage/analyzer/null_analyzer.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ def self.accept?(blob)
66
true
77
end
88

9+
def self.analyze_later?
10+
false
11+
end
12+
913
def metadata
1014
{}
1115
end

activestorage/test/models/attachment_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ class ActiveStorage::AttachmentTest < ActiveSupport::TestCase
2525
assert_equal 2736, blob.metadata[:height]
2626
end
2727

28+
test "attaching a un-analyzable blob" do
29+
blob = create_blob(filename: "blank.txt")
30+
31+
assert_not_predicate blob, :analyzed?
32+
33+
assert_no_enqueued_jobs do
34+
@user.highlights.attach(blob)
35+
end
36+
37+
assert_predicate blob.reload, :analyzed?
38+
end
39+
2840
test "mirroring a directly-uploaded blob after attaching it" do
2941
with_service("mirror") do
3042
blob = directly_upload_file_blob

0 commit comments

Comments
 (0)