<commit_before># Oxypanel
# File: websockets/task.py
# Desc: the task subscribe/watch websocket (Redis events -> websocket)

from .. import settings
from ..models.websocket import Websocket
from ..util.web.pubsub import subscribe


class Subscribe(Websocket):
    '''Subscribes to a task via Redis pubsub & relays to client.'''
    NAME = 'core/task_subscribe'

    def __init__(self, data, ws):
        self.ws = ws

        # Subscribe to the task
        subscribe(self._on_data, channel='{}{}'.format(
            settings.REDIS_TASK_PREFIX,
            data
        ))

        # Keeps the websocket open
        self.run_forever()

    def _on_data(self, data):
        '''Forward incoming Redis events -> websocket, already JSON'd.'''
        print data
        self.ws.send(data)


class Admin(Websocket):
    '''Monitors & admins all running tasks.'''
    NAME = 'core/task_admin'<commit_msg>Unsubscribe from pubsub after websocket closes.<commit_after># Oxypanel
# File: websockets/task.py
# Desc: the task subscribe/watch websocket (Redis events -> websocket)

from .. import settings
from ..models.websocket import Websocket
from ..util.web.pubsub import subscribe, unsubscribe


class Subscribe(Websocket):
    '''Subscribes to a task via Redis pubsub & relays to client.'''

    NAME = 'core/task_subscribe'

    def __init__(self, data, ws):
        self.ws = ws

        channel_name = '{0}{1}'.format(settings.REDIS_TASK_PREFIX, data)

        # Subscribe to the task
        subscribe(self.on_task_event, channel=channel_name)

        # Wait until the websocket is closed
        self.run_forever()

        # Connection closed, let's unsusbscribe our function
        unsubscribe(self.on_task_event, channel=channel_name)

    def on_task_event(self, data):
        '''Forward incoming Redis events -> websocket, already JSON'd.'''

        self.ws.send(data)


class Admin(Websocket):
    '''Monitors & admins all running tasks.'''

    NAME = 'core/task_admin'
{'fraction_non_alphanumeric': 0.10746720484359233, 'fraction_numerical': 0.0010090817356205853, 'mean_word_length': 2.9819277108433737, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '3916041', 'n_tokens_mistral': 618, 'n_tokens_neox': 589, 'n_words': 170}