Rails - logging server errors to DB - ruby-on-rails

If I get 500 errors in my server logs, I would prefer to store them to the DB in a table like server_errors (id, user_id, created_at, build_version, error_code, error_output).
My initial idea is to just run a script that constantly greps log/production.log for errors and stores them to the DB as they come in, but this seems like an ugly solution and gets unwieldy when you consider things like log rotation etc.
This seems like a common enough feature that there should be a gem for it, or some standard functionality built into Rails (2.3.12) so I was hoping that somebody might know of something like that. Or could suggest a better methodology if you can think of one.

Most folks use exception_notification, NewRelic RPM, or AirBrake (formerly known as Hoptoad) to see their exceptions.
If you would like to roll your own, take a look at how exception_notification does it. It uses a middleware component to catch the exceptions. You could catch the exceptions in your own custom middleware, then save the information to the database table you described above instead of emailing it.
For background queueing, see how resque does it.
If you're using Rails 2.3, you should use the 2-3-stable branch of exception_notification instead of the master branch.

Related

Optimize a backend rails app

I have a rails app which serves as a backend for Bootstrap based frontend. My application is fully Rest based. Which tools can I use to find the unused joins or queries from database in code? Also, any tools/gems that can help in finding the code that can be refactored?
There are gems like Bullet or Rack-mini-profiler that work if Rails app is used to render view also. But how do I use gems like those to optimize and tune my queries and code if only way to get a response is from rest client?
Depending on the configuration of Bullet, I believe by default Bullet logs all optimizable queries in (Rails.root/log/bullet.log), I guess the others have similar configurations/logs.
You could setup bullet for your test environment, since if you're shooting for a good coverage you'd most likely be testing all the AR methods in the first place.
Additional enhancers are New Relic and tools like Rubocop for some best practice.
You should check this out as well.

Generate and send xml on model save

I need to generate xml from a model and send it to a web service on model save.
I'm sure this is a common case and should be straight forward. Just create a job on after_save callback that generates the xml and sends it to the endpoint.
Since I'm new to Ruby on Rails I'm not to sure how to handle this though. My questions are more about code organization. It's not unlikely that this api connection will be discontinued in the future so I need a clean modular way to get rid of it. Would it be best practice/convention to put this in a separate gem? Can gems actually add jobs to an existing rails queue? Can gems create migrations on install? I'll probably need to add a model to keep track of the api sync. How about dropping a table on gem uninstall? Or should I not use a gem for this at all?
I realize these are broad and basic Ruby on Rails questions but I'm kind of drowning in documentation. I'm just hoping for some examples and/or advice and maybe some pointers to relevant documentation. Thanks.
Gem installs/uninstalls are unrelated to apps, they live on different level and do not khow anything about your app code, db and so on unless they are loaded.
Gems for rails can provide rake tasks and/or generators, for example you can look into devise gem structure on how it does this.
But i'd advise against moving code to a gem before you know you have to, like for example when you need to reuse it in different project.
To reuse code inside single project - use mixins/concerns
In general:
don't make it a gem
it's an unnecessary world of pain, pretty much always,
never make anything a gem unless you intend to use it in the same way in 3+ applications
don't extract it into a concern either,
it doesn't seem very likely that you'll do the same operation on multiple models, code reuse seems to not be an issue here and you can actually reuse code more efficiently using service classes too
a lot of experienced Rails programmers regard this practice as concerning, forgive the pun. It seems this view is not shared by the Rails development team, but at least from my experience writing service classes seems like unnecessary complexity until your project grows enough and then you need to refactor a BUNCH of stuff and you realize you would have been better off ditching concerns from the start
use a service class instead and delegate the necessary methods to it from the model
this will leave you with a clean interface to extract later and will also allow you to use dependency injection if you need to mock your XML service for tests
don't tie API requests to model callbacks, there's usually just 2-3 places where you need to do something with the API and a bunch of other cases where that may not be the case, imagine:
tests,
or if you get a requirement to implement cache column,
or a "number of visits" column
or a gem like Paperclip that thought that it wanted to add something to the model but changed his mind and instead of that just touched updated_at
or any such trickery which will make you a grandiose API spammer and a sufferer of VERRRRY slow database updates
if you DO tie API requests to model callbacks,
then you better make sure that error handling is done properly and that timeouts etc don't rollback or delay your DB operation,
best way from my experience is to run these things through ActiveJob + one of the backends (though obviously not the :inline backend and ideally one of the backends which don't use your main database and allow asynchronous job submission - sidekiq comes to mind as a candidate)

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.

Is there a bulk email plugin for Rails apps?

Does anyone know of a plugin or something that can be used to send bulk emails for a Rails app?
Specifically, I'd like to be able to pass an HTML email file to a rake task or something and have it emailed out to everyone who has signed up to my site and checked the "please send me info about XXX" box.
I wrote kind of a hacked-together version for myself, but I'd like something that throttles itself somewhat smartly and can pick up where it left off if interrupted.
Update: I eventually broke down and got out my credit card and signed up for a real bulk email service, and damn was that the right choice. The resulting emails are very professional, they have built-in analytics, also integrate with Google Analytics, and it's awesome for a ton of other reasons.
If you're looking to do bulk emails with Rails, I would suggest using the Mailchimp service (here's my affiliate link that has a bonus on signup) along with the hominid gem. This will allow you to sync all your user emails from your database to Mailchimp, then use a real bulk service instead of some crappy patched together one.
Another Update: I heard about Maktoub today, and it's pretty much exactly what I was describing. Disclaimer: I have never used it and would still probably steer clear and go with a paid service, but it's still probably better than rolling you own.
I couldn't find one so I wrote it myself. It's not pretty (at this stage), but should serve as a good starting point for anyone with similar needs.
Please send me a pull request if you make any beneficial changes and I'll make sure to give you credit.
Mailcar - Ruby on Rails mass / bulk email plugin
Update - I highly recommend using a service. Rolling your own is a real pain and it will be difficult to manage once your list becomes reasonably large at all.
I don't know that this is the kind of thing that can be covered by a plugin as there are whole sites/applications dedicated to this kind of thing. If you wanted to use one of those then there is www.campaignmonitor.com, it's pretty good and it has an api that you can hook into from your application.
You might want to take a look at postageapp.com
Not aware of any plugins for this and I don't know how rake could be used to do this, if at all.
Since you're using Rails, I assume you're using MySQL as your database so this may not be useful to you, but SQL Answers Mail for SQL Server sends bulk email directly from SQL Server. You could try searching for a tool that does something similar for the database you're using.
There's also a tutorial here and here on creating your own mailer.
use ActionMailer(tutorial) (docs), it comes with Rails and you should be able to rig it to run from a rake task.
There's a relatively new project called Maktoub which claims to be a Rails engine for email newsletters. I haven't tried it yet, but it looks promising.

Resources