From 4ceb6905d8f55b23ce2d1e2df4aa129039525874 Mon Sep 17 00:00:00 2001 From: Nikolay Moskvin Date: Sun, 14 Dec 2014 18:11:48 +1100 Subject: [PATCH 1/2] Added capability for updating cron without cmd (cherry picked from commit fce04f7c8485e537ad89bdf43d5b4fa9d8b860fb) --- lib/whenever.rb | 7 +++++++ lib/whenever/command_line.rb | 30 ++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/lib/whenever.rb b/lib/whenever.rb index b22281ea..d1099120 100644 --- a/lib/whenever.rb +++ b/lib/whenever.rb @@ -31,4 +31,11 @@ def self.script_rails? def self.bundler? File.exist?(File.join(path, 'Gemfile')) end + + def self.update_cron options + o = { update: true, console: false} + o.merge! options if options + Whenever::CommandLine.execute o + end + end diff --git a/lib/whenever/command_line.rb b/lib/whenever/command_line.rb index b75ea7e3..25b9da0c 100644 --- a/lib/whenever/command_line.rb +++ b/lib/whenever/command_line.rb @@ -13,20 +13,21 @@ def initialize(options={}) @options[:file] ||= 'config/schedule.rb' @options[:cut] ||= 0 @options[:identifier] ||= default_identifier + @options[:console] = true if @options[:console].nil? if !File.exist?(@options[:file]) && @options[:clear].nil? warn("[fail] Can't find file: #{@options[:file]}") - exit(1) + return_or_exit(false) end if [@options[:update], @options[:write], @options[:clear]].compact.length > 1 warn("[fail] Can only update, write or clear. Choose one.") - exit(1) + return_or_exit(false) end unless @options[:cut].to_s =~ /[0-9]*/ warn("[fail] Can't cut negative lines from the crontab #{options[:cut]}") - exit(1) + return_or_exit(false) end @options[:cut] = @options[:cut].to_i @@ -42,7 +43,7 @@ def run puts Whenever.cron(@options) puts "## [message] Above is your schedule file converted to cron syntax; your crontab file was not updated." puts "## [message] Run `whenever --help' for more options." - exit(0) + return_or_exit(true) end end @@ -85,10 +86,10 @@ def write_crontab(contents) action = 'written' if @options[:write] action = 'updated' if @options[:update] puts "[write] crontab file #{action}" - exit(0) + return_or_exit(true) else warn "[fail] Couldn't write crontab; try running `whenever' with no options to ensure your schedule file is valid." - exit(1) + return_or_exit(false) end end @@ -96,10 +97,10 @@ def updated_crontab # Check for unopened or unclosed identifier blocks if read_crontab =~ Regexp.new("^#{comment_open_regex}\s*$") && (read_crontab =~ Regexp.new("^#{comment_close_regex}\s*$")).nil? warn "[fail] Unclosed indentifier; Your crontab file contains '#{comment_open(false)}', but no '#{comment_close(false)}'" - exit(1) + return_or_exit(false) elsif (read_crontab =~ Regexp.new("^#{comment_open_regex}\s*$")).nil? && read_crontab =~ Regexp.new("^#{comment_close_regex}\s*$") warn "[fail] Unopened indentifier; Your crontab file contains '#{comment_close(false)}', but no '#{comment_open(false)}'" - exit(1) + return_or_exit(false) end # If an existing identifier block is found, replace it with the new cron entries @@ -151,5 +152,18 @@ def comment_close_regex def timestamp_regex " at: \\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2} ([+-]\\d{4}|UTC)" end + + private + + def return_or_exit success + result = 1 + result = 0 if success + if @options[:console] + exit(result) + else + result + end + end + end end From 96a8fadbda74d935d39cf2d1f3fefbc217990d74 Mon Sep 17 00:00:00 2001 From: Nikolay Moskvin Date: Wed, 9 Mar 2016 15:42:51 +0500 Subject: [PATCH 2/2] Replaced ':' to '=>' for support old ruby lang (cherry picked from commit d1fa4c076c1109ffd1dd9e4abc14f48ff4b3d733) --- lib/whenever.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/whenever.rb b/lib/whenever.rb index d1099120..d2ef6dd7 100644 --- a/lib/whenever.rb +++ b/lib/whenever.rb @@ -33,7 +33,7 @@ def self.bundler? end def self.update_cron options - o = { update: true, console: false} + o = { 'update' => true, 'console' => false} o.merge! options if options Whenever::CommandLine.execute o end