<commit_before>"""
"""

import docker
import functools
import json
import logging
import mesos.interface
import pesos.executor
import os
import threading
import time
import re
import subprocess
import traceback
import io

from pesos.vendor.mesos import mesos_pb2

from portainer.app import subcommand
from portainer.proto import portainer_pb2


logger = logging.getLogger("portainer.executor")


@subcommand("build-executor")
def main(args):

    driver = pesos.executor.PesosExecutorDriver(Executor())

    thread = threading.Thread(target=driver.run)
    thread.setDaemon(True)
    thread.start()

    while thread.isAlive():
        time.sleep(0.5)


class Executor(mesos.interface.Executor):

    TASK_STARTING = mesos_pb2.TASK_STARTING
    TASK_RUNNING = mesos_pb2.TASK_RUNNING
    TASK_FINISHED = mesos_pb2.TASK_FINISHED
    TASK_FAILED = mesos_pb2.TASK_FAILED

    def __init__(self):
        self.build_task = None

        self.docker = None
        self.docker_daemon_up = False

    def registered(self, driver, executorInfo, frameworkInfo, slaveInfo):

        logger.info("Setting up environment for building containers")

        # Parse the build task object
        try:
            build_task = portainer_pb2.BuildTask()
            build_task.ParseFromString(executorInfo.data)
        except Exception:
            logger.error("Failed to parse BuildTask in ExecutorInfo.data")
            raise

        self.build_task = build_task

        # Launch the docker daemon
        def launch_docker_daemon():
            logger.info("Launching docker daemon subprocess")

            # TODO(tarnfeld): This should be made a little more flexible
            proc = subprocess.Popen(["/usr/local/bin/wrapdocker"])

            self.docker = docker.Client()
            while True:
                try:
                    self.docker.ping()
                except:
                    logger.info("Waiting for docker daemon to respond to pings")
                    time.sleep(1)
                else:
                    self.docker_daemon_up = True
                    break

            proc.wait()

        if not build_task.HasField("docker_host"):
            daemon_thread = threading.Thread(target=launch_docker_daemon)
            daemon_thread.setDaemon(True)
            daemon_thread.start()
        else:
            self.docker = docker.Client(build_task.docker_host)
            self.docker_daemon_up = True

    def disconnected(self, driver):
        logger.info("Disconnected from master! Ahh!")

    def reregistered(self, driver, slaveInfo):
        logger.info("Re-registered from the master! Ahh!")

    def launchTask(self, driver, taskInfo):

        logger.info("Launched task %s", taskInfo.task_id.value)

        # Spawn another thread to run the task, freeing up the executor
        thread = threading.Thread(target=functools.partial(
            self._build_image,
            driver,
            taskInfo,
            self.build_task
        ))

        thread.setDaemon(True)
        thread.start()

    def shutdown(self, driver):
        logger.info("Shutting down the executor")
        if os.path.exists("/var/run/docker.pid"):
            try:
                docker_pid = int(open("/var/run/docker.pid", "r").read())
                os.kill(docker_pid, signal.SIGTERM)
            except e:
                logger.error("Caught exception killing docker daemon")
                logger.error(e)<commit_msg>Better exception handling<commit_after>"""
"""

import docker
import functools
import json
import logging
import mesos.interface
import pesos.executor
import os
import threading
import time
import re
import subprocess
import traceback
import io

from pesos.vendor.mesos import mesos_pb2

from portainer.app import subcommand
from portainer.proto import portainer_pb2


logger = logging.getLogger("portainer.executor")


@subcommand("build-executor")
def main(args):

    driver = pesos.executor.PesosExecutorDriver(Executor())

    thread = threading.Thread(target=driver.run)
    thread.setDaemon(True)
    thread.start()

    while thread.isAlive():
        time.sleep(0.5)


class Executor(mesos.interface.Executor):

    TASK_STARTING = mesos_pb2.TASK_STARTING
    TASK_RUNNING = mesos_pb2.TASK_RUNNING
    TASK_FINISHED = mesos_pb2.TASK_FINISHED
    TASK_FAILED = mesos_pb2.TASK_FAILED

    def __init__(self):
        self.build_task = None

        self.docker = None
        self.docker_daemon_up = False

    def registered(self, driver, executorInfo, frameworkInfo, slaveInfo):

        logger.info("Setting up environment for building containers")

        # Parse the build task object
        try:
            build_task = portainer_pb2.BuildTask()
            build_task.ParseFromString(executorInfo.data)
        except Exception:
            logger.error("Failed to parse BuildTask in ExecutorInfo.data")
            raise

        self.build_task = build_task

        # Launch the docker daemon
        def launch_docker_daemon():
            logger.info("Launching docker daemon subprocess")

            # TODO(tarnfeld): This should be made a little more flexible
            proc = subprocess.Popen(["/usr/local/bin/wrapdocker"])

            self.docker = docker.Client()
            while True:
                try:
                    self.docker.ping()
                except:
                    logger.info("Waiting for docker daemon to respond to pings")
                    time.sleep(1)
                else:
                    self.docker_daemon_up = True
                    break

            proc.wait()

        if not build_task.HasField("docker_host"):
            daemon_thread = threading.Thread(target=launch_docker_daemon)
            daemon_thread.setDaemon(True)
            daemon_thread.start()
        else:
            self.docker = docker.Client(build_task.docker_host)
            self.docker_daemon_up = True

    def disconnected(self, driver):
        logger.info("Disconnected from master! Ahh!")

    def reregistered(self, driver, slaveInfo):
        logger.info("Re-registered from the master! Ahh!")

    def launchTask(self, driver, taskInfo):

        logger.info("Launched task %s", taskInfo.task_id.value)

        # Spawn another thread to run the task, freeing up the executor
        thread = threading.Thread(target=functools.partial(
            self._build_image,
            driver,
            taskInfo,
            self.build_task
        ))

        thread.setDaemon(True)
        thread.start()

    def shutdown(self, driver):
        logger.info("Shutting down the executor")
        if os.path.exists("/var/run/docker.pid"):
            try:
                docker_pid = int(open("/var/run/docker.pid", "r").read())
                os.kill(docker_pid, signal.SIGTERM)
            except Exception, e:
                logger.error("Caught exception killing docker daemon")
                logger.error(e)
{'fraction_non_alphanumeric': 0.08634856566239008, 'fraction_numerical': 0.0028830906732016724, 'mean_word_length': 2.2329916123019573, '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': '3711668', 'n_tokens_mistral': 2056, 'n_tokens_neox': 1921, 'n_words': 448}