Class: DispatchPolicy::Policy
- Inherits:
-
Object
- Object
- DispatchPolicy::Policy
- Defined in:
- lib/dispatch_policy/policy.rb
Instance Attribute Summary collapse
-
#dedupe_key_builder ⇒ Object
readonly
Returns the value of attribute dedupe_key_builder.
-
#gates ⇒ Object
readonly
Returns the value of attribute gates.
-
#job_class ⇒ Object
readonly
Returns the value of attribute job_class.
-
#snapshots ⇒ Object
readonly
Returns the value of attribute snapshots.
Instance Method Summary collapse
- #build_dedupe_key(arguments) ⇒ Object
- #build_round_robin_key(arguments) ⇒ Object
- #build_snapshot(arguments) ⇒ Object
- #context(builder) ⇒ Object
- #context_builder ⇒ Object
- #dedupe_key(builder) ⇒ Object
- #gate(type, **opts) ⇒ Object
-
#initialize(job_class, &block) ⇒ Policy
constructor
A new instance of Policy.
- #name(value = nil) ⇒ Object
- #round_robin? ⇒ Boolean
- #round_robin_by(builder, weight: :equal, window: 60) ⇒ Object
- #round_robin_weight ⇒ Object
- #round_robin_window ⇒ Object
- #snapshot(key, builder) ⇒ Object
Constructor Details
#initialize(job_class, &block) ⇒ Policy
Returns a new instance of Policy.
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/dispatch_policy/policy.rb', line 7 def initialize(job_class, &block) @job_class = job_class @name = job_class.name.underscore.tr("/", "-") @context_builder = ->(_args) { {} } @gates = [] @snapshots = {} @dedupe_key_builder = nil @round_robin_builder = nil @round_robin_weight = :equal @round_robin_window = 60 instance_eval(&block) if block DispatchPolicy.registry[@name] = job_class end |
Instance Attribute Details
#dedupe_key_builder ⇒ Object (readonly)
Returns the value of attribute dedupe_key_builder.
5 6 7 |
# File 'lib/dispatch_policy/policy.rb', line 5 def dedupe_key_builder @dedupe_key_builder end |
#gates ⇒ Object (readonly)
Returns the value of attribute gates.
5 6 7 |
# File 'lib/dispatch_policy/policy.rb', line 5 def gates @gates end |
#job_class ⇒ Object (readonly)
Returns the value of attribute job_class.
5 6 7 |
# File 'lib/dispatch_policy/policy.rb', line 5 def job_class @job_class end |
#snapshots ⇒ Object (readonly)
Returns the value of attribute snapshots.
5 6 7 |
# File 'lib/dispatch_policy/policy.rb', line 5 def snapshots @snapshots end |
Instance Method Details
#build_dedupe_key(arguments) ⇒ Object
44 45 46 47 48 |
# File 'lib/dispatch_policy/policy.rb', line 44 def build_dedupe_key(arguments) return nil unless @dedupe_key_builder key = @dedupe_key_builder.call(arguments) key&.to_s end |
#build_round_robin_key(arguments) ⇒ Object
69 70 71 72 73 |
# File 'lib/dispatch_policy/policy.rb', line 69 def build_round_robin_key(arguments) return nil unless @round_robin_builder key = @round_robin_builder.call(arguments) key.nil? || key.to_s.empty? ? nil : key.to_s end |
#build_snapshot(arguments) ⇒ Object
82 83 84 |
# File 'lib/dispatch_policy/policy.rb', line 82 def build_snapshot(arguments) @snapshots.transform_values { |builder| builder.call(arguments) } end |
#context(builder) ⇒ Object
28 29 30 |
# File 'lib/dispatch_policy/policy.rb', line 28 def context(builder) @context_builder = builder end |
#context_builder ⇒ Object
32 33 34 |
# File 'lib/dispatch_policy/policy.rb', line 32 def context_builder @context_builder end |
#dedupe_key(builder) ⇒ Object
40 41 42 |
# File 'lib/dispatch_policy/policy.rb', line 40 def dedupe_key(builder) @dedupe_key_builder = builder end |
#gate(type, **opts) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/dispatch_policy/policy.rb', line 75 def gate(type, **opts) gate_class = DispatchPolicy::Gate.registry.fetch(type.to_sym) do raise ArgumentError, "Unknown gate: #{type}. Known: #{DispatchPolicy::Gate.registry.keys}" end @gates << gate_class.new(policy: self, name: type.to_sym, **opts) end |
#name(value = nil) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/dispatch_policy/policy.rb', line 21 def name(value = nil) return @name if value.nil? DispatchPolicy.registry.delete(@name) @name = value.to_s DispatchPolicy.registry[@name] = @job_class end |
#round_robin? ⇒ Boolean
57 58 59 |
# File 'lib/dispatch_policy/policy.rb', line 57 def round_robin? !@round_robin_builder.nil? end |
#round_robin_by(builder, weight: :equal, window: 60) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/dispatch_policy/policy.rb', line 50 def round_robin_by(builder, weight: :equal, window: 60) raise ArgumentError, "weight must be :equal or :time" unless %i[equal time].include?(weight) @round_robin_builder = builder @round_robin_weight = weight @round_robin_window = window end |
#round_robin_weight ⇒ Object
61 62 63 |
# File 'lib/dispatch_policy/policy.rb', line 61 def round_robin_weight @round_robin_weight end |
#round_robin_window ⇒ Object
65 66 67 |
# File 'lib/dispatch_policy/policy.rb', line 65 def round_robin_window @round_robin_window end |
#snapshot(key, builder) ⇒ Object
36 37 38 |
# File 'lib/dispatch_policy/policy.rb', line 36 def snapshot(key, builder) @snapshots[key.to_sym] = builder end |