Merge pull request #1645 from dsander/retry-twitter-action-agent

Allow TwitterUserAgent to retry failed actions
This commit is contained in:
Dominik Sander 2016-09-08 20:44:20 +02:00 committed by GitHub
commit f42ad96ed6
3 changed files with 68 additions and 13 deletions

View file

@ -16,6 +16,7 @@ module Agents
Set `expected_receive_period_in_days` to the maximum amount of time that you'd expect to pass between Events being received by this Agent.
Set `retweet` to either true or false.
Set `favorite` to either true or false.
Set `emit_error_events` to true to emit an Event when the action failed, otherwise the action will be retried.
MD
def validate_options
@ -25,6 +26,9 @@ module Agents
unless retweet? || favorite?
errors.add(:base, "at least one action must be true")
end
if emit_error_events?.nil?
errors.add(:base, "emit_error_events must be set to 'true' or 'false'")
end
end
def working?
@ -36,6 +40,7 @@ module Agents
'expected_receive_period_in_days' => '2',
'favorite' => 'false',
'retweet' => 'true',
'emit_error_events' => 'false'
}
end
@ -47,6 +52,10 @@ module Agents
boolify(options['favorite'])
end
def emit_error_events?
boolify(options['emit_error_events'])
end
def receive(incoming_events)
tweets = tweets_from_events(incoming_events)
@ -54,6 +63,7 @@ module Agents
twitter.favorite(tweets) if favorite?
twitter.retweet(tweets) if retweet?
rescue Twitter::Error => e
raise e unless emit_error_events?
create_event :payload => {
'success' => false,
'error' => e.message,
@ -71,4 +81,3 @@ module Agents
end
end
end

View file

@ -0,0 +1,15 @@
class SetEmitErrorEventForTwitterActionAgents < ActiveRecord::Migration
def up
Agents::TwitterActionAgent.find_each do |agent|
agent.options['emit_error_events'] = 'true'
agent.save!(validate: false)
end
end
def down
Agents::TwitterActionAgent.find_each do |agent|
agent.options.delete('emit_error_events')
agent.save!(validate: false)
end
end
end

View file

@ -24,11 +24,11 @@ describe Agents::TwitterActionAgent do
context 'when set up to retweet' do
before do
@agent = build_agent({
'expected_receive_period_in_days' => '2',
@agent = build_agent(
'favorite' => 'false',
'retweet' => 'true',
})
'emit_error_events' => 'true'
)
@agent.save!
end
@ -68,9 +68,9 @@ describe Agents::TwitterActionAgent do
context 'when set up to favorite' do
before do
@agent = build_agent(
'expected_receive_period_in_days' => '2',
'favorite' => 'true',
'retweet' => 'false',
'emit_error_events' => 'true'
)
@agent.save!
end
@ -107,13 +107,48 @@ describe Agents::TwitterActionAgent do
end
end
end
context 'with emit_error_events set to false' do
it 'does re-raises the exception on failure' do
agent = build_agent
stub(agent.twitter).retweet(anything) {
raise Twitter::Error.new('uh oh')
}
expect { agent.receive([@event1]) }.to raise_error(StandardError, /uh oh/)
end
end
end
describe "#validate_options" do
it 'the default options are valid' do
agent = build_agent(described_class.new.default_options)
expect(agent).to be_valid
end
context 'emit_error_events' do
it 'can be set to true' do
agent = build_agent(described_class.new.default_options.merge('emit_error_events' => 'true'))
expect(agent).to be_valid
end
it 'must be a boolean' do
agent = build_agent(described_class.new.default_options.merge('emit_error_events' => 'notbolean'))
expect(agent).not_to be_valid
end
end
it 'expected_receive_period_in_days must be set' do
agent = build_agent(described_class.new.default_options.merge('expected_receive_period_in_days' => ''))
expect(agent).not_to be_valid
end
context 'when set up to neither favorite or retweet' do
it 'is invalid' do
agent = build_agent(
'expected_receive_period_in_days' => '2',
'favorite' => 'false',
'retweet' => 'false',
)
@ -129,11 +164,7 @@ describe Agents::TwitterActionAgent do
end
it 'checks if events have been received within the expected time period' do
agent = build_agent(
'expected_receive_period_in_days' => '2',
'favorite' => 'false',
'retweet' => 'true',
)
agent = build_agent
agent.save!
expect(agent).not_to be_working # No events received
@ -147,10 +178,10 @@ describe Agents::TwitterActionAgent do
end
end
def build_agent(options)
def build_agent(options = {})
described_class.new do |agent|
agent.name = 'twitter stuff'
agent.options = options
agent.options = agent.default_options.merge(options)
agent.service = services(:generic)
agent.user = users(:bob)
end