Debian Codegraph @

cylc/flow/main_loop/health_check.py

raw
1 # THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
2 # Copyright (C) NIWA & British Crown (Met Office) & Contributors.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 """Checks the integrity of the workflow run directory.
17
18 * Ensures workflow run directory is still present.
19 * Ensures contact file is present and consistent with the running workflow.
20
21 Shuts down the workflow in the event of inconsistency or error.
22
23 """
24 import os
25
26 from cylc.flow import workflow_files
27 from cylc.flow.exceptions import CylcError, ServiceFileError
28 from cylc.flow.main_loop import periodic
29
30
31 @periodic
32 async def health_check(scheduler, _):
33 """Perform workflow health checks."""
34 # 1. check if workflow run dir still present - if not shutdown.
35 _check_workflow_run_dir(scheduler)
36 # 2. check if contact file consistent with current start - if not
37 # shutdown.
38 _check_contact_file(scheduler)
39
40
41 def _check_workflow_run_dir(scheduler):
42 if not os.path.exists(scheduler.workflow_run_dir):
43 raise CylcError(
44 'Workflow run directory does not exist:'
45 f' {scheduler.workflow_run_dir}'
46 )
47
48
49 def _check_contact_file(scheduler):
50 try:
51 contact_data = workflow_files.load_contact_file(
52 scheduler.workflow)
53 if contact_data != scheduler.contact_data:
54 raise CylcError('contact file modified')
55 except (AssertionError, IOError, ValueError, ServiceFileError) as exc:
56 raise CylcError(
57 '%s: contact file corrupted/modified and may be left'
58 % workflow_files.get_contact_file_path(scheduler.workflow)
59 ) from exc