Skip to content

Commit

Permalink
fix apply
Browse files Browse the repository at this point in the history
  • Loading branch information
cenkalti committed Jun 6, 2013
1 parent 3b5ace0 commit 9ed1137
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
14 changes: 10 additions & 4 deletions kuyruk/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ def __call__(self, *args, **kwargs):
task_result = self.apply(*args, **kwargs)
else:
task_result = TaskResult(self)
task_result.id = self.send_to_queue(args, kwargs,
host=host, local=local)
task_result.id = self.send_to_queue(
args, kwargs, host=host, local=local)

self.send_signal(events.task_postsend, args, kwargs)

Expand Down Expand Up @@ -225,16 +225,22 @@ def __init__(self, task, obj):
self.obj = obj

def __getattr__(self, item):
"""Delegates all attributes to real Task."""
return getattr(self.task, item)

def __call__(self, *args, **kwargs):
# Insert the bound object as a first argument to __call__
args = list(args)
args.insert(0, self.obj)
return super(BoundTask, self).__call__(*args, **kwargs)

def apply(self, *args, **kwargs):
args = list(args)
args.insert(0, self.obj)
# apply() may be called directly. Insert the bound object only if
# it is not inserted by __call__()
if args and args[0] is not self.obj:
args = list(args)
args.insert(0, self.obj)

return super(BoundTask, self).apply(*args, **kwargs)


Expand Down
5 changes: 5 additions & 0 deletions kuyruk/test/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ def meow(self, message):
print "Felix says:", message
must_be_called()

@kuyruk.task(eager=True)
def meow_eager(self, message):
print "Felix says:", message
must_be_called()

@kuyruk.task
def raise_exception(self):
raise Exception
Expand Down
6 changes: 6 additions & 0 deletions kuyruk/test/test_kuyruk.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ def test_class_task(self):

@patch('kuyruk.test.tasks.must_be_called')
def test_class_task_eager(self, mock_func):
cat = tasks.Cat(1, 'Felix')
cat.meow_eager('Oh my god')
mock_func.assert_called_once_with()

@patch('kuyruk.test.tasks.must_be_called')
def test_class_task_apply(self, mock_func):
cat = tasks.Cat(1, 'Felix')
cat.meow.apply('Oh my god')
mock_func.assert_called_once_with()
Expand Down

0 comments on commit 9ed1137

Please sign in to comment.