Module: Morpheus::Cli::JobsHelper

Included in:
JobsCommand, Tasks, Workflows
Defined in:
lib/morpheus/cli/mixins/jobs_helper.rb

Overview

Provides refreshing job execution records by id

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



6
7
8
9
# File 'lib/morpheus/cli/mixins/jobs_helper.rb', line 6

def self.included(klass)
  klass.send :include, Morpheus::Cli::PrintHelper
  # klass.send :include, Morpheus::Cli::ProcessesHelper
end

Instance Method Details

#api_clientObject



11
12
13
14
# File 'lib/morpheus/cli/mixins/jobs_helper.rb', line 11

def api_client
  raise "#{self.class} has not defined @api_client" if @api_client.nil?
  @api_client
end

#format_job_status(status_string, return_color = cyan) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/morpheus/cli/mixins/jobs_helper.rb', line 126

def format_job_status(status_string, return_color=cyan)
  out = ""
  if status_string
    if ['complete','success', 'successful', 'ok'].include?(status_string)
      out << "#{green}#{status_string.upcase}"
    elsif ['error', 'offline', 'failed', 'failure'].include?(status_string)
      out << "#{red}#{status_string.upcase}"
    elsif ['running'].include?(status_string)
      out << "#{cyan}#{status_string.upcase}"
    else
      out << "#{yellow}#{status_string.upcase}"
    end
  end
  out + return_color
end

#get_available_contexts_for_task(task) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/morpheus/cli/mixins/jobs_helper.rb', line 174

def get_available_contexts_for_task(task)
  #If task has target of resource, then CAN NOT run it local
  targets = []
  has_resource = task['executeTarget'] == 'resource'
  if !has_resource
    targets << {'name' => 'None', 'value' => 'appliance'}
  end
  targets << {'name' => 'Instance', 'value' => 'instance'}
  targets << {'name' => 'Instance Label', 'value' => 'instance-label'}
  targets << {'name' => 'Server', 'value' => 'server'}
  targets << {'name' => 'Server Label', 'value' => 'server-label'}
  return targets
end

#get_available_contexts_for_workflow(workflow) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/morpheus/cli/mixins/jobs_helper.rb', line 188

def get_available_contexts_for_workflow(workflow)
  #If any task has target of resource, then CAN NOT run it local
  targets = []
  has_resource = workflow['taskSetTasks'].find {|task| task['executeTarget'] == 'resource' }
  if !has_resource
    targets << {'name' => 'None', 'value' => 'appliance'}
  end
  targets << {'name' => 'Instance', 'value' => 'instance'}
  targets << {'name' => 'Instance Label', 'value' => 'instance-label'}
  targets << {'name' => 'Server', 'value' => 'server'}
  targets << {'name' => 'Server Label', 'value' => 'server-label'}
  return targets
end

#get_process_event_data(process_or_event) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/morpheus/cli/mixins/jobs_helper.rb', line 21

def get_process_event_data(process_or_event)
  {
      id: process_or_event['id'],
      description: process_or_event['description'] || (process_or_event['refType'] == 'instance' ? process_or_event['displayName'] : (process_or_event['processTypeName'] || '').capitalize),
      start_date: format_local_dt(process_or_event['startDate']),
      created_by: process_or_event['createdBy'] ? process_or_event['createdBy']['displayName'] : '',
      duration: format_human_duration((process_or_event['duration'] || process_or_event['statusEta'] || 0) / 1000.0),
      status: format_job_status(process_or_event['status']),
      error: process_or_event['message'] || process_or_event['error'],
      output: process_or_event['output'],
  }
end

#jobs_interfaceObject



16
17
18
19
# File 'lib/morpheus/cli/mixins/jobs_helper.rb', line 16

def jobs_interface
  # get_interface('jobs')
  api_client.jobs
end


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/morpheus/cli/mixins/jobs_helper.rb', line 50

