How to use ruby-pg with async gem in Rails - ruby

With the new PR https://github.com/ged/ruby-pg/pull/397 in ruby-pg, it seems that we could use non-blocking methods with async gems in Ruby 3.0. Could we have one example to show the non-blocking way?

Related

NameError: undefined local variable or method `broadcast_messages' for #<RailsStdoutLogging::StdoutLogger:0x007f4d099fb6b0>

Title error when accessing ActiveRecord model only on heroku with rails 5.0.0-beta1. Works on production env heroku. Problem persists anytime Rails.logger is set to STDOUT wether between gem 'rails_stdout_logging' or in config/environment/production.rb
I've reviewed implementation and see broadcast_messages as a method created by the accessor macro. I'm at a loss and am looking for suggestions to troubleshoot.
Turns out heroku's rails_stdout_logging gem which is included as part of heroku's rails_12factor gem has a bug with rails 5.0.0-beta1.
StdoutLogger inherits from stdlib's Logger rather than ActiveSupport's Logger via < ::Logger. I've got a workaround on my fork but it injects an ActiveSupport dependency when not using rails. I'm new to ruby so I don't know best practice to make a more eloquent fix.
See https://github.com/MattWalston/rails_stdout_logging.

How to use Caching in ruby gem?

My gem will going to call some service to get data based on Parameters given. I want to cache my gem method. I don't know How to implement caching in gem.

Does this block the event loop?

I have a sinatra app that is using thin as it's web server. I interact with my database via ActiveRecord.
If in an endpoint, I do this:
get '/test' do
Model.create(.....)
end
Does the Model.create(.....) block the event loop (thin uses eventmachine internally)? If so, how bad is this and what are the alternatives?
Thanks
My understanding is that thin is threaded by default, unless you disable that. So your request handler can be executed asynchronously using Eventmachine::Defer.
Yes, as the comment by #user2246674 says, if your handler is executed in the reactor thread then it will block everything.
As to the alternatives, I just searched Google for ActiveRecord and Eventmachine and the first hit was this post with some code: http://www.mikeperham.com/2010/03/30/using-activerecord-with-eventmachine/. You might find it useful to take a look at "em-synchrony": https://github.com/igrigorik/em-synchrony that has support for ActiveRecord.
Here is also my own question on a related subject: async requests using sinatra streaming API There I use Sinatra's streaming API to implement asynchronous request processing.
I personally would use direct DB access from my Sinatra app. That is what I always do. ActiveRecord is too heavy for me.
Yes, it does block. If you are on Mysql you can make requests using mysql2 gem, it supports async requests.
https://github.com/brianmario/mysql2

Ruby on Rails with chargify

I want to integrate chargify to my rails app. I have user object and I want the user to be able to subscribe for one month and update the boolean column on user object. I prefer to use the API not hosted pages. How can I do that?
Is there any example for chargify on ruby on rails for handling subscriptions but with details about mvc for newbies?
Based on this thread and the Googles it looks like there is not a whole lot out there.
You could try looking at the Rails 2 example here and converting it or use the gem here (gem "chargify", "~> 0.3.0").
I know none of this is aimed at newbies but the info seems to sparse.
This might get you going. It seems that Chartify itself is written in Rails, and therfore their API is ruby code, which you can use...

Getting around ruby-units conflict with activesupport

I want to use the ruby-units with a rails 3 project, but it seems like it conflicts with activesupport.
It looks to me like both activesupport and ruby-units create a to() method for String. For some reason, ruby-units one wins, and so whenever to() is called inside rails it throws an error. (Unit not recognized)
I want to know the best way to deal with this. I don't care about having the to() method from ruby-units, I just don't want it to interfere with rails. I'd like to avoid forking if there's another approach.
To see my problem:
Add to your gemfile
gem 'ruby-units'
Open up rails console (I'm on rails 3 with ruby 1.9.2) and try apples.to(1)
Without ruby-units:
"ap"
With:
"'apples' Unit not recognized"
The answer to this is to require the ruby-units library before the rails library in the Gemfile:
gem 'ruby-units'
gem 'rails'
Obviously you then won't be able to use .to() on strings to access the ruby-units conversion.

Resources