I've got a transaction which needs detailed logging, even if it fails. How can I do that?
This is my code:
# csv gets loaded etc.
import = CsvImport.new
import.logger += 'Starting import'
ActiveRecord::Base.transaction do
import.logger += 'CSV loaded, starting import'
csv_array.each_with_index do |row, index|
begin
unless importer.has_key?(row[0])
import.logger += 'Key not found'
raise StandardError
end
result = CsvImporter.import_line(row[0])
import.logger += 'Imported line #{index} successfully' if result
rescue
import.logger += 'Transaction aborted!'
end
end
import.logger += 'Transaction successful!'
end
In a failed transaction, the logger only has "Starting import" without any of the added strings inside the transaction - obviously. How can I keep that data?
The begin-rescue block is just a wrapper to catch exceptions. In your case you need, like you said, a transaction. You can use something like this
YourClass.transaction do
# do a lot of stuff
end
or
#yourmodel.transaction do
# do stuff !
end
Look at the docs: http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html
A combination of transaction and rescue block are possible: Look at the docs at section "Exception handling and rolling back"
Related
This is sample code which i am trying to handle exception from unicorn.
unicron.rb
worker_processes Integer(ENV['WEB_CONCURRENCY'] || 3)
timeout 15
preload_app true
timeout.rb
Rack::Timeout.timeout = 12
Sample code
def create_store
ActiveRecord::Base.transaction do
#store = Store.new(params[:store])
if #store.save!
sleep 12.3
InformUserWorker.perform_async(store_id)
end
end
rescue => exception
exception.backtrace.each { |trace| puts trace }
rescue Timeout::Error
puts 'That took too long, exiting...1'
rescue Rack::Timeout::RequestTimeoutException
puts 'That took too long, exiting...2'
rescue Rack::Timeout::RequestTimeoutError
puts 'That took too long, exiting...3'
rescue Rack::Timeout::RequestExpiryError
puts 'That took too long, exiting...4'
end
I am getting code=H13 desc="Connection closed without response" with this sleep 12.3 seconds, and the transaction rollback happens, but none of these exceptions are executing. I have added couple of exception here. Anything goes wrong here?.
Rescues are executed in order. So your first rescue captures anything that is being raised and the code never gets past that point.
Try putting it last:
# other rescues in here.
rescue => exception
exception.backtrace.each { |trace| puts trace }
end # this one is the last, only executed if none of the others are
If the problem is that you're not sure which class is being raised, just use something like pry to debug the exception.class
I have a rake task that loops through rows in CSV file, and inside that loop, there's a begin/rescue block to catch any possible raised exception. But when I run it, it keeps on saying 'rake aborted!' and it is not entering the rescue block
CSV.foreach(path, :headers => true) do |row|
id = row.to_hash['id'].to_i
if id.present?
begin
# call to mymethod
rescue => ex
puts "#{ex} error executing task"
end
end
end
...
def mymethod(...)
...
begin
response = RestClient.post(...)
rescue => ex
raise Exception.new('...')
end
end
Expected: It should finish looping all the rows of the CSV
Actual result: It stops after reaching the 'raise' exception saying that:
rake aborted!
Exception: error message here
...
Caused by:
RestClient::InternalServerError: 500 Internal Server Error
You can use next to skip the faulty step of loop:
CSV.foreach(path, :headers => true) do |row|
id = row.to_hash['id'].to_i
if id.present?
begin
method_which_doing_the_staff
rescue SomethingException
next
end
end
end
And raise the exception inside your method:
def method_which_doing_the_staff
stuff
...
raise SomethingException.new('hasd')
end
I solved this issue by just commenting out the line that is raising an exception because it seems like it the quickest fix for now.
# raise Exception.new('...')
I'm still open to other suggestions if there are any better ways to do it.
I use the capybara-webkit gem to scrape data from certain pages in my Rails application. I've noticed, what seems to be "random" / "sporadic", that the application will crash with the following error:
Capybara::Webkit::ConnectionError: /home/daveomcd/.rvm/gems/ruby-2.3.1/gems/capybara-webkit-1.11.1/bin/webkit_server failed to start.
from /home/daveomcd/.rvm/gems/ruby-2.3.1/gems/capybara-webkit-1.11.1/lib/capybara/webkit/server.rb:56:in `parse_port'
from /home/daveomcd/.rvm/gems/ruby-2.3.1/gems/capybara-webkit-1.11.1/lib/capybara/webkit/server.rb:42:in `discover_port'
from /home/daveomcd/.rvm/gems/ruby-2.3.1/gems/capybara-webkit-1.11.1/lib/capybara/webkit/server.rb:26:in `start'
from /home/daveomcd/.rvm/gems/ruby-2.3.1/gems/capybara-webkit-1.11.1/lib/capybara/webkit/connection.rb:67:in `start_server'
from /home/daveomcd/.rvm/gems/ruby-2.3.1/gems/capybara-webkit-1.11.1/lib/capybara/webkit/connection.rb:17:in `initialize'
from /home/daveomcd/.rvm/gems/ruby-2.3.1/gems/capybara-webkit-1.11.1/lib/capybara/webkit/driver.rb:16:in `new'
from /home/daveomcd/.rvm/gems/ruby-2.3.1/gems/capybara-webkit-1.11.1/lib/capybara/webkit/driver.rb:16:in `initialize'
from /home/daveomcd/.rvm/gems/ruby-2.3.1/gems/capybara-webkit-1.11.1/lib/capybara/webkit.rb:15:in `new'
from /home/daveomcd/.rvm/gems/ruby-2.3.1/gems/capybara-webkit-1.11.1/lib/capybara/webkit.rb:15:in `block in <top (required)>'
from /home/daveomcd/.rvm/gems/ruby-2.3.1/gems/capybara-2.7.1/lib/capybara/session.rb:85:in `driver'
from /home/daveomcd/.rvm/gems/ruby-2.3.1/gems/capybara-2.7.1/lib/capybara/session.rb:233:in `visit'
It happens even after it's already connected and accessed a website multiple times before. Here's a code snippet of what I'm using currently...
if site.url.present?
begin
# Visit the URL
session = Capybara::Session.new(:webkit)
session.visit(site.url) # here is where the error occurs...
document = Nokogiri::HTML.parse(session.body)
# Load configuration options for Development Group
roster_table_selector = site.development_group.table_selector
header_row_selector = site.development_group.table_header_selector
row_selector = site.development_group.table_row_selector
row_offset = site.development_group.table_row_selector_offset
header_format_type = site.config_header_format_type
# Get the Table and Header Row for processing
roster_table = document.css(roster_table_selector)
header_row = roster_table.css(header_row_selector)
header_hash = retrieve_headers(header_row, header_format_type)
my_object = process_rows(roster_table, header_hash, site, row_selector, row_offset)
rescue ::Capybara::Webkit::ConnectionError => e
raise e
rescue OpenURI::HTTPError => e
if e.message == '404 Not Found'
raise "404 Page not found..."
else
raise e
end
end
end
I've even thought perhaps I don't find out why it's happening necessarily - but just recover when it does. So I was going to do a "retry" in the rescue block for the error but it appears the server is just down - so I get the same result when retrying. Perhaps someone knows of a way I can check if the server is down and restart it then perform a retry? Thanks for the help!
So after further investigating it appears that I was generating a new Capybara::Session for each iteration of my loop. I moved it outside of the loop and also added Capybara.reset_sessions! at the end of my loop. Not sure if that helps with anything -- but the issue seems to have been resolved. I'll monitor it for the next hour or so. Below is an example of my ActiveJob code now...
class ScrapeJob < ActiveJob::Base
queue_as :default
include Capybara::DSL
def perform(*args)
session = Capybara::Session.new(:webkit)
Site.where(config_enabled: 1).order(:code).each do |site|
process_roster(site, session)
Capybara.reset_sessions!
end
end
def process_roster(site, session)
if site.roster_url.present?
begin
# Visit the Roster URL
session.visit(site.roster_url)
document = Nokogiri::HTML.parse(session.body)
# processing code...
# pass the session that was created as the final parameter..
my_object = process_rows( ..., session)
rescue ::Capybara::Webkit::ConnectionError => e
raise e
rescue OpenURI::HTTPError => e
if e.message == '404 Not Found'
raise "404 Page not found..."
else
raise e
end
end
end
end
end
I have spent all day on this and I cannot figure out how to use multithreading in rails, without causing the following error:
Uncaught exception: Circular dependency detected while autoloading constant Docket::System::ExecutableFactory
/Users/myuser/.rvm/gems/ruby-2.1.2#core/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:478:in `load_missing_constant'
/Users/myuser/.rvm/gems/ruby-2.1.2#core/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:180:in `const_missing'
The code works without threads, but as soon as I introduce threads, that is when I get the "circular dependency" error.
Below is the code in question:
def run
records.in_groups_of(4) do |records|
workers = records.map do |record|
Thread.new do
begin
options[:record] = record
county = create_county options
county.prepare
if county.ready
county.crawl
end
rescue ThreadError => e
puts "THREAD ERROR: #{e}"
end
end
end
workers.map(&:join)
end
end
...
def crawl
cmd = "#{executable_factory.path} #{crawler_path}"
Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
stdout_is = stdout.read
stderr_is = stderr.read
end
end
Docket::System::ExecutableFactory is one of my classes, but I have it properly nested. Directory structure:
\docket
\system
executable_factory.rb
And I have it properly namedspace:
module Docket
module System
class ExecutableFactory
...
end
end
end
So why does this error occur with threads and how can I fix it?
I use 'rubyoverflow' gem in rails:
begin
puts "=== 1 ==="
qt = Questions.retrieve_by_tag(tag).questions
puts "=== 2 ==="
rescue
puts "=== 3 ==="
end
But sometimes I get the error on the console:
couldn't parse YAML at line 843 column 4
C:/Ruby192/lib/ruby/1.9.1/psych.rb:148:in parse'
C:/Ruby192/lib/ruby/1.9.1/psych.rb:148:inparse_stream' ... rubyoverflow
(1.0.1) lib/rubyoverflow.rb:86:in request' ...
C:/Ruby192/lib/ruby/1.9.1/webrick/server.rb:183:inblock in start_thread'
But the question not about the error in the gem, but about the exception handling. I get in the console only line:
puts "=== 1 ==="
but not lines
puts "=== 2 ==="
puts "=== 3 ==="
why?
How can I restore esecution of my program if the line
qt = Questions.retrieve_by_tag(tag).questions
fails?
By default rescue traps StandardError and whatever inherits from it.
From the docs:
By default, rescue only intercepts StandardError and its descendants...
The exception being raised is probably not that, so rescue doesn't handle it.
Usually you can figure out what to use as rescue's parameter from the docs to the method raising it, but, if not, you can use
rescue Exception => e
print e.to_s
end
to see what the exception is, then replace Exception with that value.
More information is on the internet, but here's a piece of code to print a list of Exceptions.