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:
parent
8fdd66adc0
commit
480a0a9e04
@ -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'
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
29
lib/whenever/job_types/user_defined.rb
Normal file
29
lib/whenever/job_types/user_defined.rb
Normal 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
|
Loading…
Reference in New Issue
Block a user