nova/nova/conductor/tasks/base.py
Timofey Durakov 83787211b3 Create class hierarchy for tasks in conductor
Conductor has 2 approaches to hande rpc api calls:
- in place (code located in manager itself)
- in tasks (for each operation creates new task class
where all logic is located)
Second approach allows to implement single responsibility
to each method.

In this patch, a class hierarchy for tasks is introduced.
It allows share same public interface between tasks.
Cold-migration/resize logic is also  moved to its
new task class. This change simplifies unit tests,
by making them cleaner.

Closes-Bug: #1468696

Change-Id: I820c4a8ccc58e140f24597a52f2e01444cd6d134
2015-07-30 17:35:56 +03:00

57 lines
1.6 KiB
Python

# 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 abc
import functools
from oslo_utils import excutils
import six
def rollback_wrapper(original):
@functools.wraps(original)
def wrap(self):
try:
return original(self)
except Exception:
with excutils.save_and_reraise_exception():
self.rollback()
return wrap
@six.add_metaclass(abc.ABCMeta)
class TaskBase(object):
def __init__(self, context, instance):
self.context = context
self.instance = instance
@rollback_wrapper
def execute(self):
"""Run task's logic, written in _execute() method
"""
return self._execute()
@abc.abstractmethod
def _execute(self):
"""Descendants should place task's logic here, while resource
initialization should be performed over __init__
"""
pass
def rollback(self):
"""Rollback failed task
Descendants should implement this method to allow task user to
rollback status to state before execute method was call
"""
pass