(20250118) Ready to test Mail Sync (Cron) on the server.

This commit is contained in:
2025-01-18 13:14:42 +05:30
parent 766b196a80
commit 258b2fdbaf
8 changed files with 80 additions and 44 deletions
+17 -11
View File
@@ -71,6 +71,7 @@ import time
from models.core.auth_token import CoreAuthTokenModel
from models.core.user import CoreUserInfoModel
from models.message.mail.sync import MailSyncOneResult, MailSyncManyResults
from models.core.ai.llm import LLMInput, LLMInputMessage
# For asynchronous activities:
import asyncio
@@ -78,6 +79,9 @@ import asyncio
# To work with various datatypes:
from typing import List
# For random values:
import random
# Debugging:
from icecream import IceCreamDebugger
@@ -317,8 +321,6 @@ async def sync_one_account(auth_token: CoreAuthTokenModel):
:return: ??
"""
# printer("Sync'ing account.", auth_token.clientUserId["email"], auth_token.authTokenId)
# Note down the time at which the attempt to sync the account is being made:
now = date_time.get_current_utc_date_time(as_string = False)
@@ -356,13 +358,20 @@ async def sync_one_account(auth_token: CoreAuthTokenModel):
await mail_controller.release_token_from_batch_by_id(
mongo_data_conn = data_mongo,
token_id = auth_token.authTokenId,
# sync_after_ts = now + datetime.timedelta(seconds = auth_token.syncFreq or 300),
sync_after_ts = now + datetime.timedelta(seconds = 1),
sync_after_ts = now + datetime.timedelta(seconds = auth_token.syncFreq or 300),
last_sync_ts = now
)
# Done here:
if sync_results.totalCount: printer(auth_token.clientUserId["email"], sync_results.successCount, sync_results.totalCount)
if sync_results.totalCount:
printer(
auth_token.clientUserId["email"],
sync_results.totalCount,
sync_results.newCount,
sync_results.attemptedCount,
sync_results.successCount,
sync_results.failureCount
)
# ---------------------------------------------------------------------------------------------------------------------
@@ -379,14 +388,12 @@ async def sync_accounts(batch_size: int) -> None:
while True:
# Get batches of auth-tokens to work with:
# no_context_printer("Getting batch.")
# no_context_printer(batch_size)
auth_tokens_batch = await mail_controller.get_batches_to_sync(
mongo_data_conn = data_mongo,
limit = batch_size,
batch_timeout_seconds = 10
)
# no_context_printer(len(auth_tokens_batch))
no_context_printer(len(auth_tokens_batch))
# Process each batch:
now = date_time.get_current_utc_date_time(as_string = False)
@@ -394,9 +401,8 @@ async def sync_accounts(batch_size: int) -> None:
results = await asyncio.gather(*tasks)
# Small delay to not overload the database:
# if len(auth_tokens_batch) == 0: await asyncio.sleep(5.0)
await asyncio.sleep(1.0)
# no_context_printer()
if len(auth_tokens_batch) == 0: await asyncio.sleep(4.0 + (random.random() * 2.0))
else: await asyncio.sleep(1.0 + (random.random() * 2.0))
# *****************************************************************************************************************