Module: TwitterRetry::Retryable

Included in:
TwitterRetry
Defined in:
lib/twitter_retry/retryable.rb

Instance Method Summary collapse

Instance Method Details

#ignorable?(error) ⇒ Boolean

whether ignorable error

Parameters:

  • error (Exception)

Returns:

  • (Boolean)


40
41
42
43
44
# File 'lib/twitter_retry/retryable.rb', line 40

def ignorable?(error)
  TwitterRetry.config.ignorable_errors.any? do |error_class, message|
    error.is_a?(error_class) && error.message.include?(message)
  end
end

#retryable?(error) ⇒ Boolean

whether retryable error

Parameters:

  • error (Exception)

Returns:

  • (Boolean)


32
33
34
35
36
# File 'lib/twitter_retry/retryable.rb', line 32

def retryable?(error)
  TwitterRetry.config.retryable_errors.any? do |error_class, message|
    error.is_a?(error_class) && error.message.include?(message)
  end
end

#suspended?(error) ⇒ Boolean

whether suspended user error

Returns:

  • (Boolean)


47
48
49
50
# File 'lib/twitter_retry/retryable.rb', line 47

def suspended?(error)
  error.is_a?(Twitter::Error::Forbidden) &&
    error.message.include?("Your account is suspended and is not permitted to access this feature.")
end

#with_handingtrue, false

retry when error occurred matched with #RETRYABLE_ERRORS

Returns:

  • (true)

    successful

  • (false)

    error is ignored (ex. Twitter::Error::Forbidden)

Raises:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/twitter_retry/retryable.rb', line 9

def with_handing
  retry_count = 0

  begin
    yield

  rescue => error
    return false               if ignorable?(error)
    raise SuspendedError       if suspended?(error)
    raise CannotRetryableError unless retryable?(error)
    raise RetryOverError       unless retry_count < TwitterRetry.config.max_retry_count

    retry_count += 1
    sleep(TwitterRetry.config.sleep_second)
    retry
  end

  # successful
  true
end