Class: Pwrake::Writer

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/pwrake/worker/writer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWriter



8
9
10
11
12
13
14
15
# File 'lib/pwrake/worker/writer.rb', line 8

def initialize
  @out = $stderr
  @mutex = Mutex.new
  @mutex_hb = Mutex.new
  @cond_hb = true
  @heartbeat = nil
  @thread = Thread.new{ heartbeat_loop }
end

Instance Attribute Details

#outObject

Returns the value of attribute out.



17
18
19
# File 'lib/pwrake/worker/writer.rb', line 17

def out
  @out
end

Instance Method Details

#_puts(s) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/pwrake/worker/writer.rb', line 48

def _puts(s)
  begin
    @mutex.synchronize do
      @out.print s+"\n"
    end
    @out.flush
  rescue Errno::EPIPE => e
    @log.info "<#{e.inspect}" if @log
  end
  @log.info "<#{s}" if @log
end

#add_logger(log) ⇒ Object



36
37
38
# File 'lib/pwrake/worker/writer.rb', line 36

def add_logger(log)
  @log = log
end

#dputs(s) ⇒ Object



67
68
69
# File 'lib/pwrake/worker/writer.rb', line 67

def dputs(s)
  puts(s) if $DEBUG
end

#flushObject



60
61
62
63
64
65
# File 'lib/pwrake/worker/writer.rb', line 60

def flush
  begin
    @out.flush
  rescue
  end
end

#heartbeat=(t) ⇒ Object



19
20
21
22
# File 'lib/pwrake/worker/writer.rb', line 19

def heartbeat=(t)
  @heartbeat = t.to_i
  @thread.run
end

#heartbeat_loopObject



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pwrake/worker/writer.rb', line 24

def heartbeat_loop
  loop do
    @heartbeat ? sleep(@heartbeat) : sleep
    @mutex_hb.synchronize do
      if @cond_hb
        _puts "heartbeat"
      end
      @cond_hb = true
    end
  end
end

#puts(s) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/pwrake/worker/writer.rb', line 40

def puts(s)
  @mutex_hb.synchronize do
    @cond_hb = false
    @thread.run
  end
  _puts(s)
end