zilencer: Update last_audit_log_update before `sync_license_ledger..`.

We need to update 'last_audit_log_update' before calling the
'sync_license_ledger_if_needed' method to avoid 'MissingDataError'
due to 'has_stale_audit_log' being True.

Also, we made the code block that creates audit logs,
updates 'last_audit_log_update', and syncs LicenseLedger in
an atomic operation.

This helps to rely on 'last_audit_log_update' to assume
'RemoteRealmAuditLog' and 'LicenseLedger' are up-to-date.
This commit is contained in:
Prakhar Pratyush 2023-12-12 15:17:06 +05:30 committed by Tim Abbott
parent cae11a60b6
commit 88fe0a7561
1 changed files with 45 additions and 39 deletions

View File

@ -925,6 +925,11 @@ def remote_server_post_analytics(
batch_create_table_data(server, RemoteInstallationCount, remote_installation_counts) batch_create_table_data(server, RemoteInstallationCount, remote_installation_counts)
if realmauditlog_rows is not None: if realmauditlog_rows is not None:
# Creating audit logs, syncing license ledger, and updating
# 'last_audit_log_update' needs to be an atomic operation.
# This helps to rely on 'last_audit_log_update' to assume
# RemoteRealmAuditLog and LicenseLedger are up-to-date.
with transaction.atomic():
# Important: Do not return early if we receive 0 rows; we must # Important: Do not return early if we receive 0 rows; we must
# updated last_audit_log_update even if there are no new rows, # updated last_audit_log_update even if there are no new rows,
# to help identify server whose ability to connect to this # to help identify server whose ability to connect to this
@ -956,18 +961,19 @@ def remote_server_post_analytics(
) )
batch_create_table_data(server, RemoteRealmAuditLog, remote_realm_audit_logs) batch_create_table_data(server, RemoteRealmAuditLog, remote_realm_audit_logs)
# We need to update 'last_audit_log_update' before calling the
# 'sync_license_ledger_if_needed' method to avoid 'MissingDataError'
# due to 'has_stale_audit_log' being True.
RemoteZulipServer.objects.filter(uuid=server.uuid).update(
last_audit_log_update=timezone_now()
)
# Update LicenseLedger using logs in RemoteRealmAuditlog. # Update LicenseLedger using logs in RemoteRealmAuditlog.
for remote_realm in remote_realms_set: for remote_realm in remote_realms_set:
if remote_realm: if remote_realm:
billing_session = RemoteRealmBillingSession(remote_realm=remote_realm) billing_session = RemoteRealmBillingSession(remote_realm=remote_realm)
billing_session.sync_license_ledger_if_needed() billing_session.sync_license_ledger_if_needed()
# Do this last, so we can assume LicenseLedger is always
# up-to-date through last_audit_log_update.
RemoteZulipServer.objects.filter(uuid=server.uuid).update(
last_audit_log_update=timezone_now()
)
remote_realm_dict: Dict[str, RemoteRealmDictValue] = {} remote_realm_dict: Dict[str, RemoteRealmDictValue] = {}
remote_realms = RemoteRealm.objects.filter(server=server) remote_realms = RemoteRealm.objects.filter(server=server)
for remote_realm in remote_realms: for remote_realm in remote_realms: