Skip to content

Commit 9016204

Browse files
author
mpd
committed
Merge branch 'defunkt' into distributed
Conflicts: lib/resque/worker.rb
2 parents d68e999 + d85097d commit 9016204

File tree

11 files changed

+117
-36
lines changed

11 files changed

+117
-36
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ group :test do
77
gem "rack-test", "~> 0.5"
88
gem "mocha", "~> 0.9.7"
99
gem "leftright", :platforms => :mri_18
10+
gem "yajl-ruby", "~>0.8.2", :platforms => :mri
11+
gem "json", "~>1.5.3", :platforms => :jruby
1012
end

README.markdown

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -560,13 +560,8 @@ together. But, it's not that hard.
560560
Resque Dependencies
561561
-------------------
562562

563-
gem install redis redis-namespace yajl-ruby vegas sinatra
564-
565-
If you cannot install `yajl-ruby` (JRuby?), you can install the `json`
566-
gem and Resque will use it instead.
567-
568-
When problems arise, make sure you have the newest versions of the
569-
`redis` and `redis-namespace` gems.
563+
$ gem install bundler
564+
$ bundle install
570565

571566

572567
Installing Resque
@@ -606,7 +601,7 @@ Alternately you can define a `resque:setup` hook in your Rakefile if you
606601
don't want to load your app every time rake runs.
607602

608603

609-
### In a Rails app, as a gem
604+
### In a Rails 2.x app, as a gem
610605

611606
First install the gem.
612607

@@ -637,7 +632,7 @@ Don't forget you can define a `resque:setup` hook in
637632
`lib/tasks/whatever.rake` that loads the `environment` task every time.
638633

639634

640-
### In a Rails app, as a plugin
635+
### In a Rails 2.x app, as a plugin
641636

642637
$ ./script/plugin install git://github.com/defunkt/resque
643638

@@ -652,6 +647,40 @@ Don't forget you can define a `resque:setup` hook in
652647
`lib/tasks/whatever.rake` that loads the `environment` task every time.
653648

654649

650+
### In a Rails 3 app, as a gem
651+
652+
First include it in your Gemfile.
653+
654+
$ cat Gemfile
655+
...
656+
gem 'resque'
657+
...
658+
659+
Next install it with Bundler.
660+
661+
$ bundle install
662+
663+
Now start your application:
664+
665+
$ rails server
666+
667+
That's it! You can now create Resque jobs from within your app.
668+
669+
To start a worker, add this to a file in `lib/tasks` (ex:
670+
`lib/tasks/resque.rake`):
671+
672+
``` ruby
673+
require 'resque/tasks'
674+
```
675+
676+
Now:
677+
678+
$ QUEUE=* rake environment resque:work
679+
680+
Don't forget you can define a `resque:setup` hook in
681+
`lib/tasks/whatever.rake` that loads the `environment` task every time.
682+
683+
655684
Configuration
656685
-------------
657686

docs/HOOKS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ An unnamed hook (`before_perform`) will be executed first.
6666

6767
The available hooks are:
6868

69+
* `before _enqueue`: Called with the job args before a job is placed on the queue.
70+
If the hook returns `false`, the job will not be placed on the queue.
71+
6972
* `after_enqueue`: Called with the job args after a job is placed on the queue.
7073
Any exception raised propagates up to the code which queued the job.
7174

examples/god/resque.god

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ num_workers = rails_env == 'production' ? 5 : 2
44

55
num_workers.times do |num|
66
God.watch do |w|
7+
w.dir = "#{rails_root}"
78
w.name = "resque-#{num}"
89
w.group = 'resque'
910
w.interval = 30.seconds

lib/resque.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
require 'redis/namespace'
22

3-
begin
4-
require 'yajl'
5-
rescue LoadError
6-
require 'json'
7-
end
8-
93
require 'resque/version'
104

