Coverage for nova/conductor/tasks/base.py: 92%

24 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-04-24 11:16 +0000

1# Licensed under the Apache License, Version 2.0 (the "License"); you may 

2# not use this file except in compliance with the License. You may obtain 

3# a copy of the License at 

4# 

5# http://www.apache.org/licenses/LICENSE-2.0 

6# 

7# Unless required by applicable law or agreed to in writing, software 

8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 

9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 

10# License for the specific language governing permissions and limitations 

11# under the License. 

12 

13import abc 

14import functools 

15 

16from oslo_utils import excutils 

17 

18 

19def rollback_wrapper(original): 

20 @functools.wraps(original) 

21 def wrap(self): 

22 try: 

23 return original(self) 

24 except Exception as ex: 

25 with excutils.save_and_reraise_exception(): 

26 self.rollback(ex) 

27 return wrap 

28 

29 

30class TaskBase(metaclass=abc.ABCMeta): 

31 

32 def __init__(self, context, instance): 

33 self.context = context 

34 self.instance = instance 

35 

36 @rollback_wrapper 

37 def execute(self): 

38 """Run task's logic, written in _execute() method 

39 """ 

40 return self._execute() 

41 

42 @abc.abstractmethod 

43 def _execute(self): 

44 """Descendants should place task's logic here, while resource 

45 initialization should be performed over __init__ 

46 """ 

47 pass 

48 

49 def rollback(self, ex): 

50 """Rollback failed task 

51 Descendants should implement this method to allow task user to 

52 rollback status to state before execute method was call 

53 """ 

54 pass