<commit_before>from __future__ import unicode_literals

import logging
import six

from celery.task import task
from collections import defaultdict
from django.db import transaction
from django.utils import timezone
from django_redis import get_redis_connection
from temba.campaigns.models import Campaign, CampaignEvent, EventFire
from temba.msgs.models import HANDLER_QUEUE, HANDLE_EVENT_TASK, FIRE_EVENT
from temba.utils import chunk_list
from temba.utils.queues import push_task, nonoverlapping_task


logger = logging.getLogger(__name__)


@nonoverlapping_task(track_started=True, name='check_campaigns_task', lock_key='check_campaigns')
def check_campaigns_task():
    """
    See if any event fires need to be triggered
    """
    unfired = EventFire.objects.filter(fired=None, scheduled__lte=timezone.now())
    unfired = unfired.select_related('event')

    # group fire events by flow so they can be batched
    fires_by_flow_id = defaultdict(list)
    for fire in unfired:
        fires_by_flow_id[fire.event.flow_id].append(fire)

    # create queued tasks
    for flow_id, fires in six.iteritems(fires_by_flow_id):
        org_id = fires[0].contact.org_id

        # create sub-batches no no single task is too big
        for fire_batch in chunk_list(fires, 500):
            try:
                batch_ids = [f.id for f in fire_batch]
                push_task(org_id, HANDLER_QUEUE, HANDLE_EVENT_TASK, dict(type=FIRE_EVENT, fires=batch_ids))

            except Exception:  # pragma: no cover
                fire_ids_str = ','.join(six.text_type(f.id) for f in fires)
                logger.error("Error queuing campaign event fires: %s" % fire_ids_str, exc_info=True)


@task(track_started=True, name='update_event_fires_task')  # pragma: no cover
def update_event_fires(event_id):

    # get a lock
    r = get_redis_connection()
    key = 'event_fires_event_%d' % event_id<commit_msg>Fix log message so it only includes fires in its batch<commit_after>from __future__ import unicode_literals

import logging
import six

from celery.task import task
from collections import defaultdict
from django.db import transaction
from django.utils import timezone
from django_redis import get_redis_connection
from temba.campaigns.models import Campaign, CampaignEvent, EventFire
from temba.msgs.models import HANDLER_QUEUE, HANDLE_EVENT_TASK, FIRE_EVENT
from temba.utils import chunk_list
from temba.utils.queues import push_task, nonoverlapping_task


logger = logging.getLogger(__name__)


@nonoverlapping_task(track_started=True, name='check_campaigns_task', lock_key='check_campaigns')
def check_campaigns_task():
    """
    See if any event fires need to be triggered
    """
    unfired = EventFire.objects.filter(fired=None, scheduled__lte=timezone.now())
    unfired = unfired.select_related('event')

    # group fire events by flow so they can be batched
    fires_by_flow_id = defaultdict(list)
    for fire in unfired:
        fires_by_flow_id[fire.event.flow_id].append(fire)

    # create queued tasks
    for flow_id, fires in six.iteritems(fires_by_flow_id):
        org_id = fires[0].contact.org_id

        # create sub-batches no no single task is too big
        for fire_batch in chunk_list(fires, 500):
            try:
                batch_ids = [f.id for f in fire_batch]
                push_task(org_id, HANDLER_QUEUE, HANDLE_EVENT_TASK, dict(type=FIRE_EVENT, fires=batch_ids))

            except Exception:  # pragma: no cover
                fire_ids_str = ','.join(six.text_type(f.id) for f in fire_batch)
                logger.error("Error queuing campaign event fires: %s" % fire_ids_str, exc_info=True)


@task(track_started=True, name='update_event_fires_task')  # pragma: no cover
def update_event_fires(event_id):

    # get a lock
    r = get_redis_connection()
    key = 'event_fires_event_%d' % event_id
{'fraction_non_alphanumeric': 0.07780082987551867, 'fraction_numerical': 0.002074688796680498, 'mean_word_length': 3.888466413181242, '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': '3678122', 'n_tokens_mistral': 1259, 'n_tokens_neox': 1194, 'n_words': 323}