115
require 'resque/errors'
@@ -230,6 +224,12 @@ def watch_queue(queue)
230224
#
231225
# This method is considered part of the `stable` API.
232226
def enqueue(klass, *args)
227+
# Perform before_enqueue hooks. Don't perform enqueue if any hook returns false
228+
before_hooks = Plugin.before_enqueue_hooks(klass).collect do |hook|
229+
klass.send(hook, *args)
230+
end
231+
return if before_hooks.any? { |result| result == false }
232+
233233
Job.create(queue_from_class(klass), klass, *args)
234234

235235
Plugin.after_enqueue_hooks(klass).each do |hook|

lib/resque/failure/redis.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def save
99
:payload => payload,
1010
:exception => exception.class.to_s,
1111
:error => exception.to_s,
12-
:backtrace => Array(exception.backtrace),
12+
:backtrace => filter_backtrace(Array(exception.backtrace)),
1313
:worker => worker.to_s,
1414
:queue => queue
1515
}
@@ -41,6 +41,11 @@ def self.remove(index)
4141
Resque.redis.lset(:failed, index, id)
4242
Resque.redis.lrem(:failed, 1, id)
4343
end
44+
45+
def filter_backtrace(backtrace)
46+
index = backtrace.index { |item| item.include?('/lib/resque/job.rb') }
47+
backtrace.first(index.to_i)
48+
end
4449
end
4550
end
4651
end

lib/resque/helpers.rb

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
require 'multi_json'
2+
3+
# OkJson won't work because it doesn't serialize symbols
4+
# in the same way yajl and json do.
5+
if MultiJson.engine.to_s == 'MultiJson::Engines::OkJson'
6+
raise "Please install the yajl-ruby or json gem"
7+
end
8+
19
module Resque
210
# Methods used by various classes in Resque.
311
module Helpers
@@ -11,29 +19,17 @@ def redis
1119
# Given a Ruby object, returns a string suitable for storage in a
1220
# queue.
1321
def encode(object)
14-
if defined? Yajl
15-
Yajl::Encoder.encode(object)
16-
else
17-
object.to_json
18-
end
22+
::MultiJson.encode(object)
1923
end
2024

2125
# Given a string, returns a Ruby object.
2226
def decode(object)
2327
return unless object
2428

25-
if defined? Yajl
26-
begin
27-
Yajl::Parser.parse(object, :check_utf8 => false)
28-
rescue Yajl::ParseError => e
29-
raise DecodeException, e
30-
end
31-
else
32-
begin
33-
JSON.parse(object)
34-
rescue JSON::ParserError => e
35-
raise DecodeException, e
36-
end
29+
begin
30+
::MultiJson.decode(object)
31+
rescue ::MultiJson::DecodeError => e
32+
raise DecodeException, e
3733
end
3834
end
3935

lib/resque/plugin.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,10 @@ def failure_hooks(job)
4747
def after_enqueue_hooks(job)
4848
job.methods.grep(/^after_enqueue/).sort
4949
end
50+
51+
# Given an object, returns a list `before_enqueue` hook names.
52+
def before_enqueue_hooks(job)
53+
job.methods.grep(/^before_enqueue/).sort
54+
end
5055
end
5156
end

lib/resque/worker.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ def self.working
3737

3838
reportedly_working = begin
3939
redis.mapped_mget(*names).reject do |key, value|
40-
value.nil?
40+
value.nil? || value.empty?
4141
end
4242
rescue Redis::Distributed::CannotDistribute
4343
result = {}
4444
names.each do |name|
4545
value = redis.get name
46-
result[name] = value unless value.nil?
46+
result[name] = value unless value.nil? || value.empty?
4747
end
4848
result
4949
end

resque.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Gem::Specification.new do |s|
2424
s.add_dependency "redis-namespace", "~> 1.0.2"
2525
s.add_dependency "vegas", "~> 0.1.2"
2626
s.add_dependency "sinatra", ">= 0.9.2"
27-
s.add_dependency "json", ">= 1.4.6", "< 1.6"
27+
s.add_dependency "multi_json", "~> 1.0"
2828

2929
s.description = <<description
3030
Resque is a Redis-backed Ruby library for creating background jobs,

0 commit comments

Comments
 (0)