From 80bdbb1c4f9033dee26586fa6fb42322e52b6ec7 Mon Sep 17 00:00:00 2001 From: deepak_mourya Date: Fri, 16 Feb 2018 15:31:21 +0530 Subject: [PATCH] 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 --- zun/common/rpc_service.py | 2 - zun/compute/manager.py | 30 +++++++++++ zun/service/periodic.py | 69 ------------------------ zun/servicegroup/zun_service_periodic.py | 4 +- 4 files changed, 32 insertions(+), 73 deletions(-) delete mode 100644 zun/service/periodic.py diff --git a/zun/common/rpc_service.py b/zun/common/rpc_service.py index 157ffb49d..4f78d917b 100644 --- a/zun/common/rpc_service.py +++ b/zun/common/rpc_service.py @@ -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( diff --git a/zun/compute/manager.py b/zun/compute/manager.py index 4dfe53419..6810235a6 100644 --- a/zun/compute/manager.py +++ b/zun/compute/manager.py @@ -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) diff --git a/zun/service/periodic.py b/zun/service/periodic.py deleted file mode 100644 index 0cbd67168..000000000 --- a/zun/service/periodic.py +++ /dev/null @@ -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) diff --git a/zun/servicegroup/zun_service_periodic.py b/zun/servicegroup/zun_service_periodic.py index 93b378b56..ea3fd4235 100644 --- a/zun/servicegroup/zun_service_periodic.py +++ b/zun/servicegroup/zun_service_periodic.py @@ -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: