Skip to content

Commit a569f81

Browse files
committed
Support old rake tasks and more
* User config class * Add global --redis, --namespace * Add --config option which loads config from YAML * Preload config in every method (unfortunately couldn't find `before_filter` or something like that functionality in thor)
1 parent 705d2ee commit a569f81

File tree

6 files changed

+73
-11
lines changed

6 files changed

+73
-11
lines changed

Rakefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'rubygems'
22
require 'bundler/setup'
33
require 'bundler/gem_tasks'
4+
require 'resque/tasks'
45
require 'rake/testtask'
56
require 'yard'
67

bin/resque

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
#!/usr/bin/env ruby
2-
3-
require 'thor'
42
require 'resque/cli'
53

64
Resque::CLI.start(ARGV)

examples/sinatra/Gemfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# A sample Gemfile
22
source "https://rubygems.org"
33

4+
gem "rake"
45
gem "sinatra"
5-
gem "resque", path: "../../"
6+
gem "resque", path: "../../"

examples/sinatra/Rakefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require "resque/tasks"

lib/resque/cli.rb

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
require 'thor'
12
require "resque"
23

34
module Resque
45
class CLI < Thor
6+
class_option :config, :aliases => ["-c"], :type => :string
7+
class_option :redis, :aliases => ["-R"], :type => :string
8+
class_option :namespace, :aliases => ["-N"], :type => :string
59

610
desc "work", "Start processing jobs."
711
option :queue, :aliases => ["-q"], :type => :string, :default => "*"
@@ -13,30 +17,33 @@ class CLI < Thor
1317
#option :verbose, :aliases => ["-v"], :type => :boolean, :default => false
1418
#option :vverbose, :aliases => ["-vv"], :type => :boolean, :default => false
1519
def work
16-
queues = options[:queue].to_s.split(',')
20+
load_config
1721

18-
load_enviroment(options[:require])
19-
worker = Resque::Worker.new(*queues)
22+
load_enviroment(Resque.config.require)
23+
worker = Resque::Worker.new(*Resque.config.queues)
2024

21-
worker.term_timeout = options[:timeout]
25+
worker.term_timeout = Resque.config.timeout
2226
#worker.verbose = options[:verbose]
2327
#worker.very_verbose = options[:vverbose]
2428

25-
if options[:deamon]
29+
if Resque.config.deamon
2630
Process.daemon(true)
2731
end
2832

29-
if options.has_key?(:pid)
30-
File.open(options[:pid], 'w') { |f| f << worker.pid }
33+
if Resque.config.pid
34+
File.open(Resque.config.pid, 'w') { |f| f << worker.pid }
3135
end
3236

3337
Resque.logger.info "Starting worker #{worker}"
3438

35-
worker.work(options[:interval]) # interval, will block
39+
worker.work(Resque.config.interval) # interval, will block
3640
end
3741

3842
desc "kill WORKER", "Kills a worker"
3943
def kill(worker)
44+
load_config
45+
46+
before_action
4047
pid = worker.split(':')[1].to_i
4148

4249
begin
@@ -51,12 +58,16 @@ def kill(worker)
5158

5259
desc "remove WORKER", "Removes a worker"
5360
def remove(worker)
61+
load_config
62+
5463
Resque.remove_worker(worker)
5564
puts "Removed #{worker}"
5665
end
5766

5867
desc "list", "Lists known workers"
5968
def list
69+
load_config
70+
6071
if Resque.workers.any?
6172
Resque.workers.each do |worker|
6273
puts "#{worker} (#{worker.state})"
@@ -68,6 +79,12 @@ def list
6879

6980
protected
7081

82+
def load_config
83+
Resque.config = Resque::Config.new(YAML.load_file(File.expand_path(options[:config]))) if options[:config]
84+
Resque.config.redis = options[:redis] if options[:redis]
85+
Resque.config.namespace = options[:namespace] if options[:namespace]
86+
end
87+
7188
def load_enviroment(file = nil)
7289
return if file.nil?
7390

lib/resque/tasks.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# require 'resque/tasks'
2+
# will give you the resque tasks
3+
require 'resque/cli'
4+
5+
namespace :resque do
6+
task :setup
7+
8+
desc "Start a Resque worker"
9+
task :work => [ :preload, :setup ] do
10+
Resque::CLI.new.work
11+
end
12+
13+
desc "Start multiple Resque workers. Should only be used in dev mode."
14+
task :workers do
15+
Resque::CLI.new.workers
16+
end
17+
18+
# Preload app files if this is Rails
19+
task :preload => :setup do
20+
if defined?(Rails) && Rails.respond_to?(:application)
21+
# Rails 3
22+
Rails.application.eager_load!
23+
elsif defined?(Rails::Initializer)
24+
# Rails 2.3
25+
$rails_rake_task = false
26+
Rails::Initializer.run :load_application_classes
27+
end
28+
end
29+
30+
namespace :failures do
31+
desc "Sort the 'failed' queue for the redis_multi_queue failure backend"
32+
task :sort do
33+
require 'resque'
34+
require 'resque/failure/redis'
35+
36+
warn "Sorting #{Resque::Failure.count} failures..."
37+
Resque::Failure.each(0, Resque::Failure.count) do |_, failure|
38+
data = Resque.encode(failure)
39+
Resque.redis.rpush(Resque::Failure.failure_queue_name(failure['queue']), data)
40+
end
41+
warn "done!"
42+
end
43+
end
44+
end

0 commit comments

Comments
 (0)