Skip to content

Commit 964d41a

Browse files
authored
Merge pull request #331 from lardawge/ls/add-support-for-removed-records
Add support for deleted records before processing
2 parents 122c296 + 41afc5c commit 964d41a

File tree

9 files changed

+84
-9
lines changed

9 files changed

+84
-9
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 1.1.0
2+
3+
### enhancements
4+
* Suppress NotFoundError if a record gets deleted before it is processed. This is configurable and defaults to true. [lardawge]
5+
16
## 1.0.3
27

38
### enhancements

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
[![Build Status](https://github.com/lardawge/carrierwave_backgrounder/actions/workflows/ruby-ci.yml/badge.svg)](https://github.com/lardawge/carrierwave_backgrounder/actions/workflows/ruby-ci.yml)
44
[![Code Climate](https://codeclimate.com/github/lardawge/carrierwave_backgrounder.png)](https://codeclimate.com/github/lardawge/carrierwave_backgrounder)
55

6-
NOTICE: Version 1.0.0 contains breaking changes if you are coming from an earlier version.
7-
The most notible change is the removal of queue backend options other than active_job and sidekiq.
8-
If you are using other backends, switch over to active_job which should support your preference.
9-
If you are using Sidekiq, there is nothing to change.
6+
---
7+
NOTICE: Version 1.1.0 contains a change in behavior from previous version. When a record is deleted before the job is picked up, it will no longer raise an error. Prior to this change, when using `process_in_background`, if a record was missing, an error was raised. Some users might have relied on that. By default, this will no longer happen. If you want to maintain that behavior, you must set the `suppress_record_not_found_errors` configuration to `false`. This will raise a RecordNotFound error.
108

9+
---
1110
I am a fan of CarrierWave. That being said, I don't like tying up requests waiting for images to process.
1211

1312
This gem addresses that by offloading processing or storaging/processing to a background task.

lib/backgrounder/workers/base.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ def perform(*args)
1414
set_args(*args) if args.present?
1515
self.record = constantized_resource.find id
1616
rescue *not_found_errors
17+
raise not_found_errors.first unless CarrierWave::Backgrounder.suppress_not_found_errors
1718
end
1819

1920
private
2021

2122
def not_found_errors
22-
[].tap do |errors|
23+
@not_found_errors ||= [].tap do |errors|
2324
errors << ::ActiveRecord::RecordNotFound if defined?(::ActiveRecord)
2425
errors << ::Mongoid::Errors::DocumentNotFound if defined?(::Mongoid)
2526
end

lib/backgrounder/workers/process_asset_mixin.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ def self.included(base)
1111

1212
def perform(*args)
1313
record = super(*args)
14+
15+
return unless record
16+
1417
record.send(:"process_#{column}_upload=", true)
1518
asset = record.send(:"#{column}")
1619

lib/carrierwave_backgrounder.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ module Backgrounder
99
include Support::Backends
1010

1111
class << self
12-
attr_reader :worker_klass
12+
attr_reader :worker_klass, :suppress_not_found_errors
1313
end
1414

1515
def self.configure
1616
yield self
1717

18+
@suppress_not_found_errors ||= true
19+
1820
case backend
1921
when :active_job
2022
@worker_klass = 'CarrierWave::Workers::ActiveJob'
21-
22-
require 'active_job'
2323
require 'backgrounder/workers/active_job/process_asset'
2424
require 'backgrounder/workers/active_job/store_asset'
2525

@@ -34,7 +34,6 @@ def self.configure
3434
when :sidekiq
3535
@worker_klass = 'CarrierWave::Workers'
3636

37-
require 'sidekiq'
3837
::CarrierWave::Workers::ProcessAsset.class_eval do
3938
include ::Sidekiq::Worker
4039
end
@@ -43,6 +42,10 @@ def self.configure
4342
end
4443
end
4544
end
45+
46+
def self.suppress_record_not_found_errors(surpress_errors = true)
47+
@suppress_not_found_errors = surpress_errors
48+
end
4649
end
4750
end
4851

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
CarrierWave::Backgrounder.configure do |c|
22
c.backend :active_job, queue: :carrierwave
33
# c.backend :sidekiq, queue: :carrierwave
4+
5+
## Uncomment if you would like a NotFoundError raised if a record is deleted before processing
6+
# c.suppress_record_not_found_errors false
47
end

spec/integrations/process_in_background_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,35 @@
2222
end
2323
end
2424

25+
context 'when a record gets deleted before it is processed' do
26+
context 'and suppress_record_not_found_errors is set to true' do
27+
before do
28+
admin.update(avatar: load_file('test-1.jpg'))
29+
end
30+
31+
it 'does not raise an error' do
32+
admin.delete
33+
expect { process_latest_sidekiq_job }.not_to raise_error
34+
end
35+
end
36+
37+
context 'and suppress_record_not_found_errors is set to false' do
38+
before do
39+
admin.update(avatar: load_file('test-1.jpg'))
40+
CarrierWave::Backgrounder.suppress_record_not_found_errors(false)
41+
end
42+
43+
after do
44+
CarrierWave::Backgrounder.suppress_record_not_found_errors(true)
45+
end
46+
47+
it 'raises an error' do
48+
admin.delete
49+
expect { process_latest_sidekiq_job }.to raise_error(ActiveRecord::RecordNotFound)
50+
end
51+
end
52+
end
53+
2554
context 'when processing the worker' do
2655
before do
2756
admin.update(avatar: load_file('test-1.jpg'))

spec/integrations/store_in_background_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,35 @@
6868
end
6969
end
7070

71+
context 'when a record gets deleted before it is processed' do
72+
context 'and suppress_record_not_found_errors is set to true' do
73+
before do
74+
user.update(avatar: load_file('test-1.jpg'))
75+
end
76+
77+
it 'does not raise an error' do
78+
user.delete
79+
expect { process_latest_sidekiq_job }.not_to raise_error
80+
end
81+
end
82+
83+
context 'and suppress_record_not_found_errors is set to false' do
84+
before do
85+
user.update(avatar: load_file('test-1.jpg'))
86+
CarrierWave::Backgrounder.suppress_record_not_found_errors(false)
87+
end
88+
89+
after do
90+
CarrierWave::Backgrounder.suppress_record_not_found_errors(true)
91+
end
92+
93+
it 'raises an error' do
94+
user.delete
95+
expect { process_latest_sidekiq_job }.to raise_error(ActiveRecord::RecordNotFound)
96+
end
97+
end
98+
end
99+
71100
context 'when setting a column for removal' do
72101
let!(:user) {
73102
Sidekiq::Testing.inline! do

spec/support/dummy_app/config/initializers/carrierwave_backgrounder.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
require 'active_job'
2+
require 'sidekiq'
3+
14
queue_adapter = ENV['QUEUE_ADAPTER'] || :active_job
25
CarrierWave::Backgrounder.configure do |c|
36
c.backend queue_adapter.to_sym, queue: :carrierwave

0 commit comments

Comments
 (0)