Example #1
0
    def register_active_event(self):
        """
        Helper function for registering active events on a throttled channel
        """
        r = get_redis_connection()

        channel_key = Channel.redis_active_events_key(self.channel_id)

        r.incr(channel_key)
Example #2
0
    def unregister_active_event(self):
        """
        Helper function for unregistering active events on a throttled channel
        """
        r = get_redis_connection()

        channel_key = Channel.redis_active_events_key(self.channel_id)
        # are we on a throttled channel?
        current_tracked_events = r.get(channel_key)

        if current_tracked_events:

            value = int(current_tracked_events)
            if value <= 0:  # pragma: no cover
                raise ValueError("When this happens I'll quit my job and start producing moonshine/poitin/brlja !")

            r.decr(channel_key)
Example #3
0
def task_enqueue_call_events():
    from .models import IVRCall

    r = get_redis_connection()

    pending_call_events = (IVRCall.objects.filter(
        status=IVRCall.PENDING).filter(direction=IVRCall.OUTGOING).filter(
            channel__is_active=True).filter(
                modified_on__gt=timezone.now() -
                timedelta(days=IVRCall.IGNORE_PENDING_CALLS_OLDER_THAN_DAYS)
            ).select_related("channel").order_by("modified_on")[:1000])

    for call in pending_call_events:

        # are we handling a call on a throttled channel ?
        max_concurrent_events = call.channel.config.get(
            Channel.CONFIG_MAX_CONCURRENT_EVENTS)

        if max_concurrent_events:
            channel_key = Channel.redis_active_events_key(call.channel_id)
            current_active_events = r.get(channel_key)

            # skip this call if are on the limit
            if current_active_events and int(
                    current_active_events) >= max_concurrent_events:
                continue
            else:
                # we can start a new call event
                call.register_active_event()

        # enqueue the call
        ChannelLog.log_ivr_interaction(call, "Call queued internally",
                                       HttpEvent(method="INTERNAL", url=None))

        call.status = IVRCall.QUEUED
        call.save(update_fields=("status", ))

        start_call_task.apply_async(kwargs={"call_pk": call.id})
Example #4
0
def task_enqueue_call_events():
    from .models import IVRCall

    r = get_redis_connection()

    pending_call_events = (
        IVRCall.objects.filter(status=IVRCall.PENDING)
        .filter(direction=IVRCall.OUTGOING, is_active=True)
        .filter(channel__is_active=True)
        .filter(modified_on__gt=timezone.now() - timedelta(days=IVRCall.IGNORE_PENDING_CALLS_OLDER_THAN_DAYS))
        .select_related("channel")
        .order_by("modified_on")[:1000]
    )

    for call in pending_call_events:

        # are we handling a call on a throttled channel ?
        max_concurrent_events = call.channel.config.get(Channel.CONFIG_MAX_CONCURRENT_EVENTS)

        if max_concurrent_events:
            channel_key = Channel.redis_active_events_key(call.channel_id)
            current_active_events = r.get(channel_key)

            # skip this call if are on the limit
            if current_active_events and int(current_active_events) >= max_concurrent_events:
                continue
            else:
                # we can start a new call event
                call.register_active_event()

        # enqueue the call
        ChannelLog.log_ivr_interaction(call, "Call queued internally", HttpEvent(method="INTERNAL", url=None))

        call.status = IVRCall.QUEUED
        call.save(update_fields=("status",))

        start_call_task.apply_async(kwargs={"call_pk": call.id}, queue=Queue.HANDLER)