Skip to content

Commit 54dd264

Browse files
authored
set finished and cleanup state properly for operations (#3214)
* set finished and cleanup state properly for operations * remove unnecessary lines * include operation ID in cleanup log msg * simplify condition * add unit tests for cleanup status * Update tox.ini * Update quality.yml
1 parent 0ff0bff commit 54dd264

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

app/objects/c_operation.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,7 @@ async def wait_for_links_completion(self, link_ids):
265265
break
266266

267267
async def is_closeable(self):
268-
if await self.is_finished() or self.auto_close:
269-
self.state = self.states['FINISHED']
270-
return True
271-
return False
268+
return await self.is_finished() or self.auto_close
272269

273270
async def is_finished(self):
274271
if self.state in [self.states['FINISHED'], self.states['OUT_OF_TIME'], self.states['CLEANUP']] \
@@ -427,7 +424,10 @@ async def _cleanup_operation(self, services):
427424
self.add_link(link)
428425
cleanup_count += 1
429426
if cleanup_count:
427+
self.state = self.states['CLEANUP']
428+
logging.debug(f'Starting cleanup for operation {self.id}')
430429
await self._safely_handle_cleanup(cleanup_count)
430+
logging.debug(f'Completed cleanup for operation {self.id}')
431431

432432
async def _safely_handle_cleanup(self, cleanup_link_count):
433433
try:

tests/objects/test_operation.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from app.objects.c_operation import Operation
1111
from app.objects.secondclass.c_link import Link
1212
from app.service.interfaces.i_event_svc import EventServiceInterface
13+
from app.service.interfaces.i_planning_svc import PlanningServiceInterface
1314
from app.utility.base_service import BaseService
1415
from app.objects.c_source import Source
1516
from app.objects.c_planner import Planner
@@ -139,16 +140,43 @@ async def fire_event(self, exchange=None, queue=None, timestamp=True, **callback
139140
BaseService.remove_service('event_svc')
140141

141142

143+
@pytest.fixture
144+
def fake_planning_svc(event_loop, make_test_link, test_agent):
145+
class FakePlanningService(BaseService, PlanningServiceInterface):
146+
def __init__(self):
147+
self.fired = {}
148+
149+
async def get_cleanup_links(self, operation, agent):
150+
cleanup_link = make_test_link(135, link_paw=test_agent.paw, link_cleanup=1, link_status=0)
151+
return [cleanup_link]
152+
153+
def get_links(self, operation, buckets, agent, trim):
154+
pass
155+
156+
def generate_and_trim_links(self, agent, operation, abilities, trim):
157+
pass
158+
159+
def sort_links(self, links):
160+
pass
161+
162+
service = FakePlanningService()
163+
service.add_service('planning_svc', service)
164+
165+
yield service
166+
167+
BaseService.remove_service('planning_svc')
168+
169+
142170
@pytest.fixture
143171
def test_ability(ability, executor):
144172
return ability(ability_id='123', executors=[executor(name='psh', platform='windows')])
145173

146174

147175
@pytest.fixture
148176
def make_test_link(test_ability):
149-
def _make_link(link_id, link_paw='123456', link_status=-3):
177+
def _make_link(link_id, link_paw='123456', link_status=-3, link_cleanup=0):
150178
return Link(command='', paw=link_paw, ability=test_ability, id=link_id, executor=next(test_ability.executors),
151-
status=link_status)
179+
status=link_status, cleanup=link_cleanup)
152180
return _make_link
153181

154182

@@ -597,3 +625,11 @@ async def test_add_ignored_link(self, make_test_link, operation_agent):
597625
assert op.ignored_links
598626
assert test_link.id in op.ignored_links
599627
assert len(op.ignored_links) == 1
628+
629+
async def test_operation_cleanup_status(self, fake_planning_svc, operation_agent):
630+
services = {'planning_svc': fake_planning_svc}
631+
op = Operation(name='test with cleanup', agents=[operation_agent], state='running')
632+
assert op.state == 'running'
633+
assert await op.is_closeable()
634+
await op._cleanup_operation(services)
635+
assert op.state == 'cleanup'

0 commit comments

Comments
 (0)