def push_to_all(self, request, queryset): tokenlist = APNSToken.objects.all().filter(expired=False) with transaction.commit_on_success(): tasklist=[] for msg in queryset: for token in tokenlist: # Add to Msg queue entry = MsgQueue(apnstoken=token,apnsmessage=msg) entry.save() # Add to PreTaskQueue ptentry = PreTaskQueue(packet=binascii.hexlify(entry.to_packet()),msgidentifier=entry.id,pickedup=True) ptentry.save() tasklist.append((entry.to_packet(),ptentry.id,None)) # Add to Task Queue by mapping tasks together. Celery executes mapped tasks sequentially with the same worker. if tasklist: # Mark the last packet in the queue packet,ptentry,islastpacket = tasklist[-1] tasklist[-1] = packet,ptentry,True tasks.pushapnspacket.map(tasklist).delay() self.message_user(request, "%s message(s) to %s token(s) successfully added to MsgQueue." % (len(queryset) , len(tokenlist)))
def addFailedMsgs(frompcktId,toId): # Get the begining ID from which APNS push should be rescheduled. # frompcktId gives the id field from MsgQueue. Below line converts frompcktId of MSgQueue to ID on PreTaskQueue. # This step is needed to eliminate duplicate entries in MsgQueue. Each failed APNS packet is re-added to PreTaskQueue and in-turn Celery Task list. fromId = PreTaskQueue.objects.filter(msgidentifier=frompcktId).order_by('-id')[0].id logger.info('Re-Task: %d to %d ' % (fromId,toId)) ptqueue = PreTaskQueue.objects.all().filter(id__gt=fromId,id__lte=toId).order_by('id') with transaction.commit_on_success(): tasklist=[] for ptentry in ptqueue: pt = PreTaskQueue(packet=ptentry.packet,msgidentifier=ptentry.msgidentifier,pickedup=True) pt.save() tasklist.append((binascii.unhexlify(ptentry.packet),pt.id,None)) # map tasks together. Celery executes mapped tasks sequentially with the same worker. if tasklist: packet,ptentry,islastpacket = tasklist[-1] tasklist[-1] = packet,ptentry,True # re-add failed APNS packets to Celery task list pushapnspacket.map(tasklist).delay()
def addFailedMsgs(frompcktId, toId): # Get the begining ID from which APNS push should be rescheduled. # frompcktId gives the id field from MsgQueue. Below line converts frompcktId of MSgQueue to ID on PreTaskQueue. # This step is needed to eliminate duplicate entries in MsgQueue. Each failed APNS packet is re-added to PreTaskQueue and in-turn Celery Task list. fromId = PreTaskQueue.objects.filter( msgidentifier=frompcktId).order_by('-id')[0].id logger.info('Re-Task: %d to %d ' % (fromId, toId)) ptqueue = PreTaskQueue.objects.all().filter(id__gt=fromId, id__lte=toId).order_by('id') with transaction.commit_on_success(): tasklist = [] for ptentry in ptqueue: pt = PreTaskQueue(packet=ptentry.packet, msgidentifier=ptentry.msgidentifier, pickedup=True) pt.save() tasklist.append((binascii.unhexlify(ptentry.packet), pt.id, None)) # map tasks together. Celery executes mapped tasks sequentially with the same worker. if tasklist: packet, ptentry, islastpacket = tasklist[-1] tasklist[-1] = packet, ptentry, True # re-add failed APNS packets to Celery task list pushapnspacket.map(tasklist).delay()