Class: Legion::CLI::Schedule

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/legion/cli/schedule_command.rb', line 8

def self.exit_on_failure?
  true
end

Instance Method Details

#addObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/legion/cli/schedule_command.rb', line 59

def add
  out = formatter

  unless options[:cron] || options[:interval]
    out.error('Either --cron or --interval is required')
    return
  end

  payload = { function_id: options[:function_id], active: true }
  payload[:cron] = options[:cron] if options[:cron]
  payload[:interval] = options[:interval] if options[:interval]
  payload[:description] = options[:description] if options[:description]

  result = api_post('/api/schedules', **payload)
  if options[:json]
    out.json(result)
  else
    out.success("Schedule ##{result[:id]} created")
  end
end

#listObject



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

def list
  out = formatter
  query = "/api/schedules?limit=#{options[:limit]}"
  query += '&active=true' if options[:active]
  schedules = api_get(query)
  schedules = [] if schedules.nil?

  if options[:json]
    out.json(schedules)
  else
    rows = Array(schedules).map do |s|
      [s[:id], s[:function_id] || '-', s[:cron] || s[:interval] || '-',
       out.status(s[:active] ? 'active' : 'inactive'), s[:description] || '-']
    end
    out.table(%w[ID Function Schedule Status Description], rows)
    puts "  #{rows.size} schedule(s)"
  end
end

#logs(id) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/legion/cli/schedule_command.rb', line 100

def logs(id)
  out = formatter
  log_entries = api_get("/api/schedules/#{id}/logs?limit=#{options[:limit]}")
  log_entries = [] if log_entries.nil?

  if options[:json]
    out.json(log_entries)
  else
    out.header("Logs for Schedule ##{id}")
    if Array(log_entries).empty?
      puts '  No logs found.'
    else
      rows = Array(log_entries).map do |l|
        [l[:id], l[:status] || '-', l[:started_at]&.to_s || '-', l[:message] || '-']
      end
      out.table(%w[ID Status Started Message], rows)
    end
  end
end

#remove(id) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/legion/cli/schedule_command.rb', line 82

def remove(id)
  out = formatter

  unless options[:yes]
    print "Delete schedule ##{id}? [y/N] "
    return unless $stdin.gets&.strip&.downcase == 'y'
  end

  result = api_delete("/api/schedules/#{id}")
  if options[:json]
    out.json({ id: id.to_i, deleted: true }.merge(result || {}))
  else
    out.success("Schedule ##{id} deleted")
  end
end

#show(id) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/legion/cli/schedule_command.rb', line 41

def show(id)
  out = formatter
  schedule = api_get("/api/schedules/#{id}")

  if options[:json]
    out.json(schedule)
  else
    out.header("Schedule ##{id}")
    out.spacer
    out.detail(schedule.transform_keys(&:to_s))
  end
end