Class: Penelope::Core::Job

Inherits:
Object
  • Object
show all
Includes:
DataMapper::Resource
Defined in:
lib/penelope/core/job.rb

Direct Known Subclasses

GitJob

Instance Method Summary collapse

Instance Method Details

#cleanupObject



16
17
18
19
# File 'lib/penelope/core/job.rb', line 16

def cleanup
  # TODO archive working directory
  FileUtils.rm_rf working_directory
end

#performObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/penelope/core/job.rb', line 21

def perform
  @start_time = Time.now
  begin
    prepare
    retrieve
    run
  rescue Exception => e
    @exceptions ||= []
    @exceptions << e.message
  end
  @end_time = Time.now
  report
  cleanup
end

#prepareObject



36
37
38
# File 'lib/penelope/core/job.rb', line 36

def prepare
  FileUtils.mkdir_p working_directory
end

#reportObject



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/penelope/core/job.rb', line 62

def report
  jr = JobReport.new(
    pid: @pid,
    exit_status: @exit_status,
    start_time: @start_time,
    end_time: @end_time,
    result: @result,
    job_id: id
  )
  jr.exceptions = @exceptions.to_yaml if @exceptions
  jr.save
end

#retrieveObject



40
41
42
# File 'lib/penelope/core/job.rb', line 40

def retrieve
  raise 'Not Implemented'
end

#runObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/penelope/core/job.rb', line 44

def run
  @exceptions = nil
  # TODO wrap in Docker container
  begin
    FileUtils.cd(working_directory) do
      @result = IO.popen(entry_point).read
    end
    @exit_status = $?.exitstatus
    @pid = $?.pid
  rescue Exception => e
    @exit_status ||= -1
    @pid ||= -1
    @exceptions ||= []
    @exceptions << e.message
  end
  @ran = true
end

#working_directoryObject



75
76
77
# File 'lib/penelope/core/job.rb', line 75

def working_directory
  File.join(CONFIG[:data][:directory], self.class.name, id.to_s)
end