Browse Source

Move code re-used into an utils module

This drops at least one of the circular imports we have.

Signed-off-by: Pierre-Yves Chibon <pingou@pingoured.fr>
Pierre-Yves Chibon 5 years ago
parent
commit
1aac432061
5 changed files with 57 additions and 42 deletions
  1. 2 2
      pagure/cli/admin.py
  2. 1 38
      pagure/lib/tasks.py
  3. 1 1
      pagure/lib/tasks_mirror.py
  4. 1 1
      pagure/lib/tasks_services.py
  5. 52 0
      pagure/lib/tasks_utils.py

+ 2 - 2
pagure/cli/admin.py

@@ -30,8 +30,8 @@ if "PAGURE_CONFIG" not in os.environ and os.path.exists(
 import pagure.config  # noqa: E402
 import pagure.exceptions  # noqa: E402
 import pagure.lib.git  # noqa: E402
-import pagure.lib.tasks  # noqa: E402
 import pagure.lib.query  # noqa: E402
+import pagure.lib.tasks_utils  # noqa: E402
 from pagure.flask_app import generate_user_key_files  # noqa: E402
 
 
@@ -524,7 +524,7 @@ def do_generate_acl(args):
     )
     if _ask_confirmation():
         helper.generate_acls(project=project, group=group_obj)
-        pagure.lib.tasks.gc_clean()
+        pagure.lib.tasks_utils.gc_clean()
         print("Gitolite ACLs updated")
 
 

+ 1 - 38
pagure/lib/tasks.py

@@ -12,7 +12,6 @@ from __future__ import unicode_literals
 
 import collections
 import datetime
-import gc
 import hashlib
 import os
 import os.path
@@ -20,8 +19,6 @@ import shutil
 import subprocess
 import time
 
-from functools import wraps
-
 import arrow
 import pygit2
 import six
@@ -38,6 +35,7 @@ import pagure.lib.link
 import pagure.lib.query
 import pagure.lib.repo
 import pagure.utils
+from pagure.lib.tasks_utils import pagure_task
 from pagure.config import config as pagure_config
 from pagure.utils import get_parent_repo_path
 
@@ -61,35 +59,6 @@ def augment_celery_log(**kwargs):
     pagure.utils.set_up_logging(force=True)
 
 
-def pagure_task(function):
-    """ Simple decorator that is responsible for:
-    * Adjusting the status of the task when it starts
-    * Creating and cleaning up a SQLAlchemy session
-    """
-
-    @wraps(function)
-    def decorated_function(self, *args, **kwargs):
-        """ Decorated function, actually does the work. """
-        if self is not None:
-            try:
-                self.update_state(state="RUNNING")
-            except TypeError:
-                pass
-        session = pagure.lib.query.create_session(pagure_config["DB_URL"])
-        try:
-            return function(self, session, *args, **kwargs)
-        except:  # noqa: E722
-            # if the task has raised for any reason, we need to rollback the
-            # session first to not leave open uncomitted transaction hanging
-            session.rollback()
-            raise
-        finally:
-            session.remove()
-            gc_clean()
-
-    return decorated_function
-
-
 def get_result(uuid):
     """ Returns the AsyncResult object for a given task.
 
@@ -107,12 +76,6 @@ def ret(endpoint, **kwargs):
     return toret
 
 
-def gc_clean():
-    """ Force a run of the garbage collector. """
-    # https://pagure.io/pagure/issue/2302
-    gc.collect()
-
-
 @conn.task(queue=pagure_config.get("GITOLITE_CELERY_QUEUE", None), bind=True)
 @pagure_task
 def generate_gitolite_acls(

+ 1 - 1
pagure/lib/tasks_mirror.py

@@ -27,7 +27,7 @@ from cryptography.hazmat.primitives import serialization
 
 import pagure.lib.query
 from pagure.config import config as pagure_config
-from pagure.lib.tasks import pagure_task
+from pagure.lib.tasks_utils import pagure_task
 from pagure.utils import ssh_urlpattern
 
 # logging.config.dictConfig(pagure_config.get('LOGGING') or {'version': 1})

+ 1 - 1
pagure/lib/tasks_services.py

@@ -30,7 +30,7 @@ from sqlalchemy.exc import SQLAlchemyError
 
 import pagure.lib.query
 from pagure.config import config as pagure_config
-from pagure.lib.tasks import pagure_task
+from pagure.lib.tasks_utils import pagure_task
 from pagure.mail_logging import format_callstack
 from pagure.lib.lib_ci import trigger_jenkins_build
 from pagure.utils import split_project_fullname, set_up_logging

+ 52 - 0
pagure/lib/tasks_utils.py

@@ -0,0 +1,52 @@
+# -*- coding: utf-8 -*-
+
+"""
+ (c) 2018 - Copyright Red Hat Inc
+
+ Authors:
+   Pierre-Yves Chibon <pingou@pingoured.fr>
+
+"""
+
+from __future__ import unicode_literals
+
+import gc
+from functools import wraps
+
+import pagure.lib.query
+from pagure.config import config as pagure_config
+
+
+def pagure_task(function):
+    """ Simple decorator that is responsible for:
+    * Adjusting the status of the task when it starts
+    * Creating and cleaning up a SQLAlchemy session
+    """
+
+    @wraps(function)
+    def decorated_function(self, *args, **kwargs):
+        """ Decorated function, actually does the work. """
+        if self is not None:
+            try:
+                self.update_state(state="RUNNING")
+            except TypeError:
+                pass
+        session = pagure.lib.query.create_session(pagure_config["DB_URL"])
+        try:
+            return function(self, session, *args, **kwargs)
+        except:  # noqa: E722
+            # if the task has raised for any reason, we need to rollback the
+            # session first to not leave open uncomitted transaction hanging
+            session.rollback()
+            raise
+        finally:
+            session.remove()
+            gc_clean()
+
+    return decorated_function
+
+
+def gc_clean():
+    """ Force a run of the garbage collector. """
+    # https://pagure.io/pagure/issue/2302
+    gc.collect()