Merge "PeriodicWorker.create to accept arguments for periodic tasks"

This commit is contained in:
Jenkins 2016-01-16 20:23:00 +00:00 committed by Gerrit Code Review
commit b11b773ddf
2 changed files with 27 additions and 5 deletions

View File

@ -368,7 +368,7 @@ class PeriodicWorker(object):
log=None, executor_factory=None,
cond_cls=threading.Condition, event_cls=threading.Event,
schedule_strategy='last_started', now_func=utils.now,
on_failure=None):
on_failure=None, args=_NO_OP_ARGS, kwargs=_NO_OP_KWARGS):
"""Automatically creates a worker by analyzing object(s) methods.
Only picks up methods that have been tagged/decorated with
@ -425,6 +425,10 @@ class PeriodicWorker(object):
any user provided callable should not raise
exceptions on being called
:type on_failure: callable
:param args: positional arguments to be passed to all callables
:type args: tuple
:param kwargs: keyword arguments to be passed to all callables
:type kwargs: dict
"""
callables = []
for obj in objects:
@ -434,10 +438,7 @@ class PeriodicWorker(object):
if six.callable(member):
missing_attrs = _check_attrs(member)
if not missing_attrs:
# These do not support custom args, kwargs...
callables.append((member,
cls._NO_OP_ARGS,
cls._NO_OP_KWARGS))
callables.append((member, args, kwargs))
return cls(callables, log=log, executor_factory=executor_factory,
cond_cls=cond_cls, event_cls=event_cls,
schedule_strategy=schedule_strategy, now_func=now_func,

View File

@ -19,6 +19,7 @@ import time
import eventlet
from eventlet.green import threading as green_threading
import mock
import testscenarios
import futurist
@ -298,6 +299,26 @@ class TestPeriodics(testscenarios.TestWithScenarios, base.TestCase):
am_called = sum(called)
self.assertGreaterEqual(am_called, 4)
def test_create_with_arguments(self):
m = mock.Mock()
class Object(object):
@periodics.periodic(0.5)
def func1(self, *args, **kwargs):
m(*args, **kwargs)
executor_factory = lambda: self.executor_cls(**self.executor_kwargs)
w = periodics.PeriodicWorker.create(objects=[Object()],
executor_factory=executor_factory,
args=('foo',),
kwargs={'bar': 'baz'},
**self.worker_kwargs)
with self.create_destroy(w.start):
self.sleep(2.0)
w.stop()
m.assert_called_with('foo', bar='baz')
class RejectingExecutor(futurist.GreenThreadPoolExecutor):
MAX_REJECTIONS_COUNT = 2