<commit_before>from .. import queue
from .mail_service import mail_services
from utils import timestamp, post_event_url,build_event
from .mail_service.mail_service import MailServiceException
import json
import random
from flask import current_app

def get_mail_server(attempts):
    """Pick a mail service to send the email.

    For a new request, the service is choosen randomly.
    Otherwise, this function return a server that has
    never been selected before

    Args:
    attempts (list of str): contains the name of
    the services already choosen to process this request
    """

    if len(attempts):
        for server in mail_services.keys():
            if server not in attempts:
                return (server, mail_services[server])
    else:
        i = random.randrange(0, 1e9)%len(mail_services)
        return (mail_services.keys()[i], mail_services.values()[i])
    
class Dispatcher(object):
    Name = 'DISPATCHER'
    @staticmethod
    def dispatch(mail, url_events=None):
        attempts = []
        if current_app.config['QUEUE'] is True:
            post_event_url(url_events, build_event(created_by=Dispatcher.Name, \
                event=('ENQUEUE'), mail_id=mail.id, blob=json.dumps({'service':service_name})))
            service.send(mail, url_events)
            return enqueue.apply_async(args=[mail, attempts, url_events])
        else:
            (service_name, service) = get_mail_server(attempts)
            while service_name is not None:
                attempts.append(service_name)
                try:
                    post_event_url(url_events, build_event(created_by=Dispatcher.Name, \
                        event=('READY TO SEND'), mail_id=mail.id, blob=json.dumps({'service':service_name})))
                    service.send(mail, url_events)<commit_msg>bug fixing in dispatcher<commit_after>from .. import queue
from .mail_service import mail_services
from utils import timestamp, post_event_url,build_event
from .mail_service.mail_service import MailServiceException
import json
import random
from flask import current_app

def get_mail_server(attempts):
    """Pick a mail service to send the email.

    For a new request, the service is choosen randomly.
    Otherwise, this function return a server that has
    never been selected before

    Args:
    attempts (list of str): contains the name of
    the services already choosen to process this request
    """

    if len(attempts):
        for server in mail_services.keys():
            if server not in attempts:
                return (server, mail_services[server])
    else:
        i = random.randrange(0, 1e9)%len(mail_services)
        return (mail_services.keys()[i], mail_services.values()[i])
    
class Dispatcher(object):
    Name = 'DISPATCHER'
    @staticmethod
    def dispatch(mail, url_events=None):
        attempts = []
        if current_app.config['QUEUE'] is True:
            post_event_url(url_events, build_event(created_by=Dispatcher.Name, \
                event=('ENQUEUE'), mail_id=mail.id))
            return enqueue.apply_async(args=[mail, attempts, url_events])
        else:
            (service_name, service) = get_mail_server(attempts)
            while service_name is not None:
                attempts.append(service_name)
                try:
                    post_event_url(url_events, build_event(created_by=Dispatcher.Name, \
                        event=('READY TO SEND'), mail_id=mail.id, blob=json.dumps({'service':service_name})))
                    service.send(mail, url_events)
{'fraction_non_alphanumeric': 0.08883890919314029, 'fraction_numerical': 0.0016868147315153219, 'mean_word_length': 2.7730646871686107, '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': '1428448', 'n_tokens_mistral': 1012, 'n_tokens_neox': 980, 'n_words': 273}