<commit_before>from threading import Thread
from celery import Celery

from . import celeryconfig


__all__ = ('TaskApi', )


def watch_task(task, callback, kwargs=None):
    """
    watch task until it ends and then execute callback:

        callback(response, **kwargs)

    where response is a result of task

    :param task: task to watch
    :param callback: function which is called when task finishes
    :param kwargs: dict which is passed to callback

    :return: None
    """
    response = task.wait()
    if kwargs:
        callback(response, **kwargs)
    else:
        callback(response)


class TaskApi(object):
    """ universal API for tasks which are executed on celery workers """

    def __init__(self):
        self.client = Celery('image_build')

        # config_from_object('celeryconfig') won't work because importlib
        self.client.config_from_object(celeryconfig)

    def build_docker_image(self, build_image, git_url, local_tag, git_dockerfile_path=None, git_commit=None,
                           source_registry=None, target_registry=None, tag=None, repos=None,
                           callback=None, kwargs=None):
        """
        build docker image from supplied git repo

        TODO:
        DockerFile has to be in root of the gitrepo, path and commit are not implemented yet
        yum repos

        :param build_image: name of the build image (supplied docker image is built inside this image)
        :param git_url: url to git repo
        :param local_tag: image is known within the service with this tag
        :param git_dockerfile_path: path to dockerfile within git repo (default is ./Dockerfile)
        :param git_commit: which commit to checkout (master by default)
        :param source_registry: pull base image from this registry
        :param target_registry: push built image to this registry
        :param tag: tag image with this tag (and push it to target_repo if specified)
        :param repos: list of yum repos to enable in image
        :param callback: function to call when task finishes, it has to accept at least
                        one argument: return value of task
        :param kwargs: dict which is pass to callback, callback is called like this:
                         callback(task_response, **kwargs)
        :return: task_id
        """
        args = [build_image, git_url, local_tag]
        task_kwargs = {'source_registry': source_registry,
                       'target_registry': target_registry,
                       'tag': tag,
                       'git_commit': git_commit,
                       'git_dockerfile_path': git_dockerfile_path,
                       'repos': repos}
        task_info = self.client.send_task('docker_tasks.build_image', args=args, kwargs=task_kwargs)
        task_id = task_info.task_id
        if callback:
            t = Thread(target=watch_task, args=(task_info, callback, kwargs))
            #w.daemon = True
            t.start()
        return task_id

    def find_dockerfiles_in_git(self):
        raise NotImplemented()

    def push_docker_image(self, image_name, source_registry, target_registry, tags, callback, kwargs):
        """
        pull docker image from source registry, tag it with multiple tags and push it to tagrget registry

        :param image_name: image to pull
        :param source_registry: registry to pull from
        :param target_registry: registry for pushing
        :param tags: list of tags for image tagging
        :param callback: function to call when task finishes, it has to accept at least
                        one argument: return value of task<commit_msg>fix task name (add dbs_worker)<commit_after>from threading import Thread
from celery import Celery

from . import celeryconfig


__all__ = ('TaskApi', )


def watch_task(task, callback, kwargs=None):
    """
    watch task until it ends and then execute callback:

        callback(response, **kwargs)

    where response is a result of task

    :param task: task to watch
    :param callback: function which is called when task finishes
    :param kwargs: dict which is passed to callback

    :return: None
    """
    response = task.wait()
    if kwargs:
        callback(response, **kwargs)
    else:
        callback(response)


class TaskApi(object):
    """ universal API for tasks which are executed on celery workers """

    def __init__(self):
        self.client = Celery('image_build')

        # config_from_object('celeryconfig') won't work because importlib
        self.client.config_from_object(celeryconfig)

    def build_docker_image(self, build_image, git_url, local_tag, git_dockerfile_path=None, git_commit=None,
                           source_registry=None, target_registry=None, tag=None, repos=None,
                           callback=None, kwargs=None):
        """
        build docker image from supplied git repo

        TODO:
        DockerFile has to be in root of the gitrepo, path and commit are not implemented yet
        yum repos

        :param build_image: name of the build image (supplied docker image is built inside this image)
        :param git_url: url to git repo
        :param local_tag: image is known within the service with this tag
        :param git_dockerfile_path: path to dockerfile within git repo (default is ./Dockerfile)
        :param git_commit: which commit to checkout (master by default)
        :param source_registry: pull base image from this registry
        :param target_registry: push built image to this registry
        :param tag: tag image with this tag (and push it to target_repo if specified)
        :param repos: list of yum repos to enable in image
        :param callback: function to call when task finishes, it has to accept at least
                        one argument: return value of task
        :param kwargs: dict which is pass to callback, callback is called like this:
                         callback(task_response, **kwargs)
        :return: task_id
        """
        args = [build_image, git_url, local_tag]
        task_kwargs = {'source_registry': source_registry,
                       'target_registry': target_registry,
                       'tag': tag,
                       'git_commit': git_commit,
                       'git_dockerfile_path': git_dockerfile_path,
                       'repos': repos}
        task_info = self.client.send_task('dbs_worker.docker_tasks.build_image', args=args, kwargs=task_kwargs)
        task_id = task_info.task_id
        if callback:
            t = Thread(target=watch_task, args=(task_info, callback, kwargs))
            #w.daemon = True
            t.start()
        return task_id

    def find_dockerfiles_in_git(self):
        raise NotImplemented()

    def push_docker_image(self, image_name, source_registry, target_registry, tags, callback, kwargs):
        """
        pull docker image from source registry, tag it with multiple tags and push it to tagrget registry

        :param image_name: image to pull
        :param source_registry: registry to pull from
        :param target_registry: registry for pushing
        :param tags: list of tags for image tagging
        :param callback: function to call when task finishes, it has to accept at least
                        one argument: return value of task
{'fraction_non_alphanumeric': 0.0662945820854887, 'fraction_numerical': 0.0, 'mean_word_length': 2.4525375939849625, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2796600', 'n_tokens_mistral': 1935, 'n_tokens_neox': 1851, 'n_words': 733}