Class: Colonel::Crontab

Inherits:
Object
  • Object
show all
Defined in:
lib/colonel/crontab.rb

Defined Under Namespace

Classes: TimeCreator

Instance Method Summary collapse

Constructor Details

#initialize(user) ⇒ Crontab

Returns a new instance of Crontab.



5
6
7
# File 'lib/colonel/crontab.rb', line 5

def initialize(user)
  @user = user
end

Instance Method Details

#linesObject



13
14
15
# File 'lib/colonel/crontab.rb', line 13

def lines
  @_lines ||= read.split(/\n/)
end

#readObject



9
10
11
# File 'lib/colonel/crontab.rb', line 9

def read
  @tab = `crontab -l`
end

#reject_if_wrong(line) ⇒ Object



21
22
23
# File 'lib/colonel/crontab.rb', line 21

def reject_if_wrong(line)
  !line.match /^(\d+|\*)/
end

#reject_uselessObject



17
18
19
# File 'lib/colonel/crontab.rb', line 17

def reject_useless
  lines.reject { |line| reject_if_wrong(line) }
end

#update(jobs) ⇒ Object



25
26
27
28
# File 'lib/colonel/crontab.rb', line 25

def update(jobs)
  update_tab(jobs)
  write
end

#update_tab(jobs) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/colonel/crontab.rb', line 30

def update_tab(jobs)
  tab_lines = jobs.map do |j|
    [
      TimeCreator.new(
        minutes:  j.schedule.minutes,
        hours:    j.schedule.hours,
        days:     j.schedule.days,
        months:   j.schedule.months,
        weekdays: j.schedule.weekdays,
      ).generate,
      j.command.get
    ].join("\t\t\t")
  end
  tab_lines
  @tab = "# --- Updated with Colonel ---\n" + tab_lines.join("\n") + "\n# --- \n"
end

#writeObject



53
54
55
56
57
# File 'lib/colonel/crontab.rb', line 53

def write
  write_to_temp
  `crontab #{@tempfile.path}`
  @tempfile.delete
end

#write_to_tempObject



47
48
49
50
51
# File 'lib/colonel/crontab.rb', line 47

def write_to_temp
  @tempfile = Tempfile.new("#{@user}_crontab_temp")
  @tempfile.write(@tab)
  @tempfile.close
end