Class: Holistics::Jobs

Inherits:
SubcommandBase show all
Defined in:
lib/holistics/jobs.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SubcommandBase

banner, subcommand_prefix

Constructor Details

#initialize(*args) ⇒ Jobs

Returns a new instance of Jobs.


3
4
5
6
# File 'lib/holistics/jobs.rb', line 3

def initialize(*args)
  super
  @this = Model::Job.new(Holistics.client, 'jobs')
end

Class Method Details

.help(*args) ⇒ Object


7
8
9
10
11
12
13
14
# File 'lib/holistics/jobs.rb', line 7

def self.help(*args)
  super
  puts "Examples:\nholistics jobs list -t transform # List all transform jobs\n\n"
end

Instance Method Details

#cancel(id) ⇒ Object


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/holistics/jobs.rb', line 80

def cancel(id)
  puts "`jobs cancel #{id}` called with options: #{options}" if Holistics.debug?
  print "Cancelling Job #{id} ...".yellow
  result = @this.cancel(id)
  if result['status'].to_s.upcase == 'OK'
    loop do
      job_info = @this.find(id)
      unless job_info['status'] == 'cancelling'
        puts Holistics::Utils.colorize(job_info['status'])
        break
      end
      sleep 0.5
    end
  else
    puts "Failed to cancel the job #{id}".red
  end
end

#info(id) ⇒ Object


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/holistics/jobs.rb', line 41

def info(id)
  puts "`jobs info #{id}` called with options: #{options}" if Holistics.debug?
  item = @this.find(id)
  puts "Job ID: " + item['id'].to_s.yellow
  {
    status: Holistics::Utils.colorize(item['status']),
    source_method: item['source_method'],
    source_type: item['source_type'],
    source_id: item['source_id'],
    created_at: DateTime.parse(item['created_at']).to_formatted_s(:short),
    user_id: item['user_id'],
    start_time: item['start_time'] ? DateTime.parse(item['start_time']).to_formatted_s(:short) : nil,
    end_time: item['end_time'] ? DateTime.parse(item['end_time']).to_formatted_s(:short) : nil,
    tenant_id: item['tenant_id'],
    duration: (item['duration'] < 0 ? nil : Time.at(item['duration']).utc.strftime("%H:%M:%S"))
  }.each { |k, v| puts "\t" + [k,v].join(": ") }
end

#listObject


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/holistics/jobs.rb', line 18

def list
  puts "`jobs list` called with options: #{options}" if Holistics.debug?
  tp(
    @this.all.map do |item|
      {
        id: item['id'],
        source_method: item['source_method'],
        title: item['title'],
        created_at: DateTime.parse(item['created_at']).to_formatted_s(:short),
        source_type: item['source_type'],
        source_id: item['source_id'],
        # start_time: item['start_time'] ? DateTime.parse(item['start_time']).to_formatted_s(:short) : nil,
        # end_time: item['end_time'] ? DateTime.parse(item['end_time']).to_formatted_s(:short) : nil,
        duration: (item['duration'] < 0 ? nil : Time.at(item['duration']).utc.strftime("%H:%M:%S")),
        # cancelledable
        user: item['user']['name'],
        status: Holistics::Utils.colorize(item['status'])
      }
    end
  )
end

#logs(id) ⇒ Object


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/holistics/jobs.rb', line 62

def logs(id)
  puts "`jobs logs #{id}` called with options: #{options}" if Holistics.debug?
  if options[:follow]
    last_id = 0
    loop do
      data = @this.logs(id, { 'last_id': last_id })
      print_logs(id, data['logs'])
      last_id = data['logs'].last['id'] if data['logs'].length > 0
      break unless data['has_more']
      sleep 0.5
    end
  else
    data = @this.logs(id)
    print_logs(id, data['logs'], options[:number])
  end
end