How to use an ActiveRecord transaction in a Rake task to roll back in case of interruption Ctrl + C

I am writing a rake task that creates records in my database and can run an unlimited amount of time. One aspect of this task is the creation of many-to-many associations between models. If I left the task when adding records to the connection table, it would not complete this model. here is some code from the task:

(1..100).each do |page|

    related = Nokogiri::HTML(open(url + "item/#{item.id}/related/#{page}"))

    related.css('.box').each do |box|
      id = box.css('a').first.attr(:href).scan(/\/(\d+)\//)[0][0].to_i
      title = box.css('p.title a').text
      related_item = Item.create :title => title, :foreign_id => id
      ItemRelation.create :item => item, :related_item => related_item
    end

end

item.update_attributes :stage => ItemStage::RELATED

Here I repeat all the pages of related items and create an ItemRelation between the current item (this loop is inside another loop that iterates over the items, here is the variable 'item'). and all the related items that I get from scraping these pages.

, , .

, , , Ctrl + C, , , , ,

+3
1

, transaction. :

ActiveRecord::Base.transaction do
    # create relation 1
    # create relation 2
    # Ctrl+C
    # create relation 3
end

end , SQL COMMIT , , .

, - ItemRelation.transaction, , .

.

- Ctrl + C; , , - .

+11

All Articles