Add 'enabled' parameter to @periodic decorator

This change makes @periodic more compatible with periodic tasks from
oslo.service, and also enables the following pattern found in ironic:

 @periodic(enabled=CONF.periodic_task_foo_enabled)
 def foo(...)

Change-Id: I390197158522a59145420bd7988ffa74e6e5a391
This commit is contained in:
Dmitry Tantsur 2015-09-29 17:13:43 +02:00
parent 3d604eb332
commit b96691eb03
2 changed files with 6 additions and 6 deletions

View File

@ -103,7 +103,7 @@ def _check_attrs(obj):
return missing_attrs
def periodic(spacing, run_immediately=False):
def periodic(spacing, run_immediately=False, enabled=True):
"""Tags a method/function as wanting/able to execute periodically.
:param spacing: how often to run the decorated function (required)
@ -112,6 +112,8 @@ def periodic(spacing, run_immediately=False):
immediately or wait until the spacing provided has
elapsed before running for the first time
:type run_immediately: boolean
:param enabled: whether the task is enabled to run
:type enabled: boolean
"""
if spacing <= 0:
@ -119,7 +121,7 @@ def periodic(spacing, run_immediately=False):
" zero instead of %s" % spacing)
def wrapper(f):
f._is_periodic = True
f._is_periodic = enabled
f._periodic_spacing = spacing
f._periodic_run_immediately = run_immediately

View File

@ -216,14 +216,12 @@ class TestPeriodics(testscenarios.TestWithScenarios, base.TestCase):
self.sleep(0.1)
w.stop()
def test_not_added(self):
def test_disabled(self):
@periodics.periodic(0.5)
@periodics.periodic(0.5, enabled=False)
def no_add_me():
pass
no_add_me._is_periodic = False
@periodics.periodic(0.5)
def add_me():
pass