Class: Dynflow::Daemon

Inherits:
Object
  • Object
show all
Includes:
Algebrick::TypeCheck
Defined in:
lib/dynflow/daemon.rb

Instance Method Summary collapse

Constructor Details

#initialize(listener, world, lock_file = nil) ⇒ Daemon

Returns a new instance of Daemon.



5
6
7
8
9
# File 'lib/dynflow/daemon.rb', line 5

def initialize(listener, world, lock_file = nil)
  @listener  = Type! listener, Listeners::Abstract
  @world     = Type! world, World
  @lock_file = Type! lock_file, String, NilClass
end

Instance Method Details

#runObject



11
12
13
14
15
16
17
18
# File 'lib/dynflow/daemon.rb', line 11

def run
  with_lock_file do
    terminated = Future.new
    trap('SIGINT') { @world.terminate terminated }
    terminated.wait
    @listener.terminate.wait
  end
end

#with_lock_file(&block) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/dynflow/daemon.rb', line 20

def with_lock_file(&block)
  if @lock_file
    raise "Lockfile #{@lock_file} is already present." if File.exist?(@lock_file)
    File.write(@lock_file, "Locked at #{Time.now} by process #{$$}\n")
  end
  block.call
ensure
  File.delete(@lock_file) if @lock_file
end