Skip to content

Commit fa768a5

Browse files
authored
Merge pull request #7 from hoppergee/active_job_support_enhencement
Active job support enhencement
2 parents 2c1554b + 4f9f76c commit fa768a5

File tree

7 files changed

+184
-3
lines changed

7 files changed

+184
-3
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
## [Unreleased]
22

3+
## [1.1.1] - 2021-10-07
4+
5+
- Make sure all four job delivery ways work as expected
6+
- MyJob.perform_now('hi')
7+
- MyJob.perform_later('hi')
8+
- MyJob.set(queue: 'high').perform_now('hi')
9+
- MyJob.set(queue: 'high').perform_later('hi')
10+
311
## [1.1.0] - 2021-10-07
412

513
- Make tenant finding strategy customizable

lib/multi_tenant_support/active_job.rb

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
11
module MultiTenantSupport
2+
23
module ActiveJob
34
extend ActiveSupport::Concern
45

56
included do
67
attr_accessor :current_tenant
78
end
89

10+
class_methods do
11+
if Gem::Version.new(Rails.version) < Gem::Version.new("7.0.0.alpha1")
12+
def perform_now(*args)
13+
job = job_or_instantiate(*args)
14+
job.current_tenant = MultiTenantSupport.current_tenant
15+
job.perform_now
16+
end
17+
else
18+
eval("
19+
def perform_now(...)
20+
job = job_or_instantiate(...)
21+
job.current_tenant = MultiTenantSupport.current_tenant
22+
job.perform_now
23+
end
24+
")
25+
end
26+
end
27+
928
def perform_now
1029
MultiTenantSupport.under_tenant(current_tenant) do
1130
super
@@ -43,6 +62,25 @@ def find_current_tenant(data)
4362
tenant_klass.find tenant_id
4463
end
4564
end
65+
66+
module ConfiguredJob
67+
if Gem::Version.new(Rails.version) < Gem::Version.new("7.0.0.alpha1")
68+
def perform_now(*args)
69+
job = @job_class.new(*args)
70+
job.current_tenant = MultiTenantSupport.current_tenant
71+
job.perform_now
72+
end
73+
else
74+
eval("
75+
def perform_now(...)
76+
job = @job_class.new(...)
77+
job.current_tenant = MultiTenantSupport.current_tenant
78+
job.perform_now
79+
end
80+
")
81+
end
82+
end
4683
end
4784

48-
ActiveJob::Base.include(MultiTenantSupport::ActiveJob)
85+
ActiveJob::Base.include(MultiTenantSupport::ActiveJob)
86+
ActiveJob::ConfiguredJob.prepend(MultiTenantSupport::ConfiguredJob)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module MultiTenantSupport
2-
VERSION = '1.1.0'
2+
VERSION = '1.1.1'
33
end

test/integration/active_job_test.rb renamed to test/integration/active_job_perform_later_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
require 'sidekiq'
33
require 'sidekiq/testing'
44

5-
class ActiveJobIntegrationTest < ActiveSupport::TestCase
5+
class ActiveJobPeroformLaterIntegrationTest < ActiveSupport::TestCase
66

77
setup do
88
MultiTenantSupport.under_tenant(accounts(:amazon)) do
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require 'test_helper'
2+
3+
class ActiveJobPeroformNowIntegrationTest < ActiveSupport::TestCase
4+
5+
test 'update succes update user when tenant account match' do
6+
under_tenant(amazon) do
7+
UserNameUpdateJob.perform_now(bezos)
8+
9+
assert_equal "Jeff Bezos UPDATE", bezos.reload.name
10+
end
11+
end
12+
13+
test 'fail to update user when tenant account is missing' do
14+
under_tenant nil do
15+
assert_raise(MultiTenantSupport::MissingTenantError) do
16+
UserNameUpdateJob.perform_now(bezos)
17+
end
18+
end
19+
20+
under_tenant(amazon) do
21+
assert_equal "Jeff Bezos", @bezos.reload.name # Does not change
22+
end
23+
end
24+
25+
test 'fail to update user when tenant account is not match' do
26+
under_tenant(apple) do
27+
assert_raise(MultiTenantSupport::InvalidTenantAccess) do
28+
UserNameUpdateJob.perform_now(bezos)
29+
end
30+
end
31+
32+
under_tenant(amazon) do
33+
assert_equal "Jeff Bezos", @bezos.reload.name # Does not change
34+
end
35+
end
36+
37+
end
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
require 'test_helper'
2+
require 'sidekiq'
3+
require 'sidekiq/testing'
4+
5+
class ActiveJobPreconfiguredPeroformLaterIntegrationTest < ActiveSupport::TestCase
6+
7+
teardown do
8+
Sidekiq::RetrySet.new.clear
9+
end
10+
11+
test 'update succes update user when tenant account match' do
12+
under_tenant(amazon) do
13+
UserNameUpdateJob.set(queue: :integration_tests).perform_later(bezos)
14+
end
15+
16+
sleep 0.5
17+
18+
under_tenant(amazon) do
19+
assert_equal "Jeff Bezos UPDATE", bezos.reload.name
20+
end
21+
end
22+
23+
test 'fail to update user when tenant account is missing on enqueue' do
24+
UserNameUpdateJob.set(queue: :integration_tests).perform_later(bezos)
25+
26+
sleep 0.1
27+
28+
Sidekiq.redis do |connection|
29+
retries = connection.zrange "retry", 0, -1
30+
assert 1, retries.count
31+
failed_job_data = JSON.parse(retries.last)
32+
assert_equal "Error while trying to deserialize arguments: MultiTenantSupport::MissingTenantError", failed_job_data["error_message"]
33+
assert_equal "ActiveJob::DeserializationError", failed_job_data["error_class"]
34+
end
35+
36+
under_tenant(amazon) do
37+
assert_equal "Jeff Bezos", bezos.reload.name # Does not change
38+
end
39+
end
40+
41+
test 'fail to update user when tenant account is not match' do
42+
under_tenant(apple) do
43+
UserNameUpdateJob.set(queue: :integration_tests).perform_later(bezos)
44+
end
45+
46+
sleep 0.2
47+
48+
Sidekiq.redis do |connection|
49+
retries = connection.zrange "retry", 0, -1
50+
assert 1, retries.count
51+
failed_job_data = JSON.parse(retries.last)
52+
assert_equal %Q{Error while trying to deserialize arguments: Couldn't find User with 'id'=#{bezos.id} [WHERE "users"."account_id" = $1]}, failed_job_data["error_message"]
53+
assert_equal "ActiveJob::DeserializationError", failed_job_data["error_class"]
54+
end
55+
56+
under_tenant(amazon) do
57+
assert_equal "Jeff Bezos", bezos.reload.name # Does not change
58+
end
59+
end
60+
61+
end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require 'test_helper'
2+
3+
class ActiveJobPreconfiguredPeroformNowIntegrationTest < ActiveSupport::TestCase
4+
5+
test 'update succes update user when tenant account match' do
6+
under_tenant(amazon) do
7+
UserNameUpdateJob.set(queue: :integration_tests).perform_now(bezos)
8+
9+
assert_equal "Jeff Bezos UPDATE", bezos.reload.name
10+
end
11+
end
12+
13+
test 'fail to update user when tenant account is missing' do
14+
under_tenant nil do
15+
assert_raise(MultiTenantSupport::MissingTenantError) do
16+
UserNameUpdateJob.set(queue: :integration_tests).perform_now(bezos)
17+
end
18+
end
19+
20+
under_tenant(amazon) do
21+
assert_equal "Jeff Bezos", @bezos.reload.name # Does not change
22+
end
23+
end
24+
25+
test 'fail to update user when tenant account is not match' do
26+
under_tenant(apple) do
27+
assert_raise(MultiTenantSupport::InvalidTenantAccess) do
28+
UserNameUpdateJob.set(queue: :integration_tests).perform_now(bezos)
29+
end
30+
end
31+
32+
under_tenant(amazon) do
33+
assert_equal "Jeff Bezos", @bezos.reload.name # Does not change
34+
end
35+
end
36+
37+
end

0 commit comments

Comments
 (0)