#3959 Move code re-used into an utils module
Merged 6 years ago by pingou. Opened 6 years ago by pingou.

file modified
+2 -2
@@ -30,8 +30,8 @@ 

  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 @@ 

      )

      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")

  

  

file modified
+1 -38
@@ -12,7 +12,6 @@ 

  

  import collections

  import datetime

- import gc

  import hashlib

  import os

  import os.path
@@ -20,8 +19,6 @@ 

  import subprocess

  import time

  

- from functools import wraps

- 

  import arrow

  import pygit2

  import six
@@ -38,6 +35,7 @@ 

  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 @@ 

      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 @@ 

      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(

file modified
+1 -1
@@ -27,7 +27,7 @@ 

  

  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})

file modified
+1 -1
@@ -30,7 +30,7 @@ 

  

  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

@@ -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()

file modified
+1 -1
@@ -1667,7 +1667,7 @@ 

  

      repo = flask.g.repo

  

-     if pagure_config.get('DISABLE_REMOTE_PR', True):

+     if pagure_config.get("DISABLE_REMOTE_PR", True):

          flask.abort(404, "Remote pull-requests disabled on this server")

  

      if not repo.settings.get("pull_requests", True):