Class: ScheduleJob::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/schedule_job/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(argv = ARGV) ⇒ Object



5
6
7
8
9
# File 'lib/schedule_job/cli.rb', line 5

def self.run(argv = ARGV)
  cli = Cli.new
  args = cli.get_args(argv)
  cli.execute(args)
end

Instance Method Details

#execute(args) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/schedule_job/cli.rb', line 64

def execute(args)
  case
  when args[:list_jobs]               # read jobs
    list_jobs(args)
  when args[:remove_job]              # remove job
    remove_job(args)
  when args[:cron] || args[:every]    # write jobs
    install_job(args)
  else
    puts "No parameters specified. Please specify one of: --every, --cron, --list, or --rm"
  end
end

#get_args(argv = ARGV) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/schedule_job/cli.rb', line 11

def get_args(argv = ARGV)
  options = {}

  OptionParser.new do |opts|
    opts.banner = "Usage: schedule [options] command"

    opts.on("-d", "--dryrun", "Report what would happen but do not install the scheduled job.") do |dryrun|
      options[:dryrun] = dryrun
    end

    opts.on("-l", "--list", "List installed cron jobs.") do |list|
      options[:list_jobs] = list
    end

    opts.on("-r", "--rm JOB_ID", "Remove the specified job id as indicated by --list") do |job_id_to_remove|
      options[:remove_job] = job_id_to_remove
    end

    opts.on("-u", "--user USER", "User that the crontab belongs to.") do |user|
      options[:crontab_user] = user
    end

    opts.on("-e", "--every DURATION", "Run command every DURATION units of time (valid suffixes are m, h, d, M). DURATION may also be one of the special keywords: reboot, year, month, week, day, hour
    For example:
    --every 10m                       # every 10 minutes
    --every 5h                        # every 5 hours
    --every 2d                        # every 2 days
    --every 3M                        # every 3 months

    Special durations:
    --every reboot                    # after every reboot
    --every year                      # every year
    --every month                     # every month
    --every week                      # every week
    --every day                       # every day
    --every hour                      # every hour
    ") do |every|
      options[:every] = every
    end

    opts.on("-c", "--cron CRON", "Run command on the given cron schedule.
    For example:
    --cron \"*/5 15 * * 1-5\"          # Every 5 minutes, at 3:00 PM, Monday through Friday
    --cron \"0 0/30 8-9 5,20 * ?\"     # Every 30 minutes, between 8:00 AM and 9:59 AM, on day 5 and 20 of the month
    ") do |cron_schedule|
      options[:cron] = cron_schedule
    end
  end.parse!

  options[:command] = ARGV.join(" ").strip
  options
end

#install_job(args) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/schedule_job/cli.rb', line 85

def install_job(args)
  dry_run = args[:dryrun]
  command = args[:command]
  user = args[:crontab_user]

  if command.empty?
    puts "A cron job command hasn't been specified. Please specify which command you would like to run."
    exit(1)
  end

  job = case
  when args[:cron]
    cron_schedule = args[:cron]
    Cron::Job.new(cron_schedule, command)
  when args[:every]
    duration = args[:every]
    if match = duration.match(/(\d+)(m|h|d|M)/)
      qty = match[1].to_i
      unit_of_time = match[2].to_s
      Cron::Job.every(qty, unit_of_time, command)
    elsif duration.match(/reboot|year|month|week|day|hour/)
      Cron::Job.every_simple_duration(duration, command)
    else
      puts "'#{duration}' is an invalid duration. The duration must be specified as either (1) integer followed immediately by m (minutes), h (hours), d (days), M (months) (e.g. 10m) or (2) reboot, year, month, week, day, hour"
      exit(1)
    end
  end

  table = Cron::Table.new(user)
  table.add(job, dry_run)
end

#list_jobs(args) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/schedule_job/cli.rb', line 77

def list_jobs(args)
  user = args[:crontab_user]
  
  table = Cron::Table.new(user)
  
  puts table.to_s unless table.empty?
end

#remove_job(args) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/schedule_job/cli.rb', line 117

def remove_job(args)
  dry_run = args[:dryrun]
  user = args[:crontab_user]
  job_id = args[:remove_job].to_i   # this is a 1-based index that should match the indices from the --list command

  table = Cron::Table.new(user)

  table.remove(job_id, dry_run)
end