Class: Schedule::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/schedule/runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(task, logger = nil, notifier = nil) ⇒ Runner

Returns a new instance of Runner.



5
6
7
8
9
# File 'lib/schedule/runner.rb', line 5

def initialize(task, logger = nil, notifier = nil)
  @task = task
  @logger = logger || Logger.new(STDOUT, @task.name)
  @notifier = notifier
end

Instance Method Details

#runObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/schedule/runner.rb', line 11

def run
  @logger.log "=== Started (#{@task.command}) ==="

  status = nil
  
  duration = with_timing do
    status = execute(@task.command) do |output|
      buffer = ""
      begin
        while (buffer << output.readpartial(1024))
          if lines = buffer.slice!(/^.*[\n\r]/m)
            @logger.log(lines)
          end
        end
      rescue EOFError
        @logger.log(buffer) unless buffer == ""
      ensure
        buffer = nil
      end
    end
  end

  exit_status = if status && status.exitstatus == 0
    0
  elsif status && status.exitstatus
    status.exitstatus
  else
    1
  end

  if exit_status == 0
    @logger.log "=== Completed successfully (took #{duration} seconds) ==="
  else
    @logger.log "=== Failed (took #{duration} seconds) ==="
  end
  
  if @notifier
    @notifier.notify(@task, @logger.buffer.string)
  end
  
  return exit_status
end