Class: JobWorkflow::TaskThrottle

Inherits:
Object
  • Object
show all
Defined in:
lib/job_workflow/task_throttle.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, limit: nil, ttl: 180) ⇒ TaskThrottle

: (key: String, ?limit: Integer?, ?ttl: Integer) -> void



28
29
30
31
32
# File 'lib/job_workflow/task_throttle.rb', line 28

def initialize(key:, limit: nil, ttl: 180)
  @key = key
  @limit = limit
  @ttl = ttl
end

Instance Attribute Details

#keyObject (readonly)

: String



5
6
7
# File 'lib/job_workflow/task_throttle.rb', line 5

def key
  @key
end

#limitObject (readonly)

: Integer?



6
7
8
# File 'lib/job_workflow/task_throttle.rb', line 6

def limit
  @limit
end

#ttlObject (readonly)

: Integer



7
8
9
# File 'lib/job_workflow/task_throttle.rb', line 7

def ttl
  @ttl
end

Class Method Details

.from_primitive_value_with_task(value:, task:) ⇒ Object

: (value: Integer | Hash[Symbol, untyped], task: Task) -> TaskThrottle



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/job_workflow/task_throttle.rb', line 11

def from_primitive_value_with_task(value:, task:)
  case value
  when Integer
    new(key: task.throttle_prefix_key, limit: value)
  when Hash
    new(
      key: value[:key] || task.throttle_prefix_key,
      limit: value[:limit],
      ttl: value[:ttl] || 180
    )
  else
    raise ArgumentError, "throttle must be Integer or Hash"
  end
end

Instance Method Details

#semaphoreObject

: () -> Semaphore?



35
36
37
38
39
40
41
42
43
44
# File 'lib/job_workflow/task_throttle.rb', line 35

def semaphore
  local_limit = limit
  return if local_limit.nil?

  Semaphore.new(
    concurrency_key: key,
    concurrency_duration: ttl.seconds,
    concurrency_limit: local_limit
  )
end