Class: Cuted::Command

Inherits:
Object
  • Object
show all
Includes:
DRbUndumped
Defined in:
lib/cuted/command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd, opts = {}) ⇒ Command

Returns a new instance of Command.



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

def initialize(cmd, opts = {})
  @cmd = cmd.to_s
  # If the options are set, use them; otherwise defaults.
  @dir = opts[:dir] || Dir.pwd
  # Since we can force opts[:log] to be nil, and we don't want the default.
  @log = opts.has_key?(:log) ? opts[:log] : 'cute.log'
  @priority = opts[:priority] || 0
  @weight = opts[:weight] || 1
end

Instance Attribute Details

#cmdObject

Returns the value of attribute cmd.



17
18
19
# File 'lib/cuted/command.rb', line 17

def cmd
  @cmd
end

#dirObject

Returns the value of attribute dir.



17
18
19
# File 'lib/cuted/command.rb', line 17

def dir
  @dir
end

#logObject

Returns the value of attribute log.



17
18
19
# File 'lib/cuted/command.rb', line 17

def log
  @log
end

#pidObject (readonly)

Returns the value of attribute pid.



18
19
20
# File 'lib/cuted/command.rb', line 18

def pid
  @pid
end

#priorityObject

Returns the value of attribute priority.



17
18
19
# File 'lib/cuted/command.rb', line 17

def priority
  @priority
end

#prowl_keyObject

Returns the value of attribute prowl_key.



17
18
19
# File 'lib/cuted/command.rb', line 17

def prowl_key
  @prowl_key
end

#weightObject

Returns the value of attribute weight.



17
18
19
# File 'lib/cuted/command.rb', line 17

def weight
  @weight
end

Instance Method Details

#runObject

Runs in the current thread.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cuted/command.rb', line 31

def run
  # Make sure the desired working directory exists.
  FileUtils.makedirs(@dir) if not File.directory?(@dir)
  # Actually run the command.
  if @log then
    @pid = spawn(@cmd, :chdir => @dir, [:out, :err] => [@log, 'a'])
  else
    @pid = spawn(@cmd, :chdir => @dir, [:out, :err] => '/dev/null')
  end

  # In the forked process, wait for the command to be done.
  Process.waitall

  # Report that this command finished.
  begin
    require 'prowler'
    Prowler.verify_certificate = false
    Prowler.application = 'cuted'
    Prowler.notify(@cmd, "Finished in #{@dir}", @prowl_key) if @prowl_key
  rescue LoadError
  end

  # Return the CuteCommand.
  self
end

#to_sObject

Print out dir> command string.



58
59
60
# File 'lib/cuted/command.rb', line 58

def to_s
  "#{@dir}> #{@cmd}"
end