Class: Cyclid::Cli::Job

Inherits:
Thor
  • Object
show all
Defined in:
lib/cyclid/cli/job.rb

Overview

‘job’ sub-command

Instance Method Summary collapse

Instance Method Details

#format(filename) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/cyclid/cli/job.rb', line 187

def format(filename)
  job = parse_job_file(filename)

  # Re-emit; Oj & YAML are nice and trustworthy in that they wont re-order objects. This
  # means that we can nicely indent, or convert, without worrying that things like the
  # Sequence is going to get re-ordered.
  #
  # The downside is that we can't force things like ordering of top-level elements,
  # although users may consider that an advantage.
  if options[:yamlout]
    puts YAML.dump(job)
  else
    puts Oj.dump(job, indent: 2)
  end
end

#lint(filename) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/cyclid/cli/job.rb', line 135

def lint(filename)
  job = parse_job_file(filename)

  # Now run the job through the Verifier to find problems
  verifier = RemoteVerifier.new(client: client)
  verifier.verify(job)

  # Display the result summary
  verifier.status.messages.each do |message|
    case message[:type]
    when Cyclid::Linter::StatusLogger::MessageTypes::WARNING
      Formatter.warning 'Warning', message[:text]
    when Cyclid::Linter::StatusLogger::MessageTypes::ERROR
      Formatter.error 'Error', message[:text]
    end
  end

  Formatter.puts ''
  Formatter.error 'Errors', verifier.status.errors \
    unless verifier.status.errors.zero?
  Formatter.warning 'Warnings', verifier.status.warnings \
    unless verifier.status.warnings.zero?

  # Finish with an exit code of 1 if there were problems
  exit 1 unless verifier.status.errors.zero? && verifier.status.warnings.zero?
end

#listObject



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/cyclid/cli/job.rb', line 98

def list
  stats = client.job_stats(client.config.organization)
  all = client.job_list(client.config.organization, limit: stats['total'])
  jobs = all['records']

  jobs.each do |job|
    Formatter.colorize 'Name', (job['job_name'] || '')
    Formatter.colorize "\tJob", job['id'].to_s
    Formatter.colorize "\tVersion", (job['job_version'] || '')
  end
rescue StandardError => ex
  abort "Failed to get job list: #{ex}"
end

#log(jobid) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/cyclid/cli/job.rb', line 89

def log(jobid)
  job_log = client.job_log(client.config.organization, jobid)

  puts job_log['log']
rescue StandardError => ex
  abort "Failed to get job log: #{ex}"
end

#show(jobid) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cyclid/cli/job.rb', line 51

def show(jobid)
  job = client.job_get(client.config.organization, jobid)

  status_id = job['status']
  status = Cyclid::API::Constants::JOB_STATUSES[status_id]

  started = job['started'].nil? ? nil : Time.parse(job['started'])
  ended = job['ended'].nil? ? nil : Time.parse(job['ended'])

  # Calculate the duration if the job contains a valid start & end time
  duration = (Time.new(0) + (ended - started) if started && ended)

  # Pretty-print the job details (without the log)
  Formatter.colorize 'Job', job['id'].to_s
  Formatter.colorize 'Name', (job['job_name'] || '')
  Formatter.colorize 'Version', (job['job_version'] || '')
  Formatter.colorize 'Started', (started ? started.asctime : '')
  Formatter.colorize 'Ended', (ended ? ended.asctime : '')
  Formatter.colorize 'Duration', duration.strftime('%H:%M:%S') if duration
  Formatter.colorize 'Status', status
rescue StandardError => ex
  abort "Failed to get job status: #{ex}"
end

#statsObject



113
114
115
116
117
118
119
# File 'lib/cyclid/cli/job.rb', line 113

def stats
  stats = client.job_stats(client.config.organization)

  Formatter.colorize 'Total jobs', stats['total'].to_s
rescue StandardError => ex
  abort "Failed to get job list: #{ex}"
end

#status(jobid) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/cyclid/cli/job.rb', line 76

def status(jobid)
  job_status = client.job_status(client.config.organization, jobid)

  status_id = job_status['status']
  status = Cyclid::API::Constants::JOB_STATUSES[status_id]

  # Pretty-print the job status
  Formatter.colorize 'Status', status
rescue StandardError => ex
  abort "Failed to get job status: #{ex}"
end

#submit(filename) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/cyclid/cli/job.rb', line 41

def submit(filename)
  job_data, job_type = load_job_file(filename)

  job_info = client.job_submit(client.config.organization, job_data, job_type)
  Formatter.colorize 'Job', job_info['job_id'].to_s
rescue StandardError => ex
  abort "Failed to submit job: #{ex}"
end