Class: Sidekiq::Batch::Jobs::FailurePolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq/batch/jobs/failure_policy.rb

Overview

What it takes for a batch to count as failed. All four policies are the same rule with a different threshold: the batch fails when more jobs failed than it tolerates.

:any_failure        0                            (the default)
:all_failed         total_jobs - 1
{ tolerate: 10 }    10
{ tolerate: "5%" }  floor(total_jobs * 5 / 100)

Only the name and the number live here; CompletionQuery resolves the threshold in SQL, because total_jobs is unknown until the batch starts and the comparison has to stay inside the one atomic transition.

Plain Ruby on purpose: Configuration holds one of these and is read from an initializer, where ActiveSupport may not be loaded. So no blank?, no present?, nothing from ActiveSupport.

Constant Summary collapse

ANY_FAILURE =
"any_failure"
ALL_FAILED =
"all_failed"
TOLERATE_JOBS =
"tolerate_jobs"
TOLERATE_PERCENT =
"tolerate_percent"
NAMES =
[ANY_FAILURE, ALL_FAILED, TOLERATE_JOBS, TOLERATE_PERCENT].freeze
DECLARABLE =

Names a user may pass directly. The two tolerate_* names are how the column stores a tolerance; they mean nothing without a number beside them, so they are not accepted as input.

[ANY_FAILURE, ALL_FAILED].freeze
PERCENTAGE =
/\A\s*(\d+)\s*%\s*\z/
MAX_PERCENT =
100

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, tolerance:) ⇒ FailurePolicy

Returns a new instance of FailurePolicy.



103
104
105
106
107
108
# File 'lib/sidekiq/batch/jobs/failure_policy.rb', line 103

def initialize(name:, tolerance:)
  @name      = name
  @tolerance = tolerance

  freeze
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



39
40
41
# File 'lib/sidekiq/batch/jobs/failure_policy.rb', line 39

def name
  @name
end

#toleranceObject (readonly)

Returns the value of attribute tolerance.



39
40
41
# File 'lib/sidekiq/batch/jobs/failure_policy.rb', line 39

def tolerance
  @tolerance
end

Class Method Details

.normalize(value) ⇒ FailurePolicy

Coerces whatever a caller supplied into a policy.

Parameters:

Returns:

Raises:

  • (ArgumentError)

    on anything it cannot make sense of



47
48
49
50
51
52
53
54
# File 'lib/sidekiq/batch/jobs/failure_policy.rb', line 47

def normalize(value)
  case value
  when FailurePolicy   then value
  when Symbol, String  then named(value.to_s)
  when Hash            then tolerant(value)
  else reject(value)
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



110
111
112
# File 'lib/sidekiq/batch/jobs/failure_policy.rb', line 110

def ==(other)
  other.is_a?(FailurePolicy) && other.name == name && other.tolerance == tolerance
end

#hashObject



115
116
117
# File 'lib/sidekiq/batch/jobs/failure_policy.rb', line 115

def hash
  [name, tolerance].hash
end

#inspectObject



123
124
125
# File 'lib/sidekiq/batch/jobs/failure_policy.rb', line 123

def inspect
  tolerance.nil? ? "#<FailurePolicy #{name}>" : "#<FailurePolicy #{name} #{tolerance}>"
end

#to_hObject



119
120
121
# File 'lib/sidekiq/batch/jobs/failure_policy.rb', line 119

def to_h
  { failure_policy: name, failure_tolerance: tolerance }
end