<commit_before>from client.python.asl.client import GearmanService, RawTask, JsonTask
from asl.application.service_application import service_application as app
from functools import wraps
from asl.utils.params_helper import required_params

__author__ = '<NAME>'

def background_task_method(task):
    """
    Decorate a function as a background task (called with help of gearman)

    :param task: name of the task
    :type task: str
    :return: decorated function
    """

    def decorator_fn(fn):
        if 'GEARMAN' not in app.config or 'host' not in app.config['GEARMAN'] or 'GEARMAN_TASK_NAME' not in app.config:
            raise Exception("Missing gearman settings (trying to use backgorund task)")

        gearman_host = (app.config['GEARMAN']['host'], app.config['GEARMAN']['port']) if app.config['GEARMAN']['port'] \
            else app.config['GEARMAN']['host']

        gearman = GearmanService({'HOST': [gearman_host], 'TASK_NAME': app.config['GEARMAN_TASK_NAME']})
        gearman.set_blocking(False)

        @wraps(fn)
        def background_task_decorator(*args, **kwargs):
            t = RawTask(task, dict(method=fn.__name__, args=args[1:], kwargs=kwargs))

            t_result = gearman.call(t, [JsonTask])

            return t_result.result

        background_task_decorator._background_fn = fn

        return background_task_decorator

    return decorator_fn<commit_msg>Background task decorator - docstring<commit_after>from client.python.asl.client import GearmanService, RawTask, JsonTask
from asl.application.service_application import service_application as app
from functools import wraps
from asl.utils.params_helper import required_params

__author__ = '<NAME>'


def background_task_method(task):
    """
    Decorate an object method as a background task (called with help of
    gearman)

    You have to create a task which will handle the gearman call. The
    method arguments will be encoded as JSON.

    :param task: name of the task
    :type task: str
    :return: decorated function
    """

    # TODO ako vysledok vrat nejaky JOB ID, aby sa dalo checkovat na pozadi

    # TODO vytvorit este vseobecny background_task nielen pre metody

    def decorator_fn(fn):
        if 'GEARMAN' not in app.config or 'host' not in app.config['GEARMAN'] or 'GEARMAN_TASK_NAME' not in app.config:
            raise Exception("Missing gearman settings (trying to use backgorund task)")

        gearman_host = (app.config['GEARMAN']['host'], app.config['GEARMAN']['port']) if app.config['GEARMAN']['port'] \
            else app.config['GEARMAN']['host']

        gearman = GearmanService({'HOST': [gearman_host], 'TASK_NAME': app.config['GEARMAN_TASK_NAME']})
        gearman.set_blocking(False)

        @wraps(fn)
        def background_task_decorator(*args, **kwargs):
            # prvy args je self
            t = RawTask(task, dict(method=fn.__name__, args=args[1:], kwargs=kwargs))

            t_result = gearman.call(t, [JsonTask])

            return t_result.result

        background_task_decorator._background_fn = fn

        return background_task_decorator

    return decorator_fn
{'fraction_non_alphanumeric': 0.10085905186127904, 'fraction_numerical': 0.0006363347120585428, 'mean_word_length': 3.485021398002853, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5974038', 'n_tokens_mistral': 960, 'n_tokens_neox': 929, 'n_words': 262}