Added RakeTask job type and helper and refactored the job types. Cleaned up and added tests.
This commit is contained in:
parent
e24a8341a8
commit
01b7b81a10
13
lib/base.rb
13
lib/base.rb
@ -1,12 +1,15 @@
|
|||||||
require 'job_list'
|
|
||||||
require 'job_types/default'
|
|
||||||
require 'job_types/runner'
|
|
||||||
require 'outputs/cron'
|
|
||||||
|
|
||||||
module Whenever
|
module Whenever
|
||||||
VERSION = '0.1.0'
|
VERSION = '0.1.0'
|
||||||
|
|
||||||
def self.cron(options)
|
def self.cron(options)
|
||||||
Whenever::JobList.new(options).generate_cron_output
|
Whenever::JobList.new(options).generate_cron_output
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.path
|
||||||
|
if defined?(RAILS_ROOT)
|
||||||
|
RAILS_ROOT
|
||||||
|
elsif defined?(::RAILS_ROOT)
|
||||||
|
::RAILS_ROOT
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
@ -41,10 +41,16 @@ module Whenever
|
|||||||
end
|
end
|
||||||
|
|
||||||
def runner(task, options = {})
|
def runner(task, options = {})
|
||||||
options.reverse_merge!(:environment => @runner_environment, :path => @runner_path)
|
options.reverse_merge!(:environment => @environment, :path => @path)
|
||||||
options[:class] = Whenever::Job::Runner
|
options[:class] = Whenever::Job::Runner
|
||||||
command(task, options)
|
command(task, options)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def rake(task, options = {})
|
||||||
|
options.reverse_merge!(:environment => @environment, :path => @path)
|
||||||
|
options[:class] = Whenever::Job::RakeTask
|
||||||
|
command(task, options)
|
||||||
|
end
|
||||||
|
|
||||||
def generate_cron_output
|
def generate_cron_output
|
||||||
[environment_variables, cron_jobs].compact.join
|
[environment_variables, cron_jobs].compact.join
|
||||||
|
@ -1,17 +1,27 @@
|
|||||||
module Whenever
|
module Whenever
|
||||||
module Job
|
module Job
|
||||||
class Default
|
class Default
|
||||||
|
|
||||||
attr_accessor :task, :at, :cron_log
|
attr_accessor :task, :at, :cron_log
|
||||||
|
|
||||||
def initialize(options = {})
|
def initialize(options = {})
|
||||||
@task = options[:task]
|
@task = options[:task]
|
||||||
@at = options[:at]
|
@at = options[:at]
|
||||||
@cron_log = options[:cron_log]
|
@cron_log = options[:cron_log]
|
||||||
|
@environment = options[:environment] || :production
|
||||||
|
@path = options[:path] || Whenever.path
|
||||||
end
|
end
|
||||||
|
|
||||||
def output
|
def output
|
||||||
task
|
task
|
||||||
end
|
end
|
||||||
|
|
||||||
|
protected
|
||||||
|
|
||||||
|
def path_required
|
||||||
|
raise ArgumentError, "No path available; set :path, '/your/path' in your schedule file" if @path.blank?
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
12
lib/job_types/rake_task.rb
Normal file
12
lib/job_types/rake_task.rb
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
module Whenever
|
||||||
|
module Job
|
||||||
|
class RakeTask < Whenever::Job::Default
|
||||||
|
|
||||||
|
def output
|
||||||
|
path_required
|
||||||
|
"cd #{@path} && RAILS_ENV=#{@environment} /usr/bin/env rake #{task}"
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -1,30 +1,12 @@
|
|||||||
module Whenever
|
module Whenever
|
||||||
module Job
|
module Job
|
||||||
class Runner < Whenever::Job::Default
|
class Runner < Whenever::Job::Default
|
||||||
|
|
||||||
def initialize(options = {})
|
|
||||||
super(options)
|
|
||||||
|
|
||||||
@environment = options[:environment] || :production
|
|
||||||
|
|
||||||
if [Whenever::Job::Runner.rails_root, options[:path]].all?(&:blank?)
|
|
||||||
raise ArgumentError, "no cron_path available for runner to use"
|
|
||||||
else
|
|
||||||
@path = options[:path] || Whenever::Job::Runner.rails_root
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.rails_root
|
|
||||||
if defined?(RAILS_ROOT)
|
|
||||||
RAILS_ROOT
|
|
||||||
elsif defined?(::RAILS_ROOT)
|
|
||||||
::RAILS_ROOT
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def output
|
def output
|
||||||
|
path_required
|
||||||
%Q(#{File.join(@path, 'script', 'runner')} -e #{@environment} "#{task}")
|
%Q(#{File.join(@path, 'script', 'runner')} -e #{@environment} "#{task}")
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -22,3 +22,8 @@ require 'activesupport'
|
|||||||
require 'chronic'
|
require 'chronic'
|
||||||
|
|
||||||
require 'base'
|
require 'base'
|
||||||
|
require 'job_list'
|
||||||
|
require 'job_types/default'
|
||||||
|
require 'job_types/rake_task'
|
||||||
|
require 'job_types/runner'
|
||||||
|
require 'outputs/cron'
|
||||||
|
@ -4,7 +4,7 @@ class OutputCommandTest < Test::Unit::TestCase
|
|||||||
|
|
||||||
context "A plain command" do
|
context "A plain command" do
|
||||||
setup do
|
setup do
|
||||||
@output = load_whenever_output \
|
@output = Whenever.cron \
|
||||||
<<-file
|
<<-file
|
||||||
every 2.hours do
|
every 2.hours do
|
||||||
command "blahblah"
|
command "blahblah"
|
||||||
@ -19,7 +19,7 @@ class OutputCommandTest < Test::Unit::TestCase
|
|||||||
|
|
||||||
context "A command when the cron_log is set" do
|
context "A command when the cron_log is set" do
|
||||||
setup do
|
setup do
|
||||||
@output = load_whenever_output \
|
@output = Whenever.cron \
|
||||||
<<-file
|
<<-file
|
||||||
set :cron_log, 'logfile.log'
|
set :cron_log, 'logfile.log'
|
||||||
every 2.hours do
|
every 2.hours do
|
||||||
@ -35,7 +35,7 @@ class OutputCommandTest < Test::Unit::TestCase
|
|||||||
|
|
||||||
context "A command when the cron_log is set and the comand overrides it" do
|
context "A command when the cron_log is set and the comand overrides it" do
|
||||||
setup do
|
setup do
|
||||||
@output = load_whenever_output \
|
@output = Whenever.cron \
|
||||||
<<-file
|
<<-file
|
||||||
set :cron_log, 'logfile.log'
|
set :cron_log, 'logfile.log'
|
||||||
every 2.hours do
|
every 2.hours do
|
||||||
@ -52,7 +52,7 @@ class OutputCommandTest < Test::Unit::TestCase
|
|||||||
|
|
||||||
context "A command when the cron_log is set and the comand rejects it" do
|
context "A command when the cron_log is set and the comand rejects it" do
|
||||||
setup do
|
setup do
|
||||||
@output = load_whenever_output \
|
@output = Whenever.cron \
|
||||||
<<-file
|
<<-file
|
||||||
set :cron_log, 'logfile.log'
|
set :cron_log, 'logfile.log'
|
||||||
every 2.hours do
|
every 2.hours do
|
||||||
|
@ -4,7 +4,7 @@ class OutputEnvTest < Test::Unit::TestCase
|
|||||||
|
|
||||||
context "The output from Whenever with environment variables set" do
|
context "The output from Whenever with environment variables set" do
|
||||||
setup do
|
setup do
|
||||||
@output = load_whenever_output \
|
@output = Whenever.cron \
|
||||||
<<-file
|
<<-file
|
||||||
env :MYVAR, 'blah'
|
env :MYVAR, 'blah'
|
||||||
env 'MAILTO', "someone@example.com"
|
env 'MAILTO', "someone@example.com"
|
||||||
|
74
test/output_rake_test.rb
Normal file
74
test/output_rake_test.rb
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class OutputRakeTest < Test::Unit::TestCase
|
||||||
|
|
||||||
|
# Rake are generated in an almost identical way to runners so we
|
||||||
|
# only need some basic tests to ensure they are output correctly
|
||||||
|
|
||||||
|
context "A rake command with path set" do
|
||||||
|
setup do
|
||||||
|
@output = Whenever.cron \
|
||||||
|
<<-file
|
||||||
|
set :path, '/my/path'
|
||||||
|
every 2.hours do
|
||||||
|
rake "blahblah"
|
||||||
|
end
|
||||||
|
file
|
||||||
|
end
|
||||||
|
|
||||||
|
should "output the rake command using that path" do
|
||||||
|
assert_match two_hours + ' cd /my/path && RAILS_ENV=production /usr/bin/env rake blahblah', @output
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "A rake command that overrides the path set" do
|
||||||
|
setup do
|
||||||
|
@output = Whenever.cron \
|
||||||
|
<<-file
|
||||||
|
set :path, '/my/path'
|
||||||
|
every 2.hours do
|
||||||
|
rake "blahblah", :path => '/some/other/path'
|
||||||
|
end
|
||||||
|
file
|
||||||
|
end
|
||||||
|
|
||||||
|
should "output the rake command using that path" do
|
||||||
|
assert_match two_hours + ' cd /some/other/path && RAILS_ENV=production /usr/bin/env rake blahblah', @output
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "A rake command with environment set" do
|
||||||
|
setup do
|
||||||
|
@output = Whenever.cron \
|
||||||
|
<<-file
|
||||||
|
set :environment, :silly
|
||||||
|
set :path, '/my/path'
|
||||||
|
every 2.hours do
|
||||||
|
rake "blahblah"
|
||||||
|
end
|
||||||
|
file
|
||||||
|
end
|
||||||
|
|
||||||
|
should "output the rake command using that environment" do
|
||||||
|
assert_match two_hours + ' cd /my/path && RAILS_ENV=silly /usr/bin/env rake blahblah', @output
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "A rake command that overrides the environment set" do
|
||||||
|
setup do
|
||||||
|
@output = Whenever.cron \
|
||||||
|
<<-file
|
||||||
|
set :environment, :silly
|
||||||
|
set :path, '/my/path'
|
||||||
|
every 2.hours do
|
||||||
|
rake "blahblah", :environment => :serious
|
||||||
|
end
|
||||||
|
file
|
||||||
|
end
|
||||||
|
|
||||||
|
should "output the rake command using that environment" do
|
||||||
|
assert_match two_hours + ' cd /my/path && RAILS_ENV=serious /usr/bin/env rake blahblah', @output
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
@ -2,11 +2,11 @@ require 'test_helper'
|
|||||||
|
|
||||||
class OutputRunnerTest < Test::Unit::TestCase
|
class OutputRunnerTest < Test::Unit::TestCase
|
||||||
|
|
||||||
context "A runner with runner_path set" do
|
context "A runner with path set" do
|
||||||
setup do
|
setup do
|
||||||
@output = load_whenever_output \
|
@output = Whenever.cron \
|
||||||
<<-file
|
<<-file
|
||||||
set :runner_path, '/my/path'
|
set :path, '/my/path'
|
||||||
every 2.hours do
|
every 2.hours do
|
||||||
runner "blahblah"
|
runner "blahblah"
|
||||||
end
|
end
|
||||||
@ -18,11 +18,27 @@ class OutputRunnerTest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "A runner with no runner_path set and RAILS_ROOT defined" do
|
context "A runner that overrides the path set" do
|
||||||
setup do
|
setup do
|
||||||
Whenever::Job::Runner.stubs(:rails_root).returns('/my/path')
|
@output = Whenever.cron \
|
||||||
|
<<-file
|
||||||
|
set :path, '/my/path'
|
||||||
|
every 2.hours do
|
||||||
|
runner "blahblah", :path => '/some/other/path'
|
||||||
|
end
|
||||||
|
file
|
||||||
|
end
|
||||||
|
|
||||||
|
should "output the runner using that path" do
|
||||||
|
assert_match two_hours + ' /some/other/path/script/runner -e production "blahblah"', @output
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "A runner with no path set and RAILS_ROOT defined" do
|
||||||
|
setup do
|
||||||
|
Whenever.stubs(:path).returns('/my/path')
|
||||||
|
|
||||||
@output = load_whenever_output \
|
@output = Whenever.cron \
|
||||||
<<-file
|
<<-file
|
||||||
every 2.hours do
|
every 2.hours do
|
||||||
runner "blahblah"
|
runner "blahblah"
|
||||||
@ -35,28 +51,28 @@ class OutputRunnerTest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "A runner with runner_path set AND RAILS_ROOT defined" do
|
context "A runner with path set AND RAILS_ROOT defined" do
|
||||||
setup do
|
setup do
|
||||||
Whenever::Job::Runner.stubs(:rails_root).returns('/my/path')
|
Whenever.stubs(:path).returns('/my/rails/path')
|
||||||
|
|
||||||
@output = load_whenever_output \
|
@output = Whenever.cron \
|
||||||
<<-file
|
<<-file
|
||||||
set :runner_path, '/my/path'
|
set :path, '/my/path'
|
||||||
every 2.hours do
|
every 2.hours do
|
||||||
runner "blahblah"
|
runner "blahblah"
|
||||||
end
|
end
|
||||||
file
|
file
|
||||||
end
|
end
|
||||||
|
|
||||||
should "use the runner_path" do
|
should "use the path" do
|
||||||
assert_match two_hours + ' /my/path/script/runner -e production "blahblah"', @output
|
assert_match two_hours + ' /my/path/script/runner -e production "blahblah"', @output
|
||||||
assert_no_match /\/rails\/path/, @output
|
assert_no_match /\/rails\/path/, @output
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "A runner with no runner_path set and no RAILS_ROOT defined" do
|
context "A runner with no path set and no RAILS_ROOT defined" do
|
||||||
setup do
|
setup do
|
||||||
Whenever::Job::Runner.stubs(:rails_root).returns(nil)
|
Whenever.stubs(:path).returns(nil)
|
||||||
|
|
||||||
@input = <<-file
|
@input = <<-file
|
||||||
every 2.hours do
|
every 2.hours do
|
||||||
@ -67,9 +83,43 @@ class OutputRunnerTest < Test::Unit::TestCase
|
|||||||
|
|
||||||
should "raise an exception" do
|
should "raise an exception" do
|
||||||
assert_raises ArgumentError do
|
assert_raises ArgumentError do
|
||||||
load_whenever_output(@input)
|
Whenever.cron(@input)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "A runner with an environment set" do
|
||||||
|
setup do
|
||||||
|
@output = Whenever.cron \
|
||||||
|
<<-file
|
||||||
|
set :environment, :silly
|
||||||
|
set :path, '/my/path'
|
||||||
|
every 2.hours do
|
||||||
|
runner "blahblah"
|
||||||
|
end
|
||||||
|
file
|
||||||
|
end
|
||||||
|
|
||||||
|
should "output the runner using that environment" do
|
||||||
|
assert_match two_hours + ' /my/path/script/runner -e silly "blahblah"', @output
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "A runner that overrides the environment set" do
|
||||||
|
setup do
|
||||||
|
@output = Whenever.cron \
|
||||||
|
<<-file
|
||||||
|
set :environment, :silly
|
||||||
|
set :path, '/my/path'
|
||||||
|
every 2.hours do
|
||||||
|
runner "blahblah", :environment => :serious
|
||||||
|
end
|
||||||
|
file
|
||||||
|
end
|
||||||
|
|
||||||
|
should "output the runner using that environment" do
|
||||||
|
assert_match two_hours + ' /my/path/script/runner -e serious "blahblah"', @output
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
@ -21,13 +21,10 @@ require 'whenever'
|
|||||||
|
|
||||||
module TestExtensions
|
module TestExtensions
|
||||||
|
|
||||||
def load_whenever_output(input)
|
|
||||||
Whenever.cron(input)
|
|
||||||
end
|
|
||||||
|
|
||||||
def two_hours
|
def two_hours
|
||||||
"0 0,2,4,6,8,10,12,14,16,18,20,22 * * *"
|
"0 0,2,4,6,8,10,12,14,16,18,20,22 * * *"
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class Test::Unit::TestCase
|
class Test::Unit::TestCase
|
||||||
|
Loading…
Reference in New Issue
Block a user