Skip to content

Commit

Permalink
Allow creation of new job types from within schedule.rb. Not only doe…
Browse files Browse the repository at this point in the history
…s 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.
  • Loading branch information
Damien authored and javan committed Jun 11, 2010
1 parent 8fdd66a commit 480a0a9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/whenever.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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'
Expand Down
13 changes: 13 additions & 0 deletions lib/whenever/job_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ def command(task, options = {})
@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
Expand Down
2 changes: 1 addition & 1 deletion lib/whenever/job_types/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Runner < Whenever::Job::Default

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
Expand Down
29 changes: 29 additions & 0 deletions lib/whenever/job_types/user_defined.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 480a0a9

Please sign in to comment.