Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hook for thread safety #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

StevePotter
Copy link

@StevePotter StevePotter commented Mar 16, 2018

Thanks for creating such a great gem @lazebny. It's been really helpful. We have 2 main web apps. One is Rails and the other uses Cuba and Sequel. We added paper_trail to the first one but had no answers for the second. Your gem has made it possible for us to use paper_trail across both.

We encountered a problem when implementing this gem. Before, SequelPaperTrail.whodunnit and SequelPaperTrail.info_for_paper_trail were global and shared among all threads. This is a problem in a web server that serves concurrent requests in separate threads:

  1. Request 1 comes in on thread A for user Paul
  2. Server sets SequelPaperTrail.whodunnit to 'Paul'
  3. Request 2 comes in on thread B for user Amy
  4. Server sets SequelPaperTrail.whodunnit to 'Amy'
  5. Request 1 changes a model tracked in paper_trail
  6. versions table gets a new record with whodunnit of 'Amy' and NOT 'Paul'.

Corrupted data! The regular paper_trail gem has a mechanism to bypass this. So I did a quick fix, where SequelPaperTrail.whodunnit and SequelPaperTrail.info_for_paper_trail can also be a lambda or proc.

This allowed us to define a lambda that uses the current request as a context for getting the value for the current user ID. No more corrupt data!

I hope you'll review and merge this PR and publish a new version. Thanks!

@coveralls
Copy link

coveralls commented Mar 16, 2018

Coverage Status

Coverage remained the same at 100.0% when pulling e1a51c1 on Vydia:improve-thread-safety into fae8c31 on lazebny:master.

@lazebny
Copy link
Owner

lazebny commented Mar 17, 2018

Hey @StevePotter, thank you for PR. Changes look reasonable to me and i am glad that you found this gem useful.

Would you mind to do:

  • squash commits in one
  • add a basic test for it

@StevePotter
Copy link
Author

@lazebny sure. Sorry for not testing. I was being a bit lazy. I'll add tests and squash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants