Module: Polyrun::Queue::Duration

Defined in:
lib/polyrun/queue/duration.rb

Overview

Parse duration strings like 10m, 1h, 600s into seconds.

Class Method Summary collapse

Class Method Details

.parse_seconds(text) ⇒ Object

Raises:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/polyrun/queue/duration.rb', line 11

def parse_seconds(text)
  s = text.to_s.strip
  return Float(s) if s.match?(/\A\d+(\.\d+)?\z/)

  m = s.match(/\A(\d+(?:\.\d+)?)(s|m|h|d)\z/i)
  raise Polyrun::Error, "invalid duration: #{text.inspect}" unless m

  val = Float(m[1])
  case m[2].downcase
  when "s" then val
  when "m" then val * 60
  when "h" then val * 3600
  when "d" then val * 86_400
  else
    raise Polyrun::Error, "invalid duration: #{text.inspect}"
  end
end