Class: Gleis::Scheduler

Inherits:
Object
  • Object
show all
Defined in:
lib/gleis/scheduler.rb

Overview

The class implements the methods required to manage the scheduler service of a Gleis app

Class Method Summary collapse

Class Method Details

.add(app_name, frequency, time, command) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/gleis/scheduler.rb', line 4

def self.add(app_name, frequency, time, command)
  token = Token.check
  body = API.request('post', 'scheduler', token, 'name': app_name, 'frequency': frequency, 'time': time,
                                                 'command': command)
  if body['success'] == 1
    puts "Successfully added scheduler job to #{app_name}."
  else
    puts "Failed to add scheduler job: #{body['message']}"
  end
end

.list(app_name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gleis/scheduler.rb', line 15

def self.list(app_name)
  token = Token.check
  body = API.request('get', "scheduler/#{app_name}", token)
  jobs = body ['data']
  if jobs.first.empty?
    puts 'No scheduled jobs.'
  else
    puts "List of scheduled jobs:\n\n"
    printf("\t%s\n", 'TIME')
    printf("\t%-30s %-15s %s\n", 'COMMAND', 'FREQUENCY', 'LAST RUN')
    printf("\t%-30s %-15s %s\n\n", '-------', '---------', '--------')
    jobs.each do |job|
      last_run = job['last_run'] ? Time.parse(job['last_run']).localtime.strftime('%c') : 'never'
      printf("\t%-30s %-15s %s\n", job['cron_time'], job['frequency'], last_run)
      puts "\t#{job['command']}\n\n"
    end
  end
end

.remove(app_name) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/gleis/scheduler.rb', line 34

def self.remove(app_name)
  token = Token.check
  body = API.request('delete', "scheduler/#{app_name}", token)
  if body['success'] == 1
    puts "Successfully removed scheduled job from #{app_name}."
  else
    puts "Failed to remove scheduled job: #{body['message']}"
  end
end