Commit 356b649b9816e6c45937dd46e51643e8a2f6ae48
Exists in
staging
and in
42 other branches
Merge commit 'refs/merge-requests/157' of git://gitorious.org/noosfero/noosfero …
…into merge-requests/157 (ActionItem2277)
Showing
3 changed files
with
26 additions
and
1 deletions
Show diff stats
lib/feed_handler.rb
| ... | ... | @@ -17,8 +17,10 @@ class FeedHandler |
| 17 | 17 | |
| 18 | 18 | # The maximum number |
| 19 | 19 | cattr_accessor :max_errors |
| 20 | + cattr_accessor :disabled_period | |
| 20 | 21 | |
| 21 | 22 | self.max_errors = 6 |
| 23 | + self.disabled_period = 1.week | |
| 22 | 24 | |
| 23 | 25 | def parse(content) |
| 24 | 26 | raise FeedHandler::ParseError, "Content is nil" if content.nil? |
| ... | ... | @@ -52,6 +54,12 @@ class FeedHandler |
| 52 | 54 | RAILS_DEFAULT_LOGGER.info("Processing %s with id = %d" % [container.class.name, container.id]) |
| 53 | 55 | begin |
| 54 | 56 | container.class.transaction do |
| 57 | + if container.update_errors > FeedHandler.max_errors && container.fetched_at < (Time.now - FeedHandler.disabled_period) then | |
| 58 | + container.enabled = true | |
| 59 | + container.update_errors = 0 | |
| 60 | + container.save | |
| 61 | + end | |
| 62 | + next unless container.enabled | |
| 55 | 63 | actually_process_container(container) |
| 56 | 64 | container.update_errors = 0 |
| 57 | 65 | container.finish_fetch |
| ... | ... | @@ -63,6 +71,7 @@ class FeedHandler |
| 63 | 71 | container.update_errors += 1 |
| 64 | 72 | container.error_message = exception.to_s |
| 65 | 73 | if container.update_errors > FeedHandler.max_errors |
| 74 | + container.fetched_at = Time.now | |
| 66 | 75 | container.enabled = false |
| 67 | 76 | end |
| 68 | 77 | begin | ... | ... |
lib/feed_updater.rb
test/unit/feed_handler_test.rb
| ... | ... | @@ -112,6 +112,22 @@ class FeedHandlerTest < ActiveSupport::TestCase |
| 112 | 112 | assert !container.error_message.blank?, 'must set error message in container after <max_errors> errors (%s)' % container_class |
| 113 | 113 | assert !container.enabled, 'must disable continer after <max_errors> errors (%s)' % container_class |
| 114 | 114 | end |
| 115 | + | |
| 116 | + should "reenable after <disabled_period> (#{container_class})" do | |
| 117 | + FeedHandler.stubs(:max_errors).returns(4) | |
| 118 | + | |
| 119 | + container = create(container_class) | |
| 120 | + handler.stubs(:actually_process_container).with(container).raises(Exception.new("crash")) | |
| 121 | + # exceeds max_errors | |
| 122 | + 5.times { handler.process(container) } | |
| 123 | + | |
| 124 | + # after disabled period, tries to process the container again | |
| 125 | + last_error = Time.now | |
| 126 | + Time.stubs(:now).returns(last_error + FeedHandler.disabled_period + 1.second) | |
| 127 | + handler.process(container) | |
| 128 | + | |
| 129 | + assert container.enabled, 'must reenable container after <disabled_period> (%s)' % container_class | |
| 130 | + end | |
| 115 | 131 | end |
| 116 | 132 | |
| 117 | 133 | should 'not crash even when finish fetch fails' do | ... | ... |