Consolidate periodic tasks

Right now, periodic tasks are defined in two files:

* https://github.com/openstack/zun/blob/master/zun/service/periodic.py
* https://github.com/openstack/zun/blob/master/zun/compute/manager.py

I think it is better to consolidate all periodic tasks into one file.
This will be slightly better for developers to search. Let's
follow nova's convention to define all compute node's periodic
task into compute/manager.py

Change-Id: I6f8a54b290a7910248c776b42d486f8c9d362676
Closes-Bug: #1745078
This commit is contained in:
deepak_mourya 2018-02-16 15:31:21 +05:30 committed by Deepak Mourya
parent 15c4ba5011
commit 80bdbb1c4f
4 changed files with 32 additions and 73 deletions

View File

@ -25,7 +25,6 @@ from zun.common import rpc
from zun.compute import manager as compute_manager
import zun.conf
from zun.objects import base as objects_base
from zun.service import periodic
from zun.servicegroup import zun_service_periodic as servicegroup
osprofiler = importutils.try_import("osprofiler.profiler")
@ -62,7 +61,6 @@ class Service(service.Service):
def start(self):
servicegroup.setup(CONF, self.binary, self.tg)
periodic.setup(CONF, self.tg)
for endpoint in self.endpoints:
if isinstance(endpoint, compute_manager.Manager):
endpoint.init_containers(

View File

@ -13,6 +13,8 @@
# under the License.
import itertools
import functools
import six
import time
@ -22,6 +24,7 @@ from oslo_utils import excutils
from oslo_utils import uuidutils
from zun.common import consts
from zun.common import context
from zun.common import exception
from zun.common.i18n import _
from zun.common import utils
@ -39,6 +42,15 @@ CONF = zun.conf.CONF
LOG = logging.getLogger(__name__)
def set_context(func):
@functools.wraps(func)
def handler(self, ctx):
if ctx is None:
ctx = context.get_admin_context(all_projects=True)
func(self, ctx)
return handler
class Manager(periodic_task.PeriodicTasks):
"""Manages the running containers."""
@ -885,6 +897,24 @@ class Manager(periodic_task.PeriodicTasks):
except Exception:
return
@periodic_task.periodic_task(spacing=CONF.sync_container_state_interval,
run_immediately=True)
@set_context
def sync_container_state(self, ctx):
LOG.debug('Start syncing container states.')
containers = objects.Container.list(ctx)
self.driver.update_containers_states(ctx, containers)
capsules = objects.Capsule.list(ctx)
for capsule in capsules:
container = objects.Container.get_by_uuid(
ctx, capsule.containers_uuids[1])
if capsule.host != container.host:
capsule.host = container.host
capsule.save(ctx)
LOG.debug('Complete syncing container states.')
def capsule_create(self, context, capsule, requested_networks,
requested_volumes, limits):
@utils.synchronized("capsule-" + capsule.uuid)

View File

@ -1,69 +0,0 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import functools
from oslo_log import log
from oslo_service import periodic_task
from zun.common import context
import zun.conf
from zun.container import driver
from zun import objects
CONF = zun.conf.CONF
LOG = log.getLogger(__name__)
def set_context(func):
@functools.wraps(func)
def handler(self, ctx):
if ctx is None:
ctx = context.get_admin_context(all_projects=True)
func(self, ctx)
return handler
class ContainerStateSyncPeriodicJob(periodic_task.PeriodicTasks):
def __init__(self, conf):
self.host = conf.host
self.driver = driver.load_container_driver(
conf.container_driver)
super(ContainerStateSyncPeriodicJob, self).__init__(conf)
@periodic_task.periodic_task(spacing=CONF.sync_container_state_interval,
run_immediately=True)
@set_context
def sync_container_state(self, ctx):
LOG.debug('Start syncing container states.')
containers = objects.Container.list(ctx)
self.driver.update_containers_states(ctx, containers)
capsules = objects.Capsule.list(ctx)
for capsule in capsules:
container = objects.Container.get_by_uuid(
ctx, capsule.containers_uuids[1])
if capsule.host != container.host:
capsule.host = container.host
capsule.save(ctx)
LOG.debug('Complete syncing container states.')
def setup(conf, tg):
pt = ContainerStateSyncPeriodicJob(conf)
tg.add_dynamic_timer(
pt.run_periodic_tasks,
periodic_interval_max=conf.periodic_interval_max,
context=None)

View File

@ -15,8 +15,8 @@
from oslo_log import log
from oslo_service import periodic_task
from zun.compute import manager
from zun import objects
from zun.service import periodic
LOG = log.getLogger(__name__)
@ -35,7 +35,7 @@ class ZunServicePeriodicTasks(periodic_task.PeriodicTasks):
super(ZunServicePeriodicTasks, self).__init__(conf)
@periodic_task.periodic_task(run_immediately=True)
@periodic.set_context
@manager.set_context
def update_zun_service(self, ctx):
LOG.debug('Update zun_service')
if self.zun_service_ref is None: