Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
gitlab-ce
Commits
a9dbda86
Commit
a9dbda86
authored
Jun 01, 2017
by
Douwe Maan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow scheduling from after_commit hooks
parent
4edb505b
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
9 deletions
+27
-9
config/initializers/forbid_sidekiq_in_transactions.rb
config/initializers/forbid_sidekiq_in_transactions.rb
+27
-9
No files found.
config/initializers/forbid_sidekiq_in_transactions.rb
View file @
a9dbda86
module
Sidekiq
module
Sidekiq
module
Worker
module
Worker
mattr_accessor
:inside_after_commit
self
.
inside_after_commit
=
false
module
ClassMethods
module
ClassMethods
module
NoSchedulingFromTransactions
module
NoSchedulingFromTransactions
NESTING
=
::
Rails
.
env
.
test?
?
1
:
0
NESTING
=
::
Rails
.
env
.
test?
?
1
:
0
%i(perform_async perform_at perform_in)
.
each
do
|
name
|
%i(perform_async perform_at perform_in)
.
each
do
|
name
|
define_method
(
name
)
do
|*
args
|
define_method
(
name
)
do
|*
args
|
if
ActiveRecord
::
Base
.
connection
.
open_transactions
>
NESTING
return
super
(
*
args
)
if
Sidekiq
::
Worker
.
inside_after_commit
return
super
(
*
args
)
unless
ActiveRecord
::
Base
.
connection
.
open_transactions
>
NESTING
raise
<<-
MSG
.
strip_heredoc
raise
<<-
MSG
.
strip_heredoc
`
#{
self
}
.
#{
name
}
` cannot be called inside a transaction as this can lead to race
`
#{
self
}
.
#{
name
}
` cannot be called inside a transaction as this can lead to
conditions when the worker runs before the transaction is committed and tries to
race conditions when the worker runs before the transaction is committed and
access a model that has not been saved yet.
tries to
access a model that has not been saved yet.
Schedule the worker from insid
e a `run_after_commit` block instead.
Use an `after_commit` hook, or include `AfterCommitQueue` and us
e a `run_after_commit` block instead.
MSG
MSG
end
end
super
(
*
args
)
end
end
end
end
end
...
@@ -25,3 +27,19 @@ module Sidekiq
...
@@ -25,3 +27,19 @@ module Sidekiq
end
end
end
end
end
end
module
ActiveRecord
class
Base
module
InsideAfterCommit
def
committed!
(
*
)
inside_after_commit
=
Sidekiq
::
Worker
.
inside_after_commit
Sidekiq
::
Worker
.
inside_after_commit
=
true
super
ensure
Sidekiq
::
Worker
.
inside_after_commit
=
inside_after_commit
end
end
prepend
InsideAfterCommit
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment