Class: MultiserverWhenever

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

Instance Method Summary collapse

Constructor Details

#initialize(set_vars = {}) ⇒ MultiserverWhenever

Returns a new instance of MultiserverWhenever.



4
5
6
7
# File 'lib/multiserver_whenever.rb', line 4

def initialize(set_vars = {})
  defaults = {:environment => 'production'}
  @set_vars = defaults.merge(set_vars)
end

Instance Method Details

#clear!Object



9
10
11
12
13
# File 'lib/multiserver_whenever.rb', line 9

def clear!
  current_whenever_identifiers.each do |identifier|
    whenever_with_vars "--load-file #{dummy_whenever_path} --clear-crontab #{identifier}"
  end
end

#configObject



35
36
37
# File 'lib/multiserver_whenever.rb', line 35

def config
  @config ||= YAML.load(read_config)
end

#current_whenever_identifiersObject



23
24
25
# File 'lib/multiserver_whenever.rb', line 23

def current_whenever_identifiers
  @identifiers ||= parse_identifiers
end

#dummy_whenever_pathObject

So for some reason, the whenever command requires that the –load-file argument be passed, even if you are using the –clear-crontab argument to clear out outdated crontabs.

The problem is that you may need to clear out old crontab data from cron that was generated by a whenever file that no longer exists, so you can’t use it for the –load-file argument. As it’s unnecessary anyways, I’m just going to use the config/whenever/do_not_remove.rb file anytime we’re clearing crontabs out. It’s an “empty” file, but we need to keep it around.



59
60
61
# File 'lib/multiserver_whenever.rb', line 59

def dummy_whenever_path
  "config/whenever/do_not_remove.rb"
end

#hostnameObject



43
44
45
# File 'lib/multiserver_whenever.rb', line 43

def hostname
  command('hostname')
end

#parse_identifiersObject



63
64
65
66
67
# File 'lib/multiserver_whenever.rb', line 63

def parse_identifiers
  lines = read_cron.split("\n")
  lines = lines.select {|line| line =~ /^# Begin Whenever/ }
  lines.map {|line| line.gsub("# Begin Whenever generated tasks for: ", '') }
end

#read_configObject



39
40
41
# File 'lib/multiserver_whenever.rb', line 39

def read_config
  File.read("config/whenever.yml")
end

#read_cronObject



27
28
29
# File 'lib/multiserver_whenever.rb', line 27

def read_cron
  command("crontab -l")
end

#rolesObject



31
32
33
# File 'lib/multiserver_whenever.rb', line 31

def roles
  [config[hostname]].flatten.compact
end

#write!Object



15
16
17
18
19
20
21
# File 'lib/multiserver_whenever.rb', line 15

def write!
  roles.each do |role|
    relative_path = "config/whenever/#{role}.rb"
    absolute_path = File.expand_path(relative_path)
    whenever_with_vars "--load-file #{relative_path} --update-crontab #{absolute_path}"
  end
end