This is an automated email from the ASF dual-hosted git repository.

dill0wn pushed a commit to branch dw/taskcmd_filter_age
in repository https://gitbox.apache.org/repos/asf/allura.git

commit bb49367870ca2530d7a39c00ec6c989536d4afbb
Author: Dillon Walls <[email protected]>
AuthorDate: Mon May 17 22:00:18 2021 +0000

    add filter by age of task to TaskCommands
---
 Allura/allura/command/taskd.py       |  4 ++++
 Allura/allura/tests/test_commands.py | 44 ++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+)

diff --git a/Allura/allura/command/taskd.py b/Allura/allura/command/taskd.py
index 3a79cd5..4d7d624 100644
--- a/Allura/allura/command/taskd.py
+++ b/Allura/allura/command/taskd.py
@@ -177,6 +177,8 @@ class TaskCommand(base.Command):
                       help='limit to task names starting with this.  Example 
allura.tasks.index_tasks.')
     parser.add_option('--filter-result-regex', dest='filter_result_regex', 
default=None,
                       help='limit to tasks with result matching this regex.  
Example "pysolr"')
+    parser.add_option('--filter-queued-days-ago', dest='days_ago', type=int, 
default=None,
+                      help='limit to tasks queued NUM days ago.  Example 
"180"')
     min_args = 2
     max_args = None
     usage = '''<ini file> [list|retry|purge|timeout|commit]
@@ -206,6 +208,8 @@ class TaskCommand(base.Command):
             q['task_name'] = {'$regex': 
r'^{}.*'.format(re.escape(self.options.filter_name_prefix))}
         if self.options.filter_result_regex:
             q['result'] = {'$regex': self.options.filter_result_regex}
+        if self.options.days_ago:
+            q['time_queue'] = {'$lt': datetime.utcnow() - 
timedelta(days=self.options.days_ago)}
         if self.verbose:
             print(q)
         return q
diff --git a/Allura/allura/tests/test_commands.py 
b/Allura/allura/tests/test_commands.py
index ccac09c..784116a 100644
--- a/Allura/allura/tests/test_commands.py
+++ b/Allura/allura/tests/test_commands.py
@@ -18,6 +18,8 @@
 from __future__ import unicode_literals
 from __future__ import absolute_import
 
+import datetime
+
 import six
 from alluratest.tools import assert_raises, assert_in
 from testfixtures import OutputCapture
@@ -271,6 +273,9 @@ class TestEnsureIndexCommand(object):
 
 class TestTaskCommand(object):
 
+    def tearDown(self):
+        M.MonQTask.query.remove({})
+
     def test_commit(self):
         exit_code = taskd.TaskCommand('task').run([test_config, 'commit'])
         assert_equal(M.MonQTask.query.find({'task_name': 
'allura.tasks.index_tasks.commit'}).count(), 1)
@@ -281,6 +286,7 @@ class TestTaskCommand(object):
         assert_equal(exit_code, 0)
 
     def test_retry(self):
+        # self.test_commit()
         exit_code = taskd.TaskCommand('task').run([
             test_config, 'retry',
             '--filter-name-prefix', 'allura.tasks.index_tasks.',
@@ -288,6 +294,44 @@ class TestTaskCommand(object):
         ])
         assert_equal(exit_code, 0)
 
+    def test_purge(self):
+        # create task
+        self.test_commit()
+        assert_equal(M.MonQTask.query.find().count(), 1)
+        M.MonQTask.query.update({'task_name': 
'allura.tasks.index_tasks.commit'}, {'$set': {'state': 'complete'}})
+        # run purge; verify 0 records
+        exit_code = taskd.TaskCommand('task').run([
+            test_config, 'purge',
+        ])
+        assert_equal(exit_code, 0)
+        assert_equal(M.MonQTask.query.find().count(), 0)
+
+    def test_purge_old_only(self):
+        # create task
+        self.test_commit()
+        assert_equal(M.MonQTask.query.find().count(), 1)
+
+        # force task to be in complete state
+        M.MonQTask.query.update({'task_name': 
'allura.tasks.index_tasks.commit'}, {'$set': {'state': 'complete'}})
+        # run purge; verify no records deleted
+        exit_code = taskd.TaskCommand('task').run([
+            test_config, 'purge', '--filter-queued-days-ago', '180',
+        ])
+        assert_equal(exit_code, 0)
+        assert_equal(M.MonQTask.query.find().count(), 1)
+
+        # modify task to be old
+        then = datetime.datetime.utcnow() - datetime.timedelta(days=200)
+        M.MonQTask.query.update({'task_name': 
'allura.tasks.index_tasks.commit'},
+                                {'$set': {'time_queue': then, 'time_start': 
then, 'time_stop': then}})
+
+        # run purge; verify old tasks deleted
+        exit_code = taskd.TaskCommand('task').run([
+            test_config, 'purge', '--filter-queued-days-ago', '180',
+        ])
+        assert_equal(exit_code, 0)
+        assert_equal(M.MonQTask.query.find().count(), 0)
+
 
 class TestTaskdCleanupCommand(object):
 

Reply via email to