def send_message_api_listed_osuser(title,
                                   body,
                                   user_id_list,
                                   logging_method=lambda x: None,
                                   url_name=None):
    """
    リストされたユーザーにメッセージAPIを送信
    """
    all_count = len(user_id_list)
    callback_url = get_callback_url(url_name)

    q = Queue()

    # プログレスプリンタを用意する
    progress_printer = ProgressPrinter(len(user_id_list))
    progress_printer.set_print_method(logging_method)
    progress_printer.print_initial_line()

    # ワーカーを用意する
    for i in range(MAX_TREADS):
        t = SendMessageWorker(q, title, body, callback_url, progress_printer)
        t.setDaemon(True)
        t.start()

    for i in xrange(0, all_count, MAX_SEND_COUNT):
        userid_list = user_id_list[i:i + MAX_SEND_COUNT]  #20人づつ持ってくる
        q.put((list(userid_list), i + MAX_SEND_COUNT))
    q.join()  #終わるまで待つ
    logging_method(u'finished.')
def send_message_api_shard_osuser(title,
                                  body,
                                  logging_method=lambda x: None,
                                  only_shard=None,
                                  url_name=None):
    """
    osuserを全員分ループし、メッセージAPIを出していく
    """
    all_count = OpenSocialUser.objects.all_partition_count()
    logging_method(u'[%s] start. all_count=%s ' %
                   (datetime.datetime.now().strftime("%H:%M:%S"), all_count))
    callback_url = get_callback_url(url_name)

    # 全シャードをループしてメッセージを送信
    for shard in range(settings.HORIZONTAL_PARTITIONING_PARTITION_NUMBER):
        if only_shard is not None:  # only_shard が指定されている場合はそのシャードのみ
            if not shard == int(only_shard):
                continue
        db_name = settings.HORIZONTAL_PARTITIONING_DB_NAME_FORMAT % shard
        logging_method(u'processing shard %s...' % db_name)
        shard_count = OpenSocialUser.objects.using(db_name).all().count()

        q = Queue()

        # プログレスプリンタを用意する
        progress = 0
        progress_printer = ProgressPrinter(shard_count)
        progress_printer.set_print_method(logging_method)
        progress_printer.print_initial_line()

        # ワーカーを用意する
        for i in range(MAX_TREADS):
            t = SendMessageWorker(q, title, body, callback_url,
                                  progress_printer)
            t.setDaemon(True)
            t.start()

        # user_id, progress をキューにつめていく
        while True:
            userid_list = OpenSocialUser.objects.using(db_name).values_list(
                'userid', flat=True).order_by('userid')[progress:progress +
                                                        MAX_SEND_COUNT]
            if not userid_list:
                break
            progress += MAX_SEND_COUNT
            q.put((list(userid_list), progress))
        q.join()  #終わるまで待つ
        logging_method(u'shard %s done.' % db_name)

    logging_method(u'finished.')
def send_message_api_shard_osuser(title,
                                  body,
                                  logging_method=lambda x: None,
                                  only_shard=None,
                                  url_name=None):
    """
    osuserを全員分ループし、メッセージAPIを出していく
    """
    all_count = OpenSocialUser.objects.all_partition_count()
    logging_method(u'[%s] start. all_count=%s ' %
                   (datetime.datetime.now().strftime("%H:%M:%S"), all_count))
    callback_url = get_callback_url(url_name)

    # 全シャードをループしてメッセージを送信
    for shard in range(settings.HORIZONTAL_PARTITIONING_PARTITION_NUMBER):
        if only_shard is not None:  # only_shard が指定されている場合はそのシャードのみ
            if not shard == int(only_shard):
                continue
        db_name = settings.HORIZONTAL_PARTITIONING_DB_NAME_FORMAT % shard
        logging_method(u'processing shard %s...' % db_name)
        shard_count = OpenSocialUser.objects.using(db_name).all().count()
        threadpool = ThreadPool(MAX_TREADS)
        progress = 0
        progress_printer = ProgressPrinter(shard_count)
        progress_printer.set_print_method(logging_method)
        progress_printer.print_initial_line()
        while True:
            userid_list = OpenSocialUser.objects.using(db_name).values_list(
                'userid', flat=True).order_by('userid')[progress:progress +
                                                        MAX_SEND_COUNT]

            if not userid_list:
                break
            progress += MAX_SEND_COUNT

            task = SendMessageTask(list(userid_list), title, body,
                                   callback_url, progress_printer, progress)

            threadpool.add_job(task.run)  # プールにつめる
        threadpool.start()  #実行!
        logging_method(u'shard %s done.' % db_name)

    logging_method(u'finished.')
def send_message_api_listed_osuser(title,
                                   body,
                                   user_id_list,
                                   logging_method=lambda x: None,
                                   url_name=None):
    """
    リストされたユーザーにメッセージAPIを送信
    """
    all_count = len(user_id_list)
    callback_url = get_callback_url(url_name)

    threadpool = ThreadPool(MAX_TREADS)
    progress_printer = ProgressPrinter(len(user_id_list))
    progress_printer.set_print_method(logging_method)
    progress_printer.print_initial_line()

    for i in xrange(0, all_count, MAX_SEND_COUNT):
        userid_list = user_id_list[i:i + MAX_SEND_COUNT]  #20人づつ持ってくる
        task = SendMessageTask(list(userid_list), title, body, callback_url,
                               progress_printer, i + MAX_SEND_COUNT)
        threadpool.add_job(task.run)  # プールにつめる
    threadpool.start()  #実行!
    logging_method(u'finished.')