Allow creation of new job types from within schedule.rb. Not only does this make it easier to define app-specific job types such as the new rails 3 runner, or jobs which need to run under bundler, but it also allows the user to create reusable system commands rather than using the :command method every time.

This commit is contained in:
Damien 2010-05-29 20:28:03 +08:00 committed by Javan Makhmali
parent 8fdd66adc0
commit 480a0a9e04
4 changed files with 44 additions and 1 deletions

View File

@ -23,6 +23,7 @@ end
require 'whenever/base'
require 'whenever/job_list'
require 'whenever/job_types/default'
require 'whenever/job_types/user_defined'
require 'whenever/job_types/rake_task'
require 'whenever/job_types/runner'
require 'whenever/outputs/cron'

View File

@ -47,6 +47,19 @@ module Whenever
@jobs[@current_time_scope] << options[:class].new(@options.merge(:task => task).merge(options))
end
def job_type(name, options = {}, &block)
(class << self; self; end).class_eval do
define_method(name) do |task|
new_job = Whenever::Job::UserDefined.new
block.call(new_job)
options = new_job.to_options
options.reverse_merge!(:environment => @environment, :path => @path)
options[:class] = Whenever::Job::UserDefined
command(task, options)
end
end
end
def runner(task, options = {})
options.reverse_merge!(:environment => @environment, :path => @path)
options[:class] = Whenever::Job::Runner

View File

@ -4,7 +4,7 @@ module Whenever
def output
path_required
%Q(cd #{File.join(@path)} && bundle exec rails runner -e #{@environment} #{task.inspect})
%Q(cd #{File.join(@path)} && script/runner -e #{@environment} #{task.inspect})
end
end

View File

@ -0,0 +1,29 @@
module Whenever
module Job
class UserDefined < Whenever::Job::Default
attr_accessor :uses_bundler, :command, :environment
def initialize(options={})
super
@uses_bundler = options[:uses_bundler]
@command = options[:command]
@env = options[:environment]
end
def to_options
{ :uses_bundler => uses_bundler, :command => command, :environment => environment }
end
def output
out = []
out << "cd #{File.join(@path)} &&"
out << "bundle exec" if uses_bundler
out << command
out << "-e #{@environment}" if @env
out << task.inspect
out.join(" ")
end
end
end
end