4
5
6
7
8
9
10
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
|
# File 'lib/crontab_rb/write.rb', line 4
def self.write_crontab
command = 'crontab -'
shortcut_jobs, regular_jobs = [], []
contents = ''
records = Database.all
records.each do |record|
options = {
at: record[:at].presence,
job_template: "/bin/bash -l -c ':job'",
template: "cd :path && :bundle_command :runner_command -e :environment ':task' :output",
environment_variable: 'RAILS_ENV',
environment: 'production',
path: Dir.pwd,
chronic_options: {},
runner_command: "bin/rails runner",
bundle_command: "bundle exec",
task: record[:command],
mailto: ''
}
job = Whenever::Job.new(options)
Whenever::Output::Cron.output(record[:time], job, chronic_options: {}) do |cron|
cron << "\n\n"
if cron[0,1] == "@"
shortcut_jobs << cron
else
regular_jobs << cron
end
end
end
contents = shortcut_jobs.join + combine(regular_jobs).join
IO.popen(command, 'r+') do |crontab|
crontab.write(contents)
crontab.close_write
end
success = $?.exitstatus.zero?
if success
puts "[write] crontab file updated"
end
end
|