1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Only enqueue analysis jobs when blob is analyzable

This commit is contained in:
Gannon McGibbon 2020-01-16 14:14:28 -05:00 committed by George Claghorn
parent 5d2789b11b
commit ed81601723
5 changed files with 32 additions and 2 deletions

View file

@ -1,3 +1,7 @@
* Only enqueue analysis jobs for blobs with non-null analyzer classes.
*Gannon McGibbon*
* Previews are created on the same service as the original blob. * Previews are created on the same service as the original blob.
*Peter Zhu* *Peter Zhu*

View file

@ -29,12 +29,16 @@ module ActiveStorage::Blob::Analyzable
update! metadata: metadata.merge(extract_metadata_via_analyzer) update! metadata: metadata.merge(extract_metadata_via_analyzer)
end end
# Enqueues an ActiveStorage::AnalyzeJob which calls #analyze. # Enqueues an ActiveStorage::AnalyzeJob which calls #analyze, or calls #analyze inline based on analyzer class configuration.
# #
# This method is automatically called for a blob when it's attached for the first time. You can call it to analyze a blob # This method is automatically called for a blob when it's attached for the first time. You can call it to analyze a blob
# again (e.g. if you add a new analyzer or modify an existing one). # again (e.g. if you add a new analyzer or modify an existing one).
def analyze_later def analyze_later
if analyzer_class.analyze_later?
ActiveStorage::AnalyzeJob.perform_later(self) ActiveStorage::AnalyzeJob.perform_later(self)
else
analyze
end
end end
# Returns true if the blob has been analyzed. # Returns true if the blob has been analyzed.

View file

@ -12,6 +12,12 @@ module ActiveStorage
false false
end end
# Implement this method in concrete subclasses. It will determine if blob anlysis
# should be done in a job or performed inine. By default, analysis is enqueued in a job.
def self.analyze_later?
true
end
def initialize(blob) def initialize(blob)
@blob = blob @blob = blob
end end

View file

@ -6,6 +6,10 @@ module ActiveStorage
true true
end end
def self.analyze_later?
false
end
def metadata def metadata
{} {}
end end

View file

@ -25,6 +25,18 @@ class ActiveStorage::AttachmentTest < ActiveSupport::TestCase
assert_equal 2736, blob.metadata[:height] assert_equal 2736, blob.metadata[:height]
end end
test "attaching a un-analyzable blob" do
blob = create_blob(filename: "blank.txt")
assert_not_predicate blob, :analyzed?
assert_no_enqueued_jobs do
@user.highlights.attach(blob)
end
assert_predicate blob.reload, :analyzed?
end
test "mirroring a directly-uploaded blob after attaching it" do test "mirroring a directly-uploaded blob after attaching it" do
with_service("mirror") do with_service("mirror") do
blob = directly_upload_file_blob blob = directly_upload_file_blob