Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Permit sending Active Storage purge and analysis jobs to separate queues #34838

Merged
merged 1 commit into from Jan 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions activestorage/CHANGELOG.md
@@ -1,3 +1,14 @@
* Replace `config.active_storage.queue` with two options that indicate which
queues analysis and purge jobs should use, respectively:

* `config.active_storage.queues.analysis`
* `config.active_storage.queues.purge`

`config.active_storage.queue` is preferred over the new options when it's
set, but it is deprecated and will be removed in Rails 6.1.

*George Claghorn*

* Permit generating variants of TIFF images.

*Luciano Sousa*
Expand Down
2 changes: 2 additions & 0 deletions activestorage/app/jobs/active_storage/analyze_job.rb
Expand Up @@ -2,6 +2,8 @@

# Provides asynchronous analysis of ActiveStorage::Blob records via ActiveStorage::Blob#analyze_later.
class ActiveStorage::AnalyzeJob < ActiveStorage::BaseJob
queue_as { ActiveStorage.queues[:analysis] }

retry_on ActiveStorage::IntegrityError, attempts: 10, wait: :exponentially_longer

def perform(blob)
Expand Down
1 change: 0 additions & 1 deletion activestorage/app/jobs/active_storage/base_job.rb
@@ -1,5 +1,4 @@
# frozen_string_literal: true

class ActiveStorage::BaseJob < ActiveJob::Base
queue_as { ActiveStorage.queue }
end
2 changes: 2 additions & 0 deletions activestorage/app/jobs/active_storage/purge_job.rb
Expand Up @@ -2,6 +2,8 @@

# Provides asynchronous purging of ActiveStorage::Blob records via ActiveStorage::Blob#purge_later.
class ActiveStorage::PurgeJob < ActiveStorage::BaseJob
queue_as { ActiveStorage.queues[:purge] }

discard_on ActiveRecord::RecordNotFound
retry_on ActiveRecord::Deadlocked, attempts: 10, wait: :exponentially_longer

Expand Down
2 changes: 1 addition & 1 deletion activestorage/lib/active_storage.rb
Expand Up @@ -42,7 +42,7 @@ module ActiveStorage

mattr_accessor :logger
mattr_accessor :verifier
mattr_accessor :queue
mattr_accessor :queues, default: {}
mattr_accessor :previewers, default: []
mattr_accessor :analyzers, default: []
mattr_accessor :variant_processor, default: :mini_magick
Expand Down
16 changes: 15 additions & 1 deletion activestorage/lib/active_storage/engine.rb
Expand Up @@ -20,6 +20,7 @@ class Engine < Rails::Engine # :nodoc:
config.active_storage.previewers = [ ActiveStorage::Previewer::PopplerPDFPreviewer, ActiveStorage::Previewer::MuPDFPreviewer, ActiveStorage::Previewer::VideoPreviewer ]
config.active_storage.analyzers = [ ActiveStorage::Analyzer::ImageAnalyzer, ActiveStorage::Analyzer::VideoAnalyzer ]
config.active_storage.paths = ActiveSupport::OrderedOptions.new
config.active_storage.queues = ActiveSupport::OrderedOptions.new

config.active_storage.variable_content_types = %w(
image/png
Expand Down Expand Up @@ -61,7 +62,6 @@ class Engine < Rails::Engine # :nodoc:
initializer "active_storage.configs" do
config.after_initialize do |app|
ActiveStorage.logger = app.config.active_storage.logger || Rails.logger
ActiveStorage.queue = app.config.active_storage.queue
ActiveStorage.variant_processor = app.config.active_storage.variant_processor || :mini_magick
ActiveStorage.previewers = app.config.active_storage.previewers || []
ActiveStorage.analyzers = app.config.active_storage.analyzers || []
Expand Down Expand Up @@ -117,6 +117,20 @@ class Engine < Rails::Engine # :nodoc:
end
end

initializer "active_storage.queues" do
config.after_initialize do |app|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need an after_initialize here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we do, since config.active_storage.queue may be set in an initializer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, that is true. For other frameworks we use on_load but I think we don't/can't have a hook for Active Storage.

if queue = app.config.active_storage.queue
ActiveSupport::Deprecation.warn \
"config.active_storage.queue is deprecated and will be removed in Rails 6.1. " \
"Set config.active_storage.queues.purge and config.active_storage.queues.analysis instead."

ActiveStorage.queues = { purge: queue, analysis: queue }
else
ActiveStorage.queues = app.config.active_storage.queues || {}
end
end
end

initializer "active_storage.reflection" do
ActiveSupport.on_load(:active_record) do
include Reflection::ActiveRecordExtensions
Expand Down
11 changes: 9 additions & 2 deletions guides/source/configuring.md
Expand Up @@ -843,10 +843,16 @@ normal Rails server.
* `config.active_storage.content_types_to_serve_as_binary` accepts an array of strings indicating the content types that Active Storage will always serve as an attachment, rather than inline. The default is `%w(text/html
text/javascript image/svg+xml application/postscript application/x-shockwave-flash text/xml application/xml application/xhtml+xml)`.

* `config.active_storage.queue` can be used to set the name of the Active Job queue used to perform jobs like analyzing the content of a blob or purging a blog.
* `config.active_storage.queues.analysis` accepts a symbol indicating the Active Job queue to use for analysis jobs. When this option is `nil`, analysis jobs are sent to the default Active Job queue (see `config.active_job.default_queue_name`).

```ruby
config.active_storage.queues.analysis = :low_priority
```

* `config.active_storage.queues.purge` accepts a symbol indicating the Active Job queue to use for purge jobs. When this option is `nil`, purge jobs are sent to the default Active Job queue (see `config.active_job.default_queue_name`).

```ruby
config.active_storage.queue = :low_priority
config.active_storage.queues.purge = :low_priority
```

* `config.active_storage.logger` can be used to set the logger used by Active Storage. Accepts a logger conforming to the interface of Log4r or the default Ruby Logger class.
Expand All @@ -870,6 +876,7 @@ text/javascript image/svg+xml application/postscript application/x-shockwave-fla

The default is `/rails/active_storage`


### Configuring a Database

Just about every Rails application will interact with a database. You can connect to the database by setting an environment variable `ENV['DATABASE_URL']` or by using a configuration file called `config/database.yml`.
Expand Down