Skip to content

Commit

Permalink
Merge 734f751 into c09218d
Browse files Browse the repository at this point in the history
  • Loading branch information
miki725 committed Jan 15, 2016
2 parents c09218d + 734f751 commit ceddb60
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 3 deletions.
10 changes: 10 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@ History
Master (not yet on PyPI)
~~~~~~~~~~~~~~~~~~~~~~~~

In Progress

0.2 (2016-01-14)
~~~~~~~~~~~~~~~~

* **New**: Added ``EnsuredRedisTask`` which allows to ensure tasks are scheduled
via an abstract base task class in task definition rather then explicitly using
``ensure_redis_call`` while calling the task::

@app.task(base=EnsuredRedisTask)
def foo(): pass

0.1 (2016-01-13)
~~~~~~~~~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ License

The MIT License (MIT)

Copyright © 2015, dealertrack technologies
Copyright © 2015-2016, dealertrack technologies

::

Expand Down
10 changes: 10 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,13 @@ tasks which will block the scheduling until new master will be elected::
add.delay(1, 2)
# this will keep retrying until it succeeds
ensure_redis_call(add.delay, 1, 2)

Alternatively you can use a supplied abstract celery task subclass which provides
same retrying behavior in the task definition itself::

# tasks.py
from celery_redis_sentinel.tasks import EnsuredRedisTask

@app.task(base=EnsuredRedisTask)
def add(a, b):
return a + b
2 changes: 1 addition & 1 deletion celery_redis_sentinel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


__author__ = 'Miroslav Shubernetskiy'
__version__ = '0.1.0'
__version__ = '0.2.0'

try:
from .backend import RedisSentinelBackend # noqa
Expand Down
31 changes: 31 additions & 0 deletions celery_redis_sentinel/task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals

from celery import Task

from .redis_sentinel import ensure_redis_call


class EnsuredRedisTask(Task):
"""
Abstract celery task subclass which provides same functionality as
:py:class:`ensure_redis_call <celery_redis_sentinel.redis_sentinel.ensure_redis_call>`
except it is added at task definition time instead of during task schedule call.
This task subclass can be provided during task definition
by using ``base`` parameter.
Examples
--------
::
@app.task(base=EnsuredRedisTask)
def add(a, b):
return a + b
"""
abstract = True

def apply_async(self, *args, **kwargs):
_super = super(EnsuredRedisTask, self).apply_async
return ensure_redis_call(_super, *args, **kwargs)
1 change: 1 addition & 0 deletions docs/api/celery_redis_sentinel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ Submodules
celery_redis_sentinel.backend
celery_redis_sentinel.redis_sentinel
celery_redis_sentinel.register
celery_redis_sentinel.task
celery_redis_sentinel.transport

7 changes: 7 additions & 0 deletions docs/api/celery_redis_sentinel.task.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
celery_redis_sentinel.task module
=================================

.. automodule:: celery_redis_sentinel.task
:members:
:undoc-members:
:show-inheritance:
27 changes: 26 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals
import os
from itertools import chain

from setuptools import find_packages, setup

Expand All @@ -13,6 +14,25 @@ def read(fname):
return fid.read().decode('utf-8')


def remove_section_from_rst(text, section):
lines = text.splitlines()

section_line = lines.index(section)

separator = lines[section_line + 1]
assert set(separator) == {separator[0]}

next_section_line = next(
i for i, l in enumerate(lines[section_line + 2:])
if set(l) == {separator[0]}
)

return '\n'.join(chain(
lines[:section_line - 1],
lines[section_line + next_section_line:]
))


authors = read('AUTHORS.rst')
history = read('HISTORY.rst').replace('.. :changelog:', '')
licence = read('LICENSE.rst')
Expand All @@ -27,12 +47,17 @@ def read(fname):
read('requirements-dev.txt').splitlines()[1:]
)

long_description = remove_section_from_rst(
'\n\n'.join([readme, history, authors, licence]),
'Master (not yet on PyPI)'
)

setup(
name='celery-redis-sentinel',
version=__version__,
author=__author__,
description='Celery broker and results backend implementation for Redis Sentinel',
long_description='\n\n'.join([readme, history, authors, licence]),
long_description=long_description,
url='https://github.com/dealertrack/celery-redis-sentinel',
license='MIT',
packages=find_packages(exclude=['tests', 'tests.*', 'test_tasks', 'test_tasks.*']),
Expand Down
16 changes: 16 additions & 0 deletions tests/test_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals

import mock

from celery_redis_sentinel.task import EnsuredRedisTask


@mock.patch('celery_redis_sentinel.task.ensure_redis_call')
def test_apply_async(mock_ensure_redis_call):
task = EnsuredRedisTask()

actual = task.apply_async('foo', happy='rainbows')

assert actual == mock_ensure_redis_call.return_value
mock_ensure_redis_call.assert_called_once_with(mock.ANY, 'foo', happy='rainbows')

0 comments on commit ceddb60

Please sign in to comment.