BigW Consortium Gitlab
Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gitlab-ce
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Forest Godfrey
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
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
8 deletions
+26
-8
forbid_sidekiq_in_transactions.rb
config/initializers/forbid_sidekiq_in_transactions.rb
+26
-8
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
return
super
(
*
args
)
if
Sidekiq
::
Worker
.
inside_after_commit
return
super
(
*
args
)
unless
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.
`
#{
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.
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
end
super
(
*
args
)
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