module Morpheus::Cli::JobsHelper
Mixin for Morpheus::Cli
command classes Provides refreshing job execution records by id
Public Class Methods
Source
# 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
Public Instance Methods
Source
# 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
Source
# 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
Source
# 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
Source
# 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
Source
# 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
Source
# File lib/morpheus/cli/mixins/jobs_helper.rb, line 16 def jobs_interface # get_interface('jobs') api_client.jobs end
Source
# 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
Source
# 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
Source
# 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
both process and process events
Source
# 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
refresh execution request until it is finished returns json response data of the last execution request when status reached ‘completed’ or ‘failed’