Class: MiOS::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/mios/job.rb

Constant Summary collapse

Status =
{
  -1 => "Nonexistent",
  0 => "Waiting",
  1 => "In progress",
  2 => "Error",
  3 => "Aborted",
  4 => "Completed",
  5 => "Waiting for callback",
  6 => "Requeue",
  7 => "In progress with pending data"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, id, async = false, &block) ⇒ Job

Returns a new instance of Job.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mios/job.rb', line 26

def initialize(obj, id, async=false, &block)
  @obj, @id = obj, id
  reload_status!

  if block_given?
    if async
      Thread.new do
        when_complete(&block)
      end
    else
      when_complete(&block)
    end
  end
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



24
25
26
# File 'lib/mios/job.rb', line 24

def id
  @id
end

Instance Method Details

#aborted?Boolean

Returns:

  • (Boolean)


65
# File 'lib/mios/job.rb', line 65

def aborted?; status == 3; end

#completed?Boolean

Returns:

  • (Boolean)


66
# File 'lib/mios/job.rb', line 66

def completed?; status == 4; end

#error?Boolean

Returns:

  • (Boolean)


64
# File 'lib/mios/job.rb', line 64

def error?; status == 2; end

#exists?Boolean

Returns:

  • (Boolean)


61
# File 'lib/mios/job.rb', line 61

def exists?; status != -1; end

#in_progress?Boolean

Returns:

  • (Boolean)


63
# File 'lib/mios/job.rb', line 63

def in_progress?; status == 1; end

#in_progress_with_pending_data?Boolean

Returns:

  • (Boolean)


69
# File 'lib/mios/job.rb', line 69

def in_progress_with_pending_data?; status == 7; end

#reload_status!Object



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

def reload_status!
  @status = MultiJson.load(@obj.client.get("#{@obj.base_uri}/data_request", {:id => 'jobstatus', :job => @id, :plugin => 'zwave', :output_format => :json}).content)['status']
end

#requeue?Boolean

Returns:

  • (Boolean)


68
# File 'lib/mios/job.rb', line 68

def requeue?; status == 6; end

#statusObject



71
72
73
# File 'lib/mios/job.rb', line 71

def status
  @status || reload_status!
end

#waiting?Boolean

Returns:

  • (Boolean)


62
# File 'lib/mios/job.rb', line 62

def waiting?; status == 0; end

#waiting_for_callback?Boolean

Returns:

  • (Boolean)


67
# File 'lib/mios/job.rb', line 67

def waiting_for_callback?; status == 5; end

#when_complete(&block) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/mios/job.rb', line 41

def when_complete(&block)
  raise Error::JobTimeout if !exists?
  Timeout::timeout(20) do
    sleep_interval = 0.25
    
    # If the job is still processing, wait a bit and try again
    while waiting? || in_progress? || waiting_for_callback? || in_progress_with_pending_data? do
      sleep(sleep_interval += 0.25)
      reload_status!
    end
    raise JobError if error?
    raise JobAborted if aborted?
    raise JobRequeue if requeue?
  end
  yield @obj.reload
rescue Timeout::Error
  $stderr.puts "Timed out waiting for job status to become complete"
  raise Error::JobTimeout
end