Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Porting resque_scheduler stuff from my resque fork

  • Loading branch information...
commit e240e3d6a3ba2663f64440f8b853ad3f407050db 1 parent 18e205f
Ben VandenBos authored
View
53 Rakefile
@@ -0,0 +1,53 @@
+load 'tasks/resque_scheduler.rake'
+
+$LOAD_PATH.unshift 'lib'
+
+task :default => :test
+
+desc "Run tests"
+task :test do
+ Dir['test/*_test.rb'].each do |f|
+ require f
+ end
+end
+
+
+desc "Build a gem"
+task :gem => [ :test, :gemspec, :build ]
+
+begin
+ begin
+ require 'jeweler'
+ rescue LoadError
+ puts "Jeweler not available. Install it with: "
+ puts "gem install jeweler"
+ end
+
+ require 'resque_scheduler/version'
+
+ Jeweler::Tasks.new do |gemspec|
+ gemspec.name = "resque-scheduler"
+ gemspec.summary = ""
+ gemspec.description = ""
+ gemspec.email = "bvandenbos@gmail.com"
+ gemspec.homepage = "http://github.com/bvandenbos/resque-scheduler"
+ gemspec.authors = ["Ben VandenBos"]
+ gemspec.version = ResqueScheduler::Version
+
+ gemspec.add_dependency "resque"
+ gemspec.add_dependency "rufus-scheduler"
+ gemspec.add_development_dependency "jeweler"
+ gemspec.add_development_dependency "mocha"
+ end
+end
+
+
+desc "Push a new version to Gemcutter"
+task :publish => [ :test, :gemspec, :build ] do
+ system "git tag v#{ResqueScheduler::Version}"
+ system "git push origin v#{ResqueScheduler::Version}"
+ system "git push origin master"
+ system "gem push pkg/resque-#{ResqueScheduler::Version}.gem"
+ system "git clean -fd"
+ exec "rake pages"
+end
View
48 lib/resque/scheduler.rb
@@ -0,0 +1,48 @@
+require 'rufus-scheduler'
+require 'rufus/scheduler'
+require 'thwait'
+require 'resque'
+
+module Resque
+
+ class Scheduler
+
+ extend Resque::Helpers
+
+ class << self
+
+ # Schedule all jobs and sleep (never returns)
+ def run(wait = true)
+ puts "Schedule empty! Set Resque.schedule" if Resque.schedule.empty?
+
+ Resque.schedule.values.each do |config|
+ rufus_scheduler.cron config['cron'] do
+ enqueue_from_config(config)
+ end
+ end
+ # sleep baby, sleep
+ ThreadsWait.all_waits(rufus_scheduler.instance_variable_get("@thread")) if wait
+ end
+
+ def enqueue_from_config(config)
+ params = config['args'].nil? ? [] : Array(config['args'])
+ Resque.enqueue(constantize(config['class']), *params)
+ end
+
+ def rufus_scheduler
+ @rufus_scheduler ||= Rufus::Scheduler.start_new
+ end
+
+ # Stops old rufus scheduler and creates a new one. Returns the new
+ # rufus scheduler
+ def clear_schedule!
+ rufus_scheduler.stop
+ @rufus_scheduler = nil
+ rufus_scheduler
+ end
+
+ end
+
+ end
+
+end
View
36 lib/resque_scheduler.rb
@@ -0,0 +1,36 @@
+
+
+require 'resque_scheduler/version'
+require 'resque/scheduler'
+
+module ResqueScheduler
+
+ #
+ # Accepts a new schedule configuration of the form:
+ #
+ # {some_name => {"cron" => "5/* * * *",
+ # "class" => DoSomeWork,
+ # "args" => "work on this string",
+ # "description" => "this thing works it"s butter off"},
+ # ...}
+ #
+ # :name can be anything and is used only to describe the scheduled job
+ # :cron can be any cron scheduling string :job can be any resque job class
+ # :class must be a resque worker class
+ # :args can be any yaml which will be converted to a ruby literal and passed
+ # in a params. (optional)
+ # :description is just that, a description of the job (optional). If params is
+ # an array, each element in the array is passed as a separate param,
+ # otherwise params is passed in as the only parameter to perform.
+ def schedule=(schedule_hash)
+ @schedule = schedule_hash
+ end
+
+ # Returns the schedule hash
+ def schedule
+ @schedule ||= {}
+ end
+
+end
+
+Resque.extend ResqueScheduler
View
14 lib/resque_scheduler/tasks.rb
@@ -0,0 +1,14 @@
+# require 'resque/tasks'
+# will give you the resque tasks
+
+namespace :resque do
+ task :setup
+
+ desc "Start Resque Scheduler"
+ task :scheduler => :setup do
+ require 'resque'
+
+ Resque::Scheduler.run
+ end
+
+end
View
3  lib/resque_scheduler/version.rb
@@ -0,0 +1,3 @@
+module ResqueScheduler
+ Version = '0.0.1'
+end
View
2  tasks/resque_scheduler.rake
@@ -0,0 +1,2 @@
+$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
+require 'resque_scheduler/tasks'
View
23 test/scheduler_test.rb
@@ -0,0 +1,23 @@
+require File.dirname(__FILE__) + '/test_helper'
+
+class Resque::SchedulerTest < Test::Unit::TestCase
+
+ def setup
+ Resque::Scheduler.clear_schedule!
+ end
+
+ def test_enqueue_from_config_puts_stuff_in_the_resque_queue
+ Resque.stubs(:enqueue).once.returns(true).with(SomeIvarJob, '/tmp')
+ Resque::Scheduler.enqueue_from_config('cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp")
+ end
+
+ def test_config_makes_it_into_the_rufus_scheduler
+ assert_equal(0, Resque::Scheduler.rufus_scheduler.all_jobs.size)
+
+ Resque.schedule = {:some_ivar_job => {'cron' => "* * * * *", 'class' => 'SomeIvarJob', 'args' => "/tmp"}}
+ Resque::Scheduler.run(false)
+
+ assert_equal(1, Resque::Scheduler.rufus_scheduler.all_jobs.size)
+ end
+
+end
View
14 test/test_helper.rb
@@ -0,0 +1,14 @@
+require 'rubygems'
+require 'test/unit'
+require 'mocha'
+$LOAD_PATH.unshift File.dirname(File.expand_path(__FILE__)) + '/../lib'
+require 'resque_scheduler'
+
+class SomeJob
+ def self.perform(repo_id, path)
+ end
+end
+
+class SomeIvarJob < SomeJob
+ @queue = :ivar
+end
Please sign in to comment.
Something went wrong with that request. Please try again.