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..0ee7718f 100644 --- a/irods/test/rule_test.py +++ b/irods/test/rule_test.py @@ -3,10 +3,12 @@ import os import sys +import random import time import textwrap import unittest -from irods.models import DataObject +from irods.column import Like +from irods.models import DataObject, RuleExec from irods.exception import ( FAIL_ACTION_ENCOUNTERED_ERR, RULE_ENGINE_ERROR, @@ -437,6 +439,34 @@ def test_rulefile_in_file_like_object_2__336(self): self.assertRegex(lines[1], r"\[STRING\]\[A String\]") + def test_rule_exec_context(self): + if self.sess.server_version < (4, 3): + self.skipTest("""RuleExec's "context" attribute not available before 4.3.0""") + + rule_id = -1 + + try: + # Schedule a delayed rule. + n_minutes = 15 + random_int = random.randint(1<<30, (1<<31)-1) + r = Rule( + self.sess, + body=f'''delay("{n_minutes}m") {{writeLine("serverLog","{random_int}")}}''' + ) + r.execute() + + # Get the delayed rule's ID, needed for cancellation. + l = list(self.sess.query(RuleExec).filter(Like(RuleExec.name,f'%"{random_int}"%'))) + rule_id = l[0][RuleExec.id] + + # Assert context exists, is of string type, and is not empty. + self.assertGreater(l[0][RuleExec.context], "") + 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 sys.path.insert(0, os.path.abspath("../.."))