Class: Conveyor::Worker

Inherits:
Object
  • Object
show all
Includes:
Output, Conveyor::Workers::Syntax
Defined in:
lib/conveyor/worker.rb

Constant Summary

Constants included from Output

Output::MSGLVLS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Conveyor::Workers::Syntax

#any, #chdir, #copy, #delete, #extension, #file, #like, #match, #method_missing, #mkdir, #move, #run, #scp, #sync, #watch

Methods included from Output

#announce, #debug, #info, #loglvl, #notify, #output, #say, #send_notifications, #should_log?, #warning

Constructor Details

#initialize(file, worker_def, log = MSGLVLS[:debug]) ⇒ Worker

Returns a new instance of Worker.



15
16
17
18
19
20
21
# File 'lib/conveyor/worker.rb', line 15

def initialize(file, worker_def, log = MSGLVLS[:debug])
  @filename = file
  @loglvl = log
  @worker_def = worker_def
  @notify = []
  # @glob = escape_glob(glob)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Conveyor::Workers::Syntax

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



11
12
13
# File 'lib/conveyor/worker.rb', line 11

def filename
  @filename
end

#statusObject (readonly)

Returns the value of attribute status.



12
13
14
# File 'lib/conveyor/worker.rb', line 12

def status
  @status
end

#worker_defObject (readonly)

Returns the value of attribute worker_def.



13
14
15
# File 'lib/conveyor/worker.rb', line 13

def worker_def
  @worker_def
end

Instance Method Details

#error(*msg) ⇒ Object

Catch any calls to error and set the status fail flags



37
38
39
40
41
42
43
44
45
# File 'lib/conveyor/worker.rb', line 37

def error(*msg)
  opts = msg.extract_options!
  unless msg.flatten.empty?
    @status.fail!
    msg.unshift("Error encountered in #{worker_def}")
    super(*msg, opts)
  end
  @status.success?
end

#logfileObject

Default log file to be based on worker def name



31
32
33
34
# File 'lib/conveyor/worker.rb', line 31

def logfile
  dir = File.dirname(worker_def)
  File.expand_path(File.basename(worker_def, '.worker') + '.log', dir)
end

#name(value = nil) ⇒ Object

Return name to be used for logging purposes



24
25
26
27
28
# File 'lib/conveyor/worker.rb', line 24

def name(value=nil)
  @name = value unless value.nil?
  @name ||= File.basename(worker_def)
  @name
end

#startObject

Start the worker



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/conveyor/worker.rb', line 48

def start
  @status = Conveyor::Status.new(@filename)
  info "Starting #{@filename}", :color => :green
  @start = Time.now
  begin
    instance_eval(File.read(@worker_def), worker_def)
  ensure
    @elapsed = "%0.2f"%(Time.now - @start)

    #Check status and send any errors we collected
    if @status.success?
      info "Completed #{@filename}, #{@elapsed}s elapsed", :color => :green
    else
      error "Error(s) encountered in #{@filename}", :color => :red
    end
    send_notifications
  end
end