Class: Chutzen::Watcher

Inherits:
Object
  • Object
show all
Defined in:
lib/chutzen/watcher.rb

Overview

Watches the current process to make sure it’s still running as intended.

You can set a watcher to make sure a list of files increases in size at least every ‘read_timeout’ seconds.

watcher = Watcher.new(
  fail_when: {
    'read_timeout' => 2,
    'runtime' => 60
  },
  files: [file]
)
until watcher.done?
  watcher.tick
end

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fail_when: nil, files: nil) ⇒ Watcher

Returns a new instance of Watcher.



25
26
27
28
29
30
31
32
# File 'lib/chutzen/watcher.rb', line 25

def initialize(fail_when: nil, files: nil)
  fail_when&.each do |name, value|
    instance_variable_set("@#{name}", value)
  end
  @started_at = now
  @files = files
  reset
end

Instance Attribute Details

#read_timeoutObject (readonly)

Returns the value of attribute read_timeout.



23
24
25
# File 'lib/chutzen/watcher.rb', line 23

def read_timeout
  @read_timeout
end

#runtimeObject (readonly)

Returns the value of attribute runtime.



23
24
25
# File 'lib/chutzen/watcher.rb', line 23

def runtime
  @runtime
end

Instance Method Details

#done?Boolean

Returns:

  • (Boolean)


43
44
45
46
47
# File 'lib/chutzen/watcher.rb', line 43

def done?
  # Because demuxers and the watcher share an interface we need to the
  # watcher to explain the thread doesn't have to wait for it.
  true
end

#select_timeoutObject



34
35
36
37
38
39
40
41
# File 'lib/chutzen/watcher.rb', line 34

def select_timeout
  return @select_timeout if defined?(@select_timeout)

  minimal_timeout = [read_timeout, runtime].compact.min
  return unless minimal_timeout

  minimal_timeout / 2.0
end

#tickObject

Raises an exception when any of the failure conditions is reached.



50
51
52
53
54
# File 'lib/chutzen/watcher.rb', line 50

def tick
  compute_last_read
  verify_last_read
  verify_runtime
end