Class: Legion::CLI::Task

Inherits:
Thor
  • Object
show all
Defined in:
lib/legion/cli/task_command.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/legion/cli/task_command.rb', line 6

def self.exit_on_failure?
  true
end

Instance Method Details

#listObject



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

def list
  out = formatter
  with_data do
    dataset = Legion::Data::Model::Task.order(Sequel.desc(:id)).limit(options[:limit])
    dataset = dataset.where(Sequel.like(:status, "%#{options[:status]}%")) if options[:status]

    rows = dataset.map do |row|
      v = row.values
      [
        v[:id].to_s,
        v[:function_id].to_s,
        out.status(short_status(v[:status])),
        format_time(v[:created]),
        (v[:relationship_id] || '-').to_s
      ]
    end

    out.table(%w[id function status created relationship], rows)
  end
end

#logs(id) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/legion/cli/task_command.rb', line 87

def logs(id)
  out = formatter
  with_data do
    rows = Legion::Data::Model::TaskLog
           .where(task_id: id.to_i)
           .order(Sequel.desc(:id))
           .limit(options[:limit])
           .map do |row|
      v = row.values
      [
        v[:id].to_s,
        (v[:node_id] || '-').to_s,
        format_time(v[:created]),
        v[:entry].to_s
      ]
    end

    if rows.empty?
      out.warn("No logs found for task #{id}")
    else
      out.table(%w[id node created entry], rows)
    end
  end
end

#purgeObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/legion/cli/task_command.rb', line 149

def purge
  out = formatter
  with_data do
    cutoff = DateTime.now - options[:days]
    dataset = Legion::Data::Model::Task.where { created < cutoff }
    count = dataset.count

    if count.zero?
      out.success('No tasks to purge')
      return
    end

    unless options[:confirm]
      out.warn("This will delete #{count} tasks older than #{options[:days]} days")
      print '  Continue? [y/N] '
      response = $stdin.gets&.chomp
      unless response&.downcase == 'y'
        out.warn('Aborted')
        return
      end
    end

    dataset.delete
    out.success("Purged #{count} tasks")
  end
end

#show(id) ⇒ Object



42
43
44
45
46
47
48
49
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
# File 'lib/legion/cli/task_command.rb', line 42

def show(id)
  out = formatter
  with_data do
    task = Legion::Data::Model::Task[id.to_i]
    unless task
      out.error("Task #{id} not found")
      raise SystemExit, 1
    end

    v = task.values
    if options[:json]
      out.json(v)
      return
    end

    out.header("Task ##{v[:id]}")
    out.spacer
    out.detail({
                 id:              v[:id],
                 status:          v[:status],
                 function_id:     v[:function_id],
                 relationship_id: v[:relationship_id],
                 runner_id:       v[:runner_id],
                 created:         v[:created],
                 updated:         v[:updated],
                 parent_id:       v[:parent_id],
                 master_id:       v[:master_id]
               })

    if v[:args] && !v[:args].to_s.empty?
      out.spacer
      out.header('Arguments')
      begin
        args = Legion::JSON.load(v[:args])
        out.detail(args)
      rescue StandardError => e
        Legion::Logging.debug("TaskCommand#show args parse failed: #{e.message}") if defined?(Legion::Logging)
        puts "  #{v[:args]}"
      end
    end
  end
end

#trigger(function_spec = nil, *args) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/legion/cli/task_command.rb', line 127

def trigger(function_spec = nil, *args)
  out = formatter
  with_data do
    with_transport do
      target = resolve_target(function_spec, out)
      payload = parse_args(args, target[:function_args], out)

      result = execute_task(target, payload, out)

      if options[:json]
        out.json(result)
      else
        out.spacer
        out.success("Task #{result[:task_id]} #{result[:status]}")
      end
    end
  end
end