Class: Plotlyrb::PlotImage::AsyncJob

Inherits:
Struct
  • Object
show all
Defined in:
lib/plotlyrb/plot_image.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#spec_pathObject

Returns the value of attribute spec_path

Returns:

  • (Object)

    the current value of spec_path



33
34
35
# File 'lib/plotlyrb/plot_image.rb', line 33

def spec_path
  @spec_path
end

#start_timeObject

Returns the value of attribute start_time

Returns:

  • (Object)

    the current value of start_time



33
34
35
# File 'lib/plotlyrb/plot_image.rb', line 33

def start_time
  @start_time
end

#threadObject

Returns the value of attribute thread

Returns:

  • (Object)

    the current value of thread



33
34
35
# File 'lib/plotlyrb/plot_image.rb', line 33

def thread
  @thread
end

#timeoutObject

Returns the value of attribute timeout

Returns:

  • (Object)

    the current value of timeout



33
34
35
# File 'lib/plotlyrb/plot_image.rb', line 33

def timeout
  @timeout
end

Class Method Details

.from_spec_path(pi, spec_path, timeout) ⇒ Object



34
35
36
37
# File 'lib/plotlyrb/plot_image.rb', line 34

def self.from_spec_path(pi, spec_path, timeout)
  thread = Thread.new { pi.plot_image(spec_path.spec, spec_path.path) }
  new(thread, spec_path, Time.new, timeout)
end

Instance Method Details

#fail(msg) ⇒ Object



43
44
45
# File 'lib/plotlyrb/plot_image.rb', line 43

def fail(msg)
  AsyncJobResult.new(false, msg, spec_path)
end

#joinObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/plotlyrb/plot_image.rb', line 39

def join
  now = Time.new
  join_wait = (self.timeout - (now - self.start_time)).to_i + 1

  def fail(msg)
    AsyncJobResult.new(false, msg, spec_path)
  end

  # Joining should bubble any exceptions, so catch them and report the failure as an error
  begin
    if thread.join(join_wait).nil?
      thread.exit
      return fail("thread didn't finish within timeout (#{timeout}s)")
    end

    response = thread.value
    unless response.success
      return fail("Plotly returned error response: #{response}")
    end

    unless File.exist?(spec_path.path)
      return fail("Output file (#{spec_path.path}) not found")
    end
  rescue => e
    return fail(e.message)
  end

  AsyncJobResult.new(true, '', spec_path)
end