Class: Chutzen::Job

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/chutzen/job.rb

Overview

Holds a description for a job and executes it.

Instance Method Summary collapse

Constructor Details

#initialize(description) ⇒ Job

Returns a new instance of Job.



10
11
12
13
14
15
16
17
18
19
# File 'lib/chutzen/job.rb', line 10

def initialize(description)
  @notify = nil
  @result = {}
  description.each do |name, value|
    instance_variable_set("@#{name}", value)
  end
  @uuid = SecureRandom.uuid
  @work_path = File.join(ENV['HOME'], 'chutzen', @uuid)
  @dictionary = Dictionary.new(properties)
end

Instance Method Details

#eachObject

Yields Command instances for each command in the description.



32
33
34
35
36
37
38
39
40
# File 'lib/chutzen/job.rb', line 32

def each
  @commands.each do |description|
    yield Command.new(
      description,
      dictionary: @dictionary,
      work_path: @work_path
    )
  end
end

#performObject

Perform all commands in the job.



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/chutzen/job.rb', line 43

def perform
  FileUtils.mkdir_p(@work_path)
  map do |command|
    perform_notification(command.perform)
    command
  end
rescue Chutzen::StandardError => e
  warn("Job failed: #{e}")
  perform_exception_notification(e)
ensure
  FileUtils.rm_rf(@work_path)
end

#propertiesObject



21
22
23
24
25
26
27
28
29
# File 'lib/chutzen/job.rb', line 21

def properties
  {
    'uuid' => @uuid,
    'work_path' => @work_path,
    'uname' => Etc.uname.transform_keys(&:to_s),
    'nprocs' => Etc.nprocessors,
    'chutzen_version' => Chutzen::VERSION
  }
end