Last active
May 1, 2024 22:36
-
-
Save jgaskins/9aa94c43748a90141363cedc2cced21b to your computer and use it in GitHub Desktop.
ActiveSupport::Executor example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "active_support/executor" | |
require "active_record" | |
require "pg" | |
ActiveRecord::Base.establish_connection( | |
uri: "postgres:///", | |
adapter: :postgresql, | |
pool: 5, | |
) | |
# Just happens to map to a table this DB | |
class Task < ActiveRecord::Base | |
end | |
# Executed on completion of any `Executor.wrap` block | |
ActiveSupport::Executor.to_complete do | |
# Release this thread's connection back to the pool | |
ActiveRecord::Base.connection_pool.release_connection | |
end | |
# Wrap your app code in this block, for example in Rack middleware | |
ActiveSupport::Executor.wrap { Task.first } | |
# The connection's owner should be `nil` | |
pp ActiveRecord::Base.connection_pool.connections.first.owner |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment