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
# File 'lib/orchestrator_client/job.rb', line 17

def initialize(client, options = {}, type=:deploy)
  @client = client
  @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)


45
46
47
# File 'lib/orchestrator_client/job.rb', line 45

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

#detailsObject



54
55
56
# File 'lib/orchestrator_client/job.rb', line 54

def details
  @details ||= get_details
end

#each_eventObject

A poll the events endpoint yielding each event



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

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 1
    else
      events['items'].each do |event|
        finished = true if DONE_EVENTS.include?(event['type'])
        yield event
      end
    end
  end
end

#get_detailsObject



49
50
51
52
# File 'lib/orchestrator_client/job.rb', line 49

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

#nodesObject



62
63
64
# File 'lib/orchestrator_client/job.rb', line 62

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

#reportObject



58
59
60
# File 'lib/orchestrator_client/job.rb', line 58

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

#startObject



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

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



39
40
41
42
43
# File 'lib/orchestrator_client/job.rb', line 39

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 = 1000) ⇒ Object



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

def wait(timeout=1000)
  counter = 0
  while counter < timeout
    get_details
    if DONE_STATES.include?(details['state'])
      return report
    end
    sleep 1
    counter += 1
  end
end