populate_db: Refactor and rename send_messages().

We rename the send_messages function to generate_and_messages, and
factor out the actual sending part of it into a separate function, which
now gets the name send_messages().
This commit is contained in:
Mateusz Mandera 2019-07-22 14:45:34 +02:00 committed by Tim Abbott
parent e5138c38b2
commit 34b9a7bcad
1 changed files with 21 additions and 17 deletions

View File

@ -455,7 +455,7 @@ class Command(BaseCommand):
jobs.append((count, personals_pairs, options, self.stdout.write, random.randint(0, 10**10))) jobs.append((count, personals_pairs, options, self.stdout.write, random.randint(0, 10**10)))
for job in jobs: for job in jobs:
send_messages(job) generate_and_send_messages(job)
if options["delete"]: if options["delete"]:
# Create the "website" and "API" clients; if we don't, the # Create the "website" and "API" clients; if we don't, the
@ -573,7 +573,7 @@ def get_recipient_by_id(rid: int) -> Recipient:
# - multiple personals converastions # - multiple personals converastions
# - multiple messages per subject # - multiple messages per subject
# - both single and multi-line content # - both single and multi-line content
def send_messages(data: Tuple[int, Sequence[Sequence[int]], Mapping[str, Any], def generate_and_send_messages(data: Tuple[int, Sequence[Sequence[int]], Mapping[str, Any],
Callable[[str], Any], int]) -> int: Callable[[str], Any], int]) -> int:
(tot_messages, personals_pairs, options, output, random_seed) = data (tot_messages, personals_pairs, options, output, random_seed) = data
random.seed(random_seed) random.seed(random_seed)
@ -651,9 +651,18 @@ def send_messages(data: Tuple[int, Sequence[Sequence[int]], Mapping[str, Any],
recipients[num_messages] = (message_type, message.recipient.id, saved_data) recipients[num_messages] = (message_type, message.recipient.id, saved_data)
num_messages += 1 num_messages += 1
if (num_messages % message_batch_size) == 0 or (num_messages == tot_messages): if (num_messages % message_batch_size) == 0:
# Send the batch: # Send the batch and empty the list:
send_messages(messages)
messages = []
if len(messages) > 0:
# If there are unsent messages after exiting the loop, send them:
send_messages(messages)
return tot_messages
def send_messages(messages: List[Message]) -> None:
# We disable USING_RABBITMQ here, so that deferred work is # We disable USING_RABBITMQ here, so that deferred work is
# executed in do_send_message_messages, rather than being # executed in do_send_message_messages, rather than being
# queued. This is important, because otherwise, if run-dev.py # queued. This is important, because otherwise, if run-dev.py
@ -664,11 +673,6 @@ def send_messages(data: Tuple[int, Sequence[Sequence[int]], Mapping[str, Any],
do_send_messages([{'message': message} for message in messages]) do_send_messages([{'message': message} for message in messages])
settings.USING_RABBITMQ = True settings.USING_RABBITMQ = True
# Empty the list:
messages.clear()
return tot_messages
def choose_pub_date(num_messages: int, tot_messages: int, threads: int) -> datetime: def choose_pub_date(num_messages: int, tot_messages: int, threads: int) -> datetime:
# Spoofing time not supported with threading # Spoofing time not supported with threading
if threads != 1: if threads != 1: