Skip to content

Commit

Permalink
ensure that workflow_state disconnects from DB
Browse files Browse the repository at this point in the history
  • Loading branch information
wxtim committed May 1, 2024
1 parent db393ec commit d947f55
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 20 deletions.
8 changes: 8 additions & 0 deletions cylc/flow/dbstatecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,11 @@ def task_state_met(
return bool(
self.workflow_state_query(task, cycle, status, output, flow_num)
)


class CylcWorkflowDBCheckerContext(CylcWorkflowDBChecker):
def __enter__(self):
return self

def __exit__(self, *args):
self.conn.close()
5 changes: 5 additions & 0 deletions cylc/flow/scripts/workflow_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ async def check(self):
flow_num=self.args['flow_num']
)

def disconnect(self):
self.checker.conn.close()


def get_option_parser() -> COP:
parser = COP(
Expand Down Expand Up @@ -285,3 +288,5 @@ def main(parser: COP, options: 'Values', workflow_id: str) -> None:
output=options.output,
flow_num=options.flow_num
))

spoller.disconnect()
37 changes: 18 additions & 19 deletions cylc/flow/xtriggers/workflow_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from metomi.isodatetime.parsers import TimePointParser

from cylc.flow.cycling.util import add_offset
from cylc.flow.dbstatecheck import CylcWorkflowDBChecker
from cylc.flow.dbstatecheck import CylcWorkflowDBCheckerContext
from cylc.flow.pathutil import get_cylc_run_dir
from cylc.flow.workflow_files import infer_latest_run_from_id
from cylc.flow.exceptions import WorkflowConfigError
Expand Down Expand Up @@ -88,27 +88,26 @@ def workflow_state(
# It could mean the target workflow has not started yet,
# but it could also mean a typo in the workflow ID, so
# so don't hide the error.
checker = CylcWorkflowDBChecker(cylc_run_dir, workflow)

# Point validity can only be checked at run time.
# Bad function arg templating can cause a syntax error.
if checker.point_fmt is None:
# Integer cycling: raises ValueError if bad.
int(point)
else:
# Datetime cycling: raises ISO8601SyntaxError if bad
point = str(
TimePointParser().parse(
point, dump_format=checker.point_fmt
with CylcWorkflowDBCheckerContext(cylc_run_dir, workflow) as checker:
# Point validity can only be checked at run time.
# Bad function arg templating can cause a syntax error.
if checker.point_fmt is None:
# Integer cycling: raises ValueError if bad.
int(point)
else:
# Datetime cycling: raises ISO8601SyntaxError if bad
point = str(
TimePointParser().parse(
point, dump_format=checker.point_fmt
)
)
)

if not output and not status:
status = "succeeded"
if not output and not status:
status = "succeeded"

satisfied: bool = checker.task_state_met(
task, point, output=output, status=status
)
satisfied: bool = checker.task_state_met(
task, point, output=output, status=status
)

results = {
'workflow': workflow,
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_dbstatecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from textwrap import dedent
from typing import TYPE_CHECKING

from cylc.flow.dbstatecheck import CylcWorkflowDBChecker as Checker
from cylc.flow.dbstatecheck import CylcWorkflowDBCheckerContext as Checker


if TYPE_CHECKING:
Expand Down

0 comments on commit d947f55

Please sign in to comment.