def print_job_execution(job_execution, options)
  process = job_execution['process']
  print cyan
  description_cols = {
      "ID" => lambda {|it| it['id'] },
      "Job" => lambda {|it| it['job'] ? it['job']['name'] : ''},
      "Job Type" => lambda {|it| it['job'] && it['job']['type'] ? (it['job']['type']['code'] == 'morpheus.workflow' ? 'Workflow' : 'Task') : ''},
      # "Description" => lambda {|it| it['description'] || (it['job'] ? it['job']['description'] : '') },
      "Start Date" => lambda {|it| format_local_dt(it['startDate'])},
      "ETA/Time" => lambda {|it| it['duration'] ? format_human_duration(it['duration'] / 1000.0) : ''},
      "Status" => lambda {|it| format_job_status(it['status'])},
      #"Output" => lambda {|it| it['process'] && (it['process']['output']) ?(it['process']['output']).to_s.strip : ''},
      #"Error" => lambda {|it| it['process'] && (it['process']['message'] || it['process']['error']) ? red + (it['process']['message'] || it['process']['error']).to_s.strip + cyan : ''},
      "Created By" => lambda {|it| it['createdBy'].nil? ? '' : it['createdBy']['displayName'] || it['createdBy']['username']}
  }
  print_description_list(description_cols, job_execution, options)

  if process
    process_data = get_process_event_data(process)
    print_h2 "Process Details"
    process_description_cols = {
        "Process ID" => lambda {|it| it[:id]},
        "Description" => lambda {|it| it[:description]},
        "Start Date" => lambda {|it| it[:start_date]},
        "Created By" => lambda {|it| it[:created_by]},
        "Duration" => lambda {|it| it[:duration]},
        "Status" => lambda {|it| it[:status]}
    }
    print_description_list(process_description_cols, process_data, options)

    if process_data[:output] && process_data[:output].strip.length > 0
      print_h2 "Output"
      print process['output'].to_s.strip
      print reset,"\n"
    end
    if process_data[:error] && process_data[:error].strip.length > 0
      print_h2 "Error"
      print red
      print (process['message'] || process['error']).to_s.strip
      print reset,"\n"
    end

    if process['events'] && !process['events'].empty?
      print_h2 "Process Events", options
      print_process_events(process['events'], options)
    end
  end
  print reset,"\n"
  return 0, nil
end


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/morpheus/cli/mixins/jobs_helper.rb', line 101

def print_job_executions(execs, options={})
  if execs.empty?
    print cyan,"No job executions found.",reset,"\n"
  else
    rows = execs.collect do |ex|
      {
          id: ex['id'],
          job: ex['job'] ? ex['job']['name'] : '',
          description: ex['description'] || ex['job'] ? ex['job']['description'] : '',
          type: ex['job'] && ex['job']['type'] ? (ex['job']['type']['code'] == 'morpheus.workflow' ? 'Workflow' : 'Task') : '',
          start: format_local_dt(ex['startDate']),
          duration: ex['duration'] ? format_human_duration(ex['duration'] / 1000.0) : '',
          status: format_job_status(ex['status']),
          error: truncate_string(ex['process'] && (ex['process']['message'] || ex['process']['error']) ? ex['process']['message'] || ex['process']['error'] : '', options[:details] ? nil : 32),
          output: truncate_string(ex['process'] && ex['process']['output'] ? ex['process']['output'] : '', options[:details] ? nil : 32),
      }
    end

    columns = [
        :id, :job, :type, {'START DATE' => :start}, {'ETA/TIME' => :duration}, :status, :error, :output
    ]
    print as_pretty_table(rows, columns, options)
  end
end

both process and process events



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/morpheus/cli/mixins/jobs_helper.rb', line 35

def print_process_events(events, options={})
  # event_columns = [:id, :description, :start_date, :created_by, :duration, :status, :error, :output]
  event_columns = {
        "ID" => lambda {|it| it[:id]},
        "Description" => lambda {|it| it[:description]},
        "Start Date" => lambda {|it| it[:start_date]},
        "Created By" => lambda {|it| it[:created_by]},
        "Duration" => lambda {|it| it[:duration]},
        "Status" => lambda {|it| it[:status]},
        "Error" => lambda {|it| options[:details] ? it[:error].to_s.strip : truncate_string(it[:error], 32).to_s.strip.gsub("\n", ' ') },
        "Output" => lambda {|it| options[:details] ? it[:output].to_s.strip : truncate_string(it[:output], 32).to_s.strip.gsub("\n", ' ') }
    }
  print as_pretty_table(events.collect {|it| get_process_event_data(it)}, event_columns.upcase_keys!, options)
end

#wait_for_job_execution(job_execution_id, options = {}, print_output = true) ⇒ Object

refresh execution request until it is finished returns json response data of the last execution request when status reached ‘completed’ or ‘failed’



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/morpheus/cli/mixins/jobs_helper.rb', line 152

def wait_for_job_execution(job_execution_id, options={}, print_output = true)
  refresh_interval = 5
  if options[:refresh_interval].to_i > 0
    refresh_interval = options[:refresh_interval]
  end
  refresh_display_seconds = refresh_interval % 1.0 == 0 ? refresh_interval.to_i : refresh_interval
  unless options[:quiet]
    print cyan, "Refreshing every #{refresh_display_seconds} seconds until execution is complete...", "\n", reset
  end
  job_execution = jobs_interface.get_execution(job_execution_id)['jobExecution']
  while ['new','queued','pending','running'].include?(job_execution['status']) do
    sleep(refresh_interval)
    job_execution = jobs_interface.get_execution(job_execution_id)['jobExecution']
  end
  if print_output && options[:quiet] != true
    print_h1 "Morpheus Job Execution", [], options
    print_job_execution(job_execution, options)
  end
  return job_execution
end