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
Jérome Perrin
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
Hide 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
Worker
mattr_accessor
:inside_after_commit
self
.
inside_after_commit
=
false
module
ClassMethods
module
NoSchedulingFromTransactions
NESTING
=
::
Rails
.
env
.
test?
?
1
:
0
%i(perform_async perform_at perform_in)
.
each
do
|
name
|
define_method
(
name
)
do
|*
args
|
if
ActiveRecord
::
Base
.
connection
.
open_transactions
>
NESTING
raise
<<-
MSG
.
strip_heredoc
`
#{
self
}
.
#{
name
}
` cannot be called inside a transaction as this can lead to race
conditions when the worker runs before the transaction is committed and tries to
access a model that has not been saved yet.
return
super
(
*
args
)
if
Sidekiq
::
Worker
.
inside_after_commit
return
super
(
*
args
)
unless
ActiveRecord
::
Base
.
connection
.
open_transactions
>
NESTING
Schedule the worker from inside a `run_after_commit` block instead.
MSG
end
raise
<<-
MSG
.
strip_heredoc
`
#{
self
}
.
#{
name
}
` cannot be called inside a transaction as this can lead to
race conditions when the worker runs before the transaction is committed and
tries to access a model that has not been saved yet.
super
(
*
args
)
Use an `after_commit` hook, or include `AfterCommitQueue` and use a `run_after_commit` block instead.
MSG
end
end
end
...
...
@@ -25,3 +27,19 @@ module Sidekiq
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