def check_messages_task(): # pragma: needs cover """ Checks to see if any of our aggregators have errored messages that need to be retried. Also takes care of flipping Contacts from Failed to Normal and back based on their status. """ from django.utils import timezone from .models import INCOMING, PENDING from temba.orgs.models import Org from temba.channels.tasks import send_msg_task from temba.flows.tasks import start_msg_flow_batch_task now = timezone.now() five_minutes_ago = now - timedelta(minutes=5) r = get_redis_connection() # for any org that sent messages in the past five minutes, check for pending messages for org in Org.objects.filter( msgs__created_on__gte=five_minutes_ago).distinct(): # more than 1,000 messages queued? don't do anything, wait for our queue to go down queued = r.zcard('send_message_task:%d' % org.id) if queued < 1000: org.trigger_send() # fire a few send msg tasks in case we dropped one somewhere during a restart # (these will be no-ops if there is nothing to do) for i in range(100): send_msg_task.apply_async(queue='msgs') handle_event_task.apply_async(queue='handler') start_msg_flow_batch_task.apply_async(queue='flows') # also check any incoming messages that are still pending somehow, reschedule them to be handled unhandled_messages = Msg.objects.filter(direction=INCOMING, status=PENDING, created_on__lte=five_minutes_ago) unhandled_messages = unhandled_messages.exclude(channel__org=None).exclude( contact__is_test=True) unhandled_count = unhandled_messages.count() if unhandled_count: print("** Found %d unhandled messages" % unhandled_count) for msg in unhandled_messages[:100]: msg.handle()
def check_messages_task(): # pragma: needs cover """ Checks to see if any of our aggregators have errored messages that need to be retried. Also takes care of flipping Contacts from Failed to Normal and back based on their status. """ from .models import INCOMING, PENDING from temba.orgs.models import Org from temba.channels.tasks import send_msg_task from temba.flows.tasks import start_msg_flow_batch_task now = timezone.now() five_minutes_ago = now - timedelta(minutes=5) r = get_redis_connection() # for any org that sent messages in the past five minutes, check for pending messages for org in Org.objects.filter(msgs__created_on__gte=five_minutes_ago).distinct(): # more than 1,000 messages queued? don't do anything, wait for our queue to go down queued = r.zcard("send_message_task:%d" % org.id) if queued < 1000: org.trigger_send() # fire a few send msg tasks in case we dropped one somewhere during a restart # (these will be no-ops if there is nothing to do) for i in range(100): send_msg_task.apply_async(queue=Queue.MSGS) handle_event_task.apply_async(queue=Queue.HANDLER) start_msg_flow_batch_task.apply_async(queue=Queue.FLOWS) # also check any incoming messages that are still pending somehow, reschedule them to be handled unhandled_messages = Msg.objects.filter(direction=INCOMING, status=PENDING, created_on__lte=five_minutes_ago) unhandled_messages = unhandled_messages.exclude(channel__is_active=False).exclude(contact__is_test=True) unhandled_count = unhandled_messages.count() if unhandled_count: print("** Found %d unhandled messages" % unhandled_count) for msg in unhandled_messages[:100]: msg.handle()