Skip to content

Commit

Permalink
Resque.inline configuration support
Browse files Browse the repository at this point in the history
In order perform Resque jobs inline introduced
Resque.inline attribute.
  • Loading branch information
bogdan authored and defunkt committed Mar 17, 2011
1 parent 5e89916 commit b4fcfa7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
5 changes: 5 additions & 0 deletions README.markdown
Expand Up @@ -662,6 +662,11 @@ this way we can tell our Sinatra app about the config file:

Now everyone is on the same page.

Also, you could disable jobs queueing by setting 'inline' attribute.
For example, if you want to run all jobs in the same process for cucumber, try:

Resque.inline = ENV['RAILS_ENV'] == "cucumber"


Plugins and Hooks
-----------------
Expand Down
15 changes: 13 additions & 2 deletions lib/resque.rb
Expand Up @@ -118,6 +118,17 @@ def to_s
"Resque Client connected to #{redis_id}"
end

# If 'inline' is true Resque will call #perform method inline
# without queuing it into Redis and without any Resque callbacks.
# The 'inline' is false Resque jobs will be put in queue regularly.
def inline?
@inline
end
alias_method :inline, :inline?

def inline=(inline)
@inline = inline
end

#
# queue manipulation
Expand Down Expand Up @@ -255,8 +266,8 @@ def reserve(queue)

# Validates if the given klass could be a valid Resque job
#
# If no queue can be inferred this method will raise a `Resque::NoQueueError`
#
# If no queue can be inferred this method will raise a `Resque::NoQueueError`
#
# If given klass is nil this method will raise a `Resque::NoClassError`
def validate!(klass)
Job.validate!(klass)
Expand Down
8 changes: 7 additions & 1 deletion lib/resque/job.rb
Expand Up @@ -42,10 +42,16 @@ def initialize(queue, payload)
def self.create(queue, klass, *args)
validate!(klass, queue)

ret = Resque.push(queue, :class => klass.to_s, :args => args)
if Resque.inline?
ret = constantize(klass).perform(*decode(encode(args)))
else
ret = Resque.push(queue, :class => klass.to_s, :args => args)
end

Plugin.after_enqueue_hooks(klass).each do |hook|
klass.send(hook, *args)
end

ret
end

Expand Down
12 changes: 11 additions & 1 deletion test/resque_test.rb
Expand Up @@ -8,7 +8,7 @@
Resque.push(:people, { 'name' => 'bob' })
Resque.push(:people, { 'name' => 'mark' })
end

test "can set a namespace through a url-like string" do
assert Resque.redis
assert_equal :resque, Resque.redis.namespace
Expand Down Expand Up @@ -237,4 +237,14 @@
Resque.decode("{\"error\":\"Module not found \\u002\"}")
end
end

test "inlining jobs" do
begin
Resque.inline = true
Resque.enqueue(SomeIvarJob, 20, '/tmp')
assert_equal 0, Resque.size(:ivar)
ensure
Resque.inline = false
end
end
end

0 comments on commit b4fcfa7

Please sign in to comment.