<commit_before>from multiprocessing import Process

from cnavbot.utils import logger
from cnavbot.messaging import pubsub


class Resource(object):
    topics = {}

    def __init__(self, publisher, *args, **kwargs):
        self.publisher = publisher

    def run(publisher):
        raise NotImplementedError()


class Service(object):

    # required
    name = None
    resource = None
    address = None
    port = None

    # set after start
    resource_instance = None
    process = None

    running = False

    def __init__(self, *args, **kwargs):
        required_attributes = ('name', 'resource', 'address', 'port')

        for attr in required_attributes:
            if kwargs.get(attr):
                setattr(self, attr, kwargs.pop(attr))

        if not all((getattr(self, attr) for attr in required_attributes)):
            raise NotImplementedError(
                "Not all required attributes set: {}".format(
                    ", ".join(required_attributes)
                )
            )

    @classmethod
    def get_subscriber(cls, publisher_address=None):
        return pubsub.Subscriber(
            publishers=(publisher_address or cls.address, ),
            topics=cls.resource.topics.values(),
        )

    def get_publisher(self):
        return pubsub.Publisher(port=self.port)

    def start(self):
        if self.running:
            logger.warning("Service already running")
            return

        logger.info("Starting {} service...".format(self.name))
        self.process = Process(target=self.run_resource)
        self.process.start()
        self.running = True

    def run_resource(self):
        self.resource_instance = self.resource(publisher=self.get_publisher())
        self.resource_instance.run()<commit_msg>Fix running service resource instance<commit_after>from multiprocessing import Process

from cnavbot.utils import logger
from cnavbot.messaging import pubsub


class Resource(object):
    topics = {}

    def __init__(self, publisher, *args, **kwargs):
        self.publisher = publisher

    def run(publisher):
        raise NotImplementedError()


class Service(object):

    # required
    name = None
    resource = None
    address = None
    port = None

    # set after start
    process = None

    running = False

    def __init__(self, *args, **kwargs):
        required_attributes = ('name', 'resource', 'address', 'port')

        for attr in required_attributes:
            if kwargs.get(attr):
                setattr(self, attr, kwargs.pop(attr))

        if not all((getattr(self, attr) for attr in required_attributes)):
            raise NotImplementedError(
                "Not all required attributes set: {}".format(
                    ", ".join(required_attributes)
                )
            )

    @classmethod
    def get_subscriber(cls, publisher_address=None):
        return pubsub.Subscriber(
            publishers=(publisher_address or cls.address, ),
            topics=cls.resource.topics.values(),
        )

    def get_publisher(self):
        return pubsub.Publisher(port=self.port)

    def start(self):
        if self.running:
            logger.warning("Service already running")
            return

        logger.info("Starting {} service...".format(self.name))
        self.process = Process(target=self.run_resource)
        self.process.start()
        self.running = True

    def run_resource(self):
        resource_instance = self.resource(publisher=self.get_publisher())
        resource_instance.run()
{'fraction_non_alphanumeric': 0.10036754311563471, 'fraction_numerical': 0.0, 'mean_word_length': 2.350378787878788, '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': '4412300', 'n_tokens_mistral': 970, 'n_tokens_neox': 933, 'n_words': 231}