Skip to content

Commit

Permalink
Implement use so workers can use middleware.
Browse files Browse the repository at this point in the history
Note: I'm ~99.9% sure we'll want to use `unshift` rather than `<<` in
Gearup::Builder#use, but I'll let tests involving multiple middleware
decide :)
  • Loading branch information
Brian Cobb committed Apr 12, 2012
1 parent 35761eb commit 767e0a9
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 16 deletions.
35 changes: 35 additions & 0 deletions features/workers_are_supported_by_middleware.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Feature: Workers are supported by middleware

Gearup's DSL implements `use` to specify middleware used by a worker.

Scenario: Run a worker that uses a basic middleware
Given a file named "worker.rb" with:
"""
module Test
class FromYaml
def initialize(ability)
@ability = ability
end
def call(data, job)
@ability.call(YAML.load(data), job)
end
end
class Echo
def call(data, job)
return data
end
end
end
use Test::FromYaml
enable 'test.echo', Test::Echo.new
"""
When I successfully run `gearup -l ../../log/test.log -v worker.rb`
And I run the test.echo task with "--- :hello"
Then the task should complete with "hello"
15 changes: 12 additions & 3 deletions lib/gearup/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,26 @@ def self.build_from_file(file)

def initialize(worker_to_be_built)
@worker = worker_to_be_built
@middleware = []
end

def enable(ability_name, ability_to_perform)
worker.enable(ability_name, &gearup_ability_for(ability_to_perform))
worker.enable(ability_name, gearup_ability_for(ability_to_perform))
end

def use(middleware)
@middleware.unshift middleware
end

def using_middleware?(middleware)
@middleware.include? middleware
end

private

def gearup_ability_for(ability)
lambda do |data, job|
ability.call(data, job)
@middleware.reduce(ability) do |stack, middleware_class|
middleware_class.new(stack)
end
end

Expand Down
7 changes: 5 additions & 2 deletions lib/gearup/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ def work
@worker.work
end

def enable(ability_name, &ability)
def enable(ability_name, ability)
return if able_to_perform?(ability_name)

ability ||= Proc.new { }

@abilities << ability_name

@worker.add_ability(ability_name, &ability)
@worker.add_ability(ability_name) do |data, job|
ability.call(data, job)
end

@worker.after_ability(ability_name, &debug_after_ability(ability_name))
end

Expand Down
11 changes: 9 additions & 2 deletions spec/builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@
subject { Gearup::Builder.new(worker) }

it 'knows how to enable abilities on workers' do
ability, gearup_ability = stub, lambda { }
ability, gearup_ability = stub, stub

subject.stub(:gearup_ability_for).with(ability).and_return(gearup_ability)
subject.worker.should_receive(:enable).with('test', &gearup_ability)
subject.worker.should_receive(:enable).with('test', gearup_ability)

subject.enable('test', ability)
end

it 'can add middleware to the stack' do
middleware = stub

subject.use(middleware)
subject.should be_using_middleware(middleware)
end

end
18 changes: 9 additions & 9 deletions spec/worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,28 @@
end

it 'enables abilities on the gearman worker' do
ability = 'test'
gearman_worker.should_receive(:add_ability).with(ability)
ability_name = 'test'
gearman_worker.should_receive(:add_ability).with(ability_name)

subject.enable(ability)
subject.enable(ability_name, stub)
subject.should be_able_to_perform('test')
end

it 'can disable abilities on the gearman worker' do
ability = 'test'
ability_name = 'test'

gearman_worker.should_receive(:remove_ability).with(ability)
subject.enable(ability)
subject.disable(ability)
gearman_worker.should_receive(:remove_ability).with(ability_name)
subject.enable(ability_name, stub)
subject.disable(ability_name)

subject.should_not be_able_to_perform(ability)
subject.should_not be_able_to_perform(ability_name)
end

it 'disables all abilities and the worker on shutdown' do
subject.should_receive(:disable).with('test')
gearman_worker.should_receive(:worker_enabled=).with(false)

subject.enable('test')
subject.enable('test', stub)
subject.shutdown
end

Expand Down

1 comment on commit 767e0a9

@bcobb
Copy link
Owner

@bcobb bcobb commented on 767e0a9 Apr 12, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naturally, the comment in the commit message does not reflect the fact that I used unshift anyway in the implementation. Heh.

Please sign in to comment.