How do I handle Memcached::ServerIsMarkedDead error with Rails? - ruby-on-rails

In my Rails app, I changed my model's find method to search into the cache before looking into the database, but I sometime get this error:
Memcached::ServerIsMarkedDead
While waiting for Memcached server to be up again, how should I handle this error and force Rails to search into the database?
Thank you,
Kevin

You should be able to just rescue the exception and search the database in that case.
Rails.cache.fetch encapsulates this pattern in a more generic way, and you may want to look into that instead of hacking find, which is likely to bite you soon when Rails 3 comes out.

Related

Manually deleted a row in postgresql and now ActiveRecord is breaking

My Rails app is using ActiveRecord backed by Postgres. I went into the database through psql and deleted a row from one of the tables. In hindsight this was a terrible decision as it is causing parts of the app to crash that are trying to reference that object.
Things like:
undefined method `first_name' for nil:NilClass
Or
Couldn't find Person with id=137
So now I have two questions.
How do I fix this?
Is manipulating the data through rails console production the best way to manually update it? Or is should I avoid that too?
Example of code
Reward.all.order("created_at DESC").where(warning: false, created_at: #yearrange).page(params[:page])
Where Reward contains a reference to the Person id
Contrary to e.g. PHP, ruby/Rack applications such as Rails stay loaded in memory for use by subsequent requests. Chances are a reference to the old data is referenced in a cache somewhere. Try to get the app server to reload to fix.
A more likely culprit, that said, is memcached or similar if you use such a thing. Flush it as needed.
I'm not familiar enough with Rails to be sure on this last point, but I'd imagine the Rails console uses your app's code flow and flushes caches as needed when it does.
Note: as point out by #muistooshort in the comments, it might also be a dangling foreign key somewhere if you didn't create the FK cnstraints in your schema.

clarifying rails find method

I call the rails find method a lot during my view. Is this a generally accepted good practice? I'm leaning towards no, but why? Does find call the database each time? Does it cache the result anywhere? I looked up the ruby/rails docs on find but they didn't specify if it actually made a call to the database each time or not.
No, it is not a good idea to call find from the view. It is the job of a controller to load and assemble all the data that a view will need, and pass it to the view for presentation to the user.
Repeated calls to find for the same object should be cached by Rails, so it wouldn't hit the database each time, unless the arguments or other parameters were different.
A realtively easy way to test this would be to fire up a terminal and run rails c. In the console, run the code that returns tplangroup and play with it in the console. You should be able to see database call in the development console.
Based on my experience (not at a computer with rails right now) I think it would hit the database in each of these calls, unless you created tplangroup using the .includes(:tplan) for eager loading.
Hope this helps

log an exception myself, just like Rails

If your app raises an exception not caught by your app, then Rails will log it, using it's own rules for when to log, what to log (backtrace clean), how to format the log, etc. And will then present a 500 error message.
I am rescue'ing some exceptions myself, because I can recover from them with a better user experience than a generic 500 error.
However, they still represent unexpected conditions that should not have been possible, and I still want to log them -- I want to log them just like Rails does.
One would think there would be a method in Rails I can actually call myself to do this. Alternately, if needed, if I could find where in Rails does this, I could just copy and paste duplicate the logic to do it the same way.
But I'm having trouble finding what's what in Rails regarding this stuff at all. The code is confusing, spread accross middleware as well as Rails itself, different parts that look like they're going to tell me what I need but then don't seem to, etc.
Anyone have any ideas?

Preserve external changes in CouchDB with CouchRest Model

I'm using couchrest_model to manage some DBs in Rails. So far, it worked like a charm, but I noticed that if I PUT some data via HTTP request, CouchRest Model doesn't seem to realise that the changes are made, so it wipes off the whole record. Of course, I can see the changes in Futon, but not in Rails. When I enter the console, the previously saved instance is just not there.
Of course, I could use HTTP all the way, but I'd really like to make use of validations and other goodies that are available in ActiveRecord class.
Is there any chance that I can make these two guys work together?
P.S.
If you think/know that this approach will work with any other CouchDB Ruby/Rails gem, please, do tell! =)
I've mentioned CouchRest Model because IMO it's the most up-to-date and advanced gem out there.
I realised that this one was so damn easy, it's just that I was using the wrong tool (apart from being a proper n00b). AFAICT, it's not possible to use CouchRest Model solely to carry out persistent operations on CouchDB backend. All external calls that alter the database record(s) in certain way will somehow "remove" that record from ActiveARecord. Instead, you'd probably like to use CouchPotato, since it supports persistent operations.
I'll be glad to give checkmark if anyone comes up with vaguely better idea that this one.

Does a rollback still occur if I use begin...rescue and an error occurs?

I've got some strange errors happening in my rails app and I'm trying to log better errors instead of the whole stack of passenger stuff that I don't care about. I thought I would do this with a Rescue clause and explicit error handling, like logging the params hash. But I'm concerned if this would interrupt any rollback that is happening. For that matter, I'm assuming rollbacks automatically occur when an error occurs as part of the normal rails error handling, but I haven't been able to find that documented anywhere. I'm using Dreamhost with MySQL, so I thought transactions and rollbacks were happening there.
This is not very advisable (to put a big begin-rescue on your code).
Why don't you use backtrace silencers? (from Rails 2.3)
http://afreshcup.com/home/2008/11/29/rails-23-backtrace-silencing.html.
From release notes:
Rails automatically adds silencers to
get rid of the most common noise in a
new application, and builds a
config/backtrace_silencers.rb file to
hold your own additions
If you use an earlier version of Rails, use http://github.com/thoughtbot/quietbacktrace.

Resources