Permalink
Browse files

Handle job_type settings more gracefully and allow quoting of the tas…

…k to be supressed
  • Loading branch information...
1 parent 7e6a199 commit b664c037e45a18d903b6c9c2a5dfefd125c99a0e @idlefingers idlefingers committed with Jun 2, 2010
Showing with 191 additions and 20 deletions.
  1. +12 −4 README.rdoc
  2. +32 −13 lib/whenever/job_types/user_defined.rb
  3. +147 −3 test/output_user_defined_test.rb
View
@@ -73,25 +73,33 @@ You can define custom job types in your schedule file:
job_type :rails3_runner do |job|
job.command = "rails runner"
- job.uses_bundler = true
+ job.settings = { :use_bundler => true }
end
every 1.day, :at => "2am" do
rails3_runner "MyModel.do_something_amazing"
end
-This would create a job with the command "bundle exec rails runner".
+This would create a job like: <tt>cd /path/to/app && bundle exec rails runner "MyModel.do_something_amazing"</tt>
By defining jobs like this, you can also make reusable system commands such as:
job_type :my_great_command do |job|
- job.no_environment = true # this stops whenever appending '-e production' to the task
job.command = "/usr/bin/my_great_command"
+ job.settings = { :environment => false, :path => false, :quote_task => false }
end
every 3.days do
- my_great_command "arg1 arg2 arg3"
+ my_great_command "arg1 arg2 'arg3'"
end
+
+This would create a job like: <tt>/usr/bin/my_great_command arg1 arg2 'arg3'</tt>
+
+The settings available when creating job types include:
+
+environment:: Set to <tt>false</tt> to stop the command trying to specify the environment at all. Set to a string or symbol to set the environment to that.
+path:: Set to <tt>false</tt> to stop the command running cd before the task. Set to a string or symbol to set the path the cd to.
+use_bundler:: If set to <tt>true</tt>, the task will be prefixed by <tt>bundle exec</tt>
== Credit
@@ -1,29 +1,48 @@
module Whenever
module Job
class UserDefined < Whenever::Job::Default
- attr_accessor :uses_bundler, :command, :no_environment
+ attr_accessor :command, :settings
- def initialize(options={})
+ def initialize(opts={})
super
- @uses_bundler = options[:uses_bundler]
- @command = options[:command]
- @no_environment = options[:no_environment]
+ @command = opts[:command]
+ @settings = opts[:settings] ||= {}
end
def to_options
- { :uses_bundler => uses_bundler, :command => command, :no_environment => no_environment }
+ { :command => command, :settings => settings }
end
def output
- out = []
- out << "cd #{File.join(@path)} &&"
- out << "bundle exec" if uses_bundler
- out << command
- out << "-e #{@environment}" unless no_environment
- out << task.inspect
- out.join(" ")
+ [ cd_path, bundler, command, app_environment, quoted_task ].compact.join(" ")
end
+ private
+
+ def cd_path
+ parse_setting :path, "cd #{settings[:path]} &&", "cd #{File.join(@path)} &&"
+ end
+
+ def app_environment
+ parse_setting :environment, "-e #{settings[:environment]}", "-e #{@environment}"
+ end
+
+ def bundler
+ "bundle exec" if settings[:use_bundler]
+ end
+
+ def parse_setting(name, with_option, without_option)
+ if settings.has_key? name
+ with_option if settings[name]
+ else
+ without_option
+ end
+ end
+
+ def quoted_task
+ settings[:quote_task] == false ? task : task.inspect
+ end
+
end
end
end
@@ -2,7 +2,7 @@
class OutputUserDefinedTest < Test::Unit::TestCase
- context "A simple custom job type" do
+ context "A custom job type" do
setup do
@output = Whenever.cron \
<<-file
@@ -21,14 +21,74 @@ class OutputUserDefinedTest < Test::Unit::TestCase
end
end
+ context "A custom job type which does not quote the task" do
+ setup do
+ @output = Whenever.cron \
+ <<-file
+ set :path, '/my/path'
+ job_type :simple_job do |job|
+ job.command = "hello"
+ job.settings = { :quote_task => false }
+ end
+ every 2.hours do
+ simple_job "blahblah"
+ end
+ file
+ end
+
+ should "output the command using simple_job and not quote the task" do
+ assert_match two_hours + ' cd /my/path && hello -e production blahblah', @output
+ end
+ end
+
+ context "A custom job type with no path" do
+ setup do
+ @output = Whenever.cron \
+ <<-file
+ set :path, '/my/path'
+ job_type :simple_job do |job|
+ job.command = "hello"
+ job.settings = { :path => false }
+ end
+ every 2.hours do
+ simple_job "blahblah"
+ end
+ file
+ end
+
+ should "output the command using simple_job without changing directory first" do
+ assert_match two_hours + ' hello -e production "blahblah"', @output
+ end
+ end
+
+ context "A custom job type with path" do
+ setup do
+ @output = Whenever.cron \
+ <<-file
+ set :path, '/my/path'
+ job_type :simple_job do |job|
+ job.command = "hello"
+ job.settings = { :path => "/path/to/app" }
+ end
+ every 2.hours do
+ simple_job "blahblah"
+ end
+ file
+ end
+
+ should "output the command using simple_job and a custom path" do
+ assert_match two_hours + ' cd /path/to/app && hello -e production "blahblah"', @output
+ end
+ end
+
context "A custom job which uses bunder" do
setup do
@output = Whenever.cron \
<<-file
set :path, '/my/path'
job_type :simple_job do |job|
job.command = "hello"
- job.uses_bundler = true
+ job.settings = { :use_bundler => true }
end
every 2.hours do
simple_job "blahblah"
@@ -41,14 +101,55 @@ class OutputUserDefinedTest < Test::Unit::TestCase
end
end
+ context "A custom job which uses bunder and a custom path" do
+ setup do
+ @output = Whenever.cron \
+ <<-file
+ set :path, '/my/path'
+ job_type :simple_job do |job|
+ job.command = "hello"
+ job.settings = { :use_bundler => true, :path => "/path/to/app" }
+ end
+ every 2.hours do
+ simple_job "blahblah"
+ end
+ file
+ end
+
+ should "output the command using simple_job with bundler and a custom path" do
+ assert_match two_hours + ' cd /path/to/app && bundle exec hello -e production "blahblah"', @output
+ end
+ end
+
+ context "A custom job with settings set individually" do
+ setup do
+ @output = Whenever.cron \
+ <<-file
+ set :path, '/my/path'
+ job_type :simple_job do |job|
+ job.command = "hello"
+ job.settings[:use_bundler] = true
+ job.settings[:path] = "/path/to/app"
+ end
+ every 2.hours do
+ simple_job "blahblah"
+ end
+ file
+ end
+
+ should "output the command with all the settings as expected" do
+ assert_match two_hours + ' cd /path/to/app && bundle exec hello -e production "blahblah"', @output
+ end
+ end
+
context "A custom job which doesn't use an environment" do
setup do
@output = Whenever.cron \
<<-file
set :path, '/my/path'
job_type :simple_job do |job|
job.command = "hello"
- job.no_environment = true
+ job.settings = { :environment => false }
end
every 2.hours do
simple_job "blahblah"
@@ -80,6 +181,26 @@ class OutputUserDefinedTest < Test::Unit::TestCase
end
end
+ context "A custom job that overrides the path set from the job_type definition" do
+ setup do
+ @output = Whenever.cron \
+ <<-file
+ set :path, '/my/path'
+ job_type :simple_job do |job|
+ job.command = "hello"
+ job.settings = { :path => "/some/other/path" }
+ end
+ every 2.hours do
+ simple_job "blahblah"
+ end
+ file
+ end
+
+ should "output the runner using that path" do
+ assert_match two_hours + ' cd /some/other/path && hello -e production "blahblah"', @output
+ end
+ end
+
context "A custom job with an environment set" do
setup do
@output = Whenever.cron \
@@ -120,6 +241,27 @@ class OutputUserDefinedTest < Test::Unit::TestCase
end
end
+ context "A custom job that overrides the environment set in the job_type definition" do
+ setup do
+ @output = Whenever.cron \
+ <<-file
+ set :environment, :silly
+ set :path, '/my/path'
+ job_type :simple_job do |job|
+ job.command = "hello"
+ job.settings = { :environment => :serious }
+ end
+ every 2.hours do
+ simple_job "blahblah"
+ end
+ file
+ end
+
+ should "output the runner using that environment" do
+ assert_match two_hours + ' cd /my/path && hello -e serious "blahblah"', @output
+ end
+ end
+
context "A custom job where the environment is overridden using the :set option" do
setup do
@output = Whenever.cron :set => 'environment=serious', :string => \
@@ -160,4 +302,6 @@ class OutputUserDefinedTest < Test::Unit::TestCase
end
end
+
+
end

0 comments on commit b664c03

Please sign in to comment.