Ejemplo n.º 1
0
def send_test_mail():
    servers = make_servers(is_test=False)
    available_server_ids = [k for k, v in servers.items() if v['status'] != 4]

    data = {'mode': 'test'}
    for s in available_server_ids:
        print "send to {0}".format(s),
        api_send_mail(s, json.dumps(data))
        print "done"
Ejemplo n.º 2
0
def send_one_mail(mail, server_ids):
    data = make_payload(mail)

    # data format:
    # {
    #     'char_ids': [],
    #     'mail': {},
    # }

    # 某server收到send_mail的api请求后,如果data中有 char_ids,那么就是发送给这些角色
    # 如果没有char_ids,那么就是发送给全服

    has_send_to = [int(i) for i in mail.has_send_to.split(',') if i.isdigit()]

    if mail.send_type == 3:
        # 发给部分角色
        cids = [int(i) for i in mail.send_to.split(',') if int(i) not in has_send_to]
        chars = Character.objects.filter(id__in=cids)
        cid_sid = [(c.id, c.server_id) for c in chars]
        cid_sid.sort(key=lambda item: item[1])
        cid_sid = groupby(cid_sid, lambda item: item[1])

        for sid, in_server_cids in cid_sid:
            this_server_cids = [_c for _c, _ in in_server_cids]
            data['char_id'] = this_server_cids
            try:
                api_send_mail(sid, json.dumps(data))
            except APIFailure:
                raise Exception(traceback.format_exc())
            else:
                mail.has_send_to = mail.has_send_to + ',' + ','.join([str(i) for i in this_server_cids])
                mail.save()
        return

    if mail.send_type == 1:
        # 发给全部
        sids = [i for i in server_ids if i not in has_send_to]
    else:
        # 发给部分服务器
        sids = [int(i) for i in mail.send_to.split(',') if int(i) not in has_send_to]

    for sid in sids:
        try:
            api_send_mail(sid, json.dumps(data))
        except APIFailure:
            raise Exception(traceback.format_exc())
        else:
            mail.has_send_to = mail.has_send_to + ',' + str(sid)
            mail.save()