diff --git a/irods/models.py b/irods/models.py index 5b87bb34..8986d6a2 100644 --- a/irods/models.py +++ b/irods/models.py @@ -33,11 +33,7 @@ class RuleExec(Model): last_exe_time = Column(DateTime, "RULE_EXEC_LAST_EXE_TIME", 1010) frequency = Column(String, "RULE_EXEC_FREQUENCY", 1006) priority = Column(String, "RULE_EXEC_PRIORITY", 1007) - - -# # If needed in 4.2.9, we can update the Query class to dynamically -# # attach this field based on server version: -# context = Column(String, 'RULE_EXEC_CONTEXT', 1012) + context = Column(String, 'RULE_EXEC_CONTEXT', 1012, min_version=(4, 3, 0)) # # These are either unused or usually absent: # exec_status = Column(String,'RULE_EXEC_STATUS', 1011) diff --git a/irods/test/rule_test.py b/irods/test/rule_test.py index dccc07a6..a983c6ca 100644 --- a/irods/test/rule_test.py +++ b/irods/test/rule_test.py @@ -1,22 +1,25 @@ #! /usr/bin/env python # -*- coding: utf-8 -*- +import io +import json import os +import random import sys -import time import textwrap +import time import unittest -from irods.models import DataObject +from io import open as io_open + +from irods.column import Like from irods.exception import ( FAIL_ACTION_ENCOUNTERED_ERR, RULE_ENGINE_ERROR, UnknowniRODSError, ) -import irods.test.helpers as helpers +from irods.models import DataObject, RuleExec from irods.rule import Rule -from io import open as io_open -import io - +from irods.test import helpers RE_Plugins_installed_run_condition_args = ( os.environ.get("PYTHON_RULE_ENGINE_INSTALLED", "*").lower()[:1] == "y", @@ -436,6 +439,35 @@ def test_rulefile_in_file_like_object_2__336(self): self.assertRegex(lines[0], r"\[INTEGER\]\[5\]") self.assertRegex(lines[1], r"\[STRING\]\[A String\]") + def test_rule_exec_context_column_is_available_via_genquery1__issue_823(self): + rule_id = -1 + + try: + # Schedule a delayed rule. The amount of delay does not matter as only its existence + # in the catalog matters and we'll be cancelling it immediately after the test. + delay_seconds = 15 * 60 + + # Generate a unique number for use in querying the rule. + random_int = random.randint(1 << 30, (1 << 31) - 1) # noqa: S311 + r = Rule( + self.sess, + body=f'''delay("{delay_seconds}") {{ writeLine("serverLog","{random_int}") }}''', + ) + # Schedule the delayed rule. + r.execute() + + # Get the delayed rule's ID, needed for cancellation. + results = list(self.sess.query(RuleExec).filter(Like(RuleExec.name, f'%"{random_int}"%'))) + rule_id = results[0][RuleExec.id] + + # Assert context exists, and is JSON-parsable with a non-null-length result. + rule_context = results[0][RuleExec.context] + self.assertGreater(len(json.loads(rule_context)), 0) + finally: + # Remove the delayed rule from the queue. + if rule_id >= 0: + r.remove_by_id(rule_id) + if __name__ == "__main__": # let the tests find the parent irods lib