Class: OrchestratorClient::Job

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

Constant Summary collapse

DONE_STATES =
['stopped', 'finished', 'failed']
DONE_EVENTS =
['job_aborted', 'job_finished']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, options = {}, type = :deploy) ⇒ Job

Returns a new instance of Job.



17
18
19
20
21
22
23
# File 'lib/orchestrator_client/job.rb', line 17

def initialize(client, options = {}, type=:deploy)
  @client = client
  @poll_interval = options.delete(:_poll_interval) || client.config['job-poll-interval']
  @poll_timeout = options.delete(:_poll_timeout) || client.config['job-poll-timeout']
  @options = options
  @type = type
end

Instance Attribute Details

#job_idObject

Returns the value of attribute job_id.



6
7
8
# File 'lib/orchestrator_client/job.rb', line 6

def job_id
  @job_id
end

#job_nameObject

Returns the value of attribute job_name.



6
7
8
# File 'lib/orchestrator_client/job.rb', line 6

def job_name
  @job_name
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/orchestrator_client/job.rb', line 6

def options
  @options
end

Instance Method Details

#assert_started?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/orchestrator_client/job.rb', line 47

def assert_started?
  Raise ArgumentError "Job is not yet started" unless @job_name
end

#detailsObject



56
57
58
# File 'lib/orchestrator_client/job.rb', line 56

def details
  @details ||= get_details
end

#each_eventObject

A poll the events endpoint yielding each event



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/orchestrator_client/job.rb', line 69

def each_event
  finished = false
  start = nil
  while !finished
    events = @client.jobs.events(@job_name)
    start = events['next-events']['event']
    if events['items'].empty?
      sleep @poll_interval
    else
      events['items'].each do |event|
        finished = true if DONE_EVENTS.include?(event['type'])
        yield event
      end
    end
  end
end

#get_detailsObject



51
52
53
54
# File 'lib/orchestrator_client/job.rb', line 51

def get_details
  assert_started?
  @details = @client.jobs.details(@job_name)
end

#nodesObject



64
65
66
# File 'lib/orchestrator_client/job.rb', line 64

def nodes
  @client.jobs.nodes(@job_name)
end

#reportObject



60
61
62
# File 'lib/orchestrator_client/job.rb', line 60

def report
  @client.jobs.report(@job_name)
end

#startObject



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

def start
  case @type
  when :deploy
    result = @client.command.deploy(options)
  when :task
    result = @client.command.task(options)
  when :plan_task
    result = @client.command.plan_task(options)
  end

  @job_name = result['job']['name']
  @job_id = result['job']['id']
  @next_event=nil
  @job_name
end

#stopObject



41
42
43
44
45
# File 'lib/orchestrator_client/job.rb', line 41

def stop
  unless job_name
    Raise ArgumentError "Job name not known to stop"
  end
end

#validate_scopeObject



8
9
10
11
12
13
14
15
# File 'lib/orchestrator_client/job.rb', line 8

def validate_scope
  scope = @options['scope']
  if scope.empty
    Raise ArgumentError 'Scope cannot be empty'
  elif  scope['whole_environment']
    puts 'Deprecation Warning: Whole environment behavior may not be stable'
  end
end

#wait(timeout = @poll_timeout) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/orchestrator_client/job.rb', line 86

def wait(timeout=@poll_timeout)
  counter = 0
  while counter < timeout
    get_details
    if DONE_STATES.include?(details['state'])
      return report
    end
    sleep @poll_interval
    counter += @poll_interval
  end
  raise OrchestratorClient::JobWaitTimeout
end