Skip to content

Instantly share code, notes, and snippets.

@jpcody

jpcody/wtf.rb Secret

Last active April 23, 2016 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpcody/e5b6c7e0e5937cc0855ef34c753a7d19 to your computer and use it in GitHub Desktop.
Save jpcody/e5b6c7e0e5937cc0855ef34c753a7d19 to your computer and use it in GitHub Desktop.
class Client
after_create :reset_subdomains
after_destroy :reset_subdomains
# …
def self.subdomains
Rails.logger.fatal "[#{Thread.current.object_id}] Client.subdomains"
Rails.logger.fatal "[#{Thread.current.object_id}] Object: #{Thread.current[:client_subdomains].object_id}"
Rails.logger.fatal "[#{Thread.current.object_id}] Stored: #{Thread.current[:client_subdomains]}"
Rails.logger.fatal "[#{Thread.current.object_id}] Actual: #{Client.pluck(:subdomain)}"
Rails.logger.fatal ("-" * 50)
Thread.current[:client_subdomains] || Client.pluck(:subdomain)
end
def reset_subdomains
Rails.logger.fatal "[#{Thread.current.object_id}] Client#reset_subdomains"
Rails.logger.fatal "[#{Thread.current.object_id}] New: #{Client.pluck(:subdomain)}"
Rails.logger.fatal ("-" * 50)
Thread.current[:client_subdomains] = Client.pluck(:subdomain)
end
# …
end
logged = <<-TXT
F, [2016-04-23T20:18:03.904424 #13046] FATAL -- : [69886706129300] Client.subdomains
F, [2016-04-23T20:18:03.904567 #13046] FATAL -- : [69886706129300] Object: 69886746828520
F, [2016-04-23T20:18:03.904718 #13046] FATAL -- : [69886706129300] Stored: […]
F, [2016-04-23T20:18:03.906526 #13046] FATAL -- : [69886706129300] Actual: […]
F, [2016-04-23T20:18:03.906688 #13046] FATAL -- : --------------------------------------------------
F, [2016-04-23T20:18:05.191462 #13040] FATAL -- : [69886706129300] Client.subdomains
F, [2016-04-23T20:18:05.191662 #13040] FATAL -- : [69886706129300] Object: 8
F, [2016-04-23T20:18:05.191773 #13040] FATAL -- : [69886706129300] Stored:
F, [2016-04-23T20:18:05.199315 #13040] FATAL -- : [69886706129300] Actual: […]
F, [2016-04-23T20:18:05.199489 #13040] FATAL -- : --------------------------------------------------
F, [2016-04-23T20:18:45.805147 #13043] FATAL -- : [69886706129300] Client.subdomains
F, [2016-04-23T20:18:45.805327 #13043] FATAL -- : [69886706129300] Object: 8
F, [2016-04-23T20:18:45.805434 #13043] FATAL -- : [69886706129300] Stored:
F, [2016-04-23T20:18:45.807298 #13043] FATAL -- : [69886706129300] Actual: […]
F, [2016-04-23T20:18:45.807444 #13043] FATAL -- : --------------------------------------------------
F, [2016-04-23T20:18:45.828466 #13043] FATAL -- : [69886706129300] Client#reset_subdomains
F, [2016-04-23T20:18:45.830039 #13043] FATAL -- : [69886706129300] New: […, "joshtest12"]
F, [2016-04-23T20:18:45.830213 #13043] FATAL -- : --------------------------------------------------
F, [2016-04-23T20:18:45.937948 #13040] FATAL -- : [69886706129300] Client.subdomains
F, [2016-04-23T20:18:45.938102 #13040] FATAL -- : [69886706129300] Object: 8
F, [2016-04-23T20:18:45.938215 #13040] FATAL -- : [69886706129300] Stored:
F, [2016-04-23T20:18:45.939900 #13040] FATAL -- : [69886706129300] Actual: […, "joshtest12"]
F, [2016-04-23T20:18:45.940107 #13040] FATAL -- : --------------------------------------------------
F, [2016-04-23T20:18:52.485299 #13043] FATAL -- : [69886706129300] Client.subdomains
F, [2016-04-23T20:18:52.485427 #13043] FATAL -- : [69886706129300] Object: 69886755454500
F, [2016-04-23T20:18:52.485655 #13043] FATAL -- : [69886706129300] Stored: […, "joshtest12"]
F, [2016-04-23T20:18:52.487545 #13043] FATAL -- : [69886706129300] Actual: […, "joshtest12"]
F, [2016-04-23T20:18:52.487688 #13043] FATAL -- : --------------------------------------------------
F, [2016-04-23T20:18:54.089372 #13046] FATAL -- : [69886706129300] Client.subdomains
F, [2016-04-23T20:18:54.089522 #13046] FATAL -- : [69886706129300] Object: 69886746828520
F, [2016-04-23T20:18:54.089691 #13046] FATAL -- : [69886706129300] Stored: […]
F, [2016-04-23T20:18:54.091523 #13046] FATAL -- : [69886706129300] Actual: […, "joshtest12"]
F, [2016-04-23T20:18:54.091668 #13046] FATAL -- : --------------------------------------------------
TXT
@jpcody
Copy link
Author

jpcody commented Apr 23, 2016

The thread's object_id never changes, so I assume we're always dealing with the same thread, as I expect when working with Unicorn.

  • On line 41, I add the "joshtest12" subdomain, then for some god-forsaken reason, on line 45, Client.subdomains evaluates to nil.
  • On line 50, we get a new object_id, which is what I expect, as the thread_local variable should have been reset.
  • On line 55, it somehow magically reverts back to the same array from line 27, which was before we added the "josh12" subdomain.

Another random tidbit, when I look at Thread.current.keys, I see two keys being removed: [:client_subdomains, :"current-70168851621200"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment