Skip to content

Commit

Permalink
futzing about
Browse files Browse the repository at this point in the history
  • Loading branch information
ariccio committed Mar 22, 2024
1 parent 3ef709e commit f311a2e
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 16 deletions.
2 changes: 1 addition & 1 deletion config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ development:
username: postgres
password: <%= Rails.application.credentials.dig(:development, :postgres_password) %>
timeout: 1000
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 10 } %>
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 15 } %>

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
Expand Down
38 changes: 37 additions & 1 deletion lib/support/manages_transactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ def color(text, color)
end

def begin_transaction
Thread.list.each_with_index do |t, i|
puts("Thread #{i}: alive?: #{t.alive?}")
puts("Thread #{i}: stop?: #{t.stop?}")
t.backtrace.take(15).each_with_index do |line, index|
puts("\t\tbacktrace #{index}: #{line}")
end
puts("Thread #{i}: group: #{t.group}")
puts("Thread #{i}: status: #{t.status}")
puts("Thread #{i}: inspect: #{t.inspect}")
end

puts (color("connection_pool_names: #{ActiveRecord::Base.connection_handler.connection_pool_names}", :BLUE))
ActiveRecord::Base.connection_handler.flush_idle_connections!
ActiveRecord::Base.connection_handler.clear_active_connections!(:all)
Expand All @@ -79,7 +90,17 @@ def begin_transaction
@connection_subscriber = ActiveSupport::Notifications.subscribe("!connection.active_record") { |name, started, finished, data, payload|
# puts payload


Thread.list.each_with_index do |t, i|
puts("Thread #{i}: alive?: #{t.alive?}")
puts("Thread #{i}: stop?: #{t.stop?}")
t.backtrace.take(15).each_with_index do |line, index|
puts("\t\tbacktrace #{index}: #{line}")
end
puts("Thread #{i}: group: #{t.group}")
puts("Thread #{i}: status: #{t.status}")
puts("Thread #{i}: inspect: #{t.inspect}")
end

Rails.logger.debug(color("ATTEMPT beginning transaction for #{name}, #{started}, #{finished}, #{data}, #{payload}", :RED))
if payload.key?(:spec_name) && (spec_name = payload[:spec_name])
setup_shared_connection_pool
Expand All @@ -104,10 +125,25 @@ def begin_transaction
end

def rollback_transaction
Thread.list.each_with_index do |t, i|
puts("Thread #{i}: alive?: #{t.alive?}")
puts("Thread #{i}: stop?: #{t.stop?}")
t.backtrace.take(15).each_with_index do |line, index|
puts("\t\tbacktrace #{index}: #{line}")
end
puts("Thread #{i}: group: #{t.group}")
puts("Thread #{i}: status: #{t.status}")
puts("Thread #{i}: inspect: #{t.inspect}")
end

if @connections.present?
# puts("rollback_transaction: connections present to rollback")
else
puts(color("rollback_transaction: NO connections present to rollback", :RED))
ActiveRecord::Base.connection_handler.connection_pool_list.each do |pool|
pool.reap
end

return
end

Expand Down
24 changes: 19 additions & 5 deletions lib/support/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,14 @@ def using_ssl?
end

def responsive?
return false if @server_thread&.join(0)
# return false if @server_thread&.join(0)
if @server_thread&.join(0)
return false
end

res = @checker.request { |http| http.get("/__identify__") }

res = @checker.request { |http| http.get("/__identify__") }
# puts("res: #{res}")
res.body == app.object_id.to_s if res.is_a?(Net::HTTPSuccess) || res.is_a?(Net::HTTPRedirection)
rescue SystemCallError, Net::ReadTimeout, OpenSSL::SSL::SSLError => e
puts("NOT responsive! #{e.full_message}")
Expand All @@ -64,7 +68,7 @@ def responsive?

def wait_for_pending_requests
puts("waiting for pending requests...")
timer = Timer.new(60)
timer = Timer.new(10)
while pending_requests?
raise "Requests did not finish in 60 seconds: #{middleware.pending_requests}" if timer.expired?

Expand All @@ -73,21 +77,31 @@ def wait_for_pending_requests
end

def boot
unless responsive?
Thread.abort_on_exception = true
puts("booting backend cypress db manager server...")
is_responsive = responsive?
Thread.report_on_exception = true
puts("is_responsive: #{is_responsive}")
unless is_responsive
puts("not responsive, booting server...")
Server.ports[port_key] = port

@server_thread = Thread.new {
puts("starting puma for backend cypres reset server...")
Puma.create(middleware, port, host)
}

puts("server thread: #{@server_thread}")
puts("server thread status: #{@server_thread.status}")
timer = Timer.new(60)
until responsive?
raise "Rack application timed out during boot" if timer.expired?

@server_thread.join(0.1)
puts("server thread: #{@server_thread}")
@initializer_hooks.run(:after_server_start)
end
puts("server thread: #{@server_thread}")
puts("server thread status: #{@server_thread.status}")
end

self
Expand Down
2 changes: 2 additions & 0 deletions lib/support/server/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def initialize(app, server_errors, extra_middleware = [])
end

def pending_requests
puts("pending_requests: #{@counter.value}")
@counter.value
end

Expand All @@ -51,6 +52,7 @@ def clear_error
end

def call(env)
puts("env: #{env}")
if env["PATH_INFO"] == "/__identify__"
[200, {}, [@app.object_id.to_s]]
else
Expand Down
22 changes: 14 additions & 8 deletions lib/support/starts_rails_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@
require_relative "server"

module FakeCypressRailsRunner

class Responder
def call(env)
# puts("env: #{env}")
if Rails.env.production?
raise StandardError, "Logic error - do not continue"
end
puts("\n\n\n\n\nRESETTING DB\n\n\n\n\n")
TracksResets.instance.reset_needed!
[202, {"Content-Type" => "text/plain"}, ["Accepted"]]
end
end

class StartsRailsServer
def call(host:, port:, transactional_server:)
configure_rails_to_run_our_state_reset_on_every_request!(transactional_server)
Expand All @@ -29,14 +42,7 @@ def create_rack_app
Rack::Builder.new do
use Rack::CommonLogger
map "/cypress_rails_reset_state" do
run lambda { |env|
if Rails.env.production?
raise StandardError, "Logic error - do not continue"
end
puts("\n\n\n\n\nRESETTING DB\n\n\n\n\n")
TracksResets.instance.reset_needed!
[202, {"Content-Type" => "text/plain"}, ["Accepted"]]
}
run Responder.new
end
# map "/" do
# run Rails.application
Expand Down
22 changes: 21 additions & 1 deletion lib/support/tracks_resets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,32 @@ def self.instance
end

def reset_needed!
Thread.list.each_with_index do |t, i|
puts("Thread #{i}: alive?: #{t.alive?}")
puts("Thread #{i}: stop?: #{t.stop?}")
t.backtrace.take(15).each_with_index do |line, index|
puts("\t\tbacktrace #{index}: #{line}")
end
puts("Thread #{i}: group: #{t.group}")
puts("Thread #{i}: status: #{t.status}")
puts("Thread #{i}: inspect: #{t.inspect}")
end
@reset_needed = true
end

def reset_state_if_needed(transactional_server)
if @reset_needed
puts("reset needed from backend cypress db manager rails server...")
Thread.list.each_with_index do |t, i|
puts("Thread #{i}: alive?: #{t.alive?}")
puts("Thread #{i}: stop?: #{t.stop?}")
t.backtrace.take(15).each_with_index do |line, index|
puts("\t\tbacktrace #{index}: #{line}")
end
puts("Thread #{i}: group: #{t.group}")
puts("Thread #{i}: status: #{t.status}")
puts("Thread #{i}: inspect: #{t.inspect}")
end
puts("reset needed from backend cypress db manager rails server...")
ResetsState.new.call(transactional_server: transactional_server)
@reset_needed = false
end
Expand Down

0 comments on commit f311a2e

Please sign in to comment.