Sidekiq::Worker::EnqueueFromTransactionError: `AuditEvents::AuditEventStreamingWorker.perform_async` cannot be called inside a transaction
Description
We are getting a couple of Sidekiq::Worker::EnqueueFromTransactionError exception while enqueuing AuditEventStreamingWorker. A similar issue was encountered in the past which was fixed via MR !76765 (merged).
Stacktrace
Sidekiq::Worker::EnqueueFromTransactionError: `AuditEvents::AuditEventStreamingWorker.perform_async` cannot be called inside a transaction as this can lead to
race conditions when the worker runs before the transaction is committed and
tries to access a model that has not been saved yet.
Use an `after_commit` hook, or include `AfterCommitQueue` and use a `run_after_commit` block instead.
from config/initializers/forbid_sidekiq_in_transactions.rb:25:in `block (2 levels) in <module:NoEnqueueingFromTransactions>'
from ee/app/models/ee/audit_event.rb:62:in `stream_to_external_destinations'
from ee/lib/gitlab/audit/auditor.rb:92:in `block in send_to_stream'
from ee/lib/gitlab/audit/auditor.rb:92:in `each'
from ee/lib/gitlab/audit/auditor.rb:92:in `send_to_stream'
from ee/lib/gitlab/audit/auditor.rb:88:in `record'
from ee/lib/gitlab/audit/auditor.rb:72:in `block in multiple_audit'
from ee/lib/gitlab/audit/auditor.rb:72:in `then'
from ee/lib/gitlab/audit/auditor.rb:72:in `multiple_audit'
from ee/lib/gitlab/audit/auditor.rb:47:in `audit'
from ee/app/services/concerns/approval_rules/updater.rb:32:in `with_audit_logged'
from ee/app/services/concerns/approval_rules/updater.rb:12:in `action'
from ee/app/services/approval_rules/base_service.rb:8:in `execute'
from ee/app/services/security/security_orchestration_policies/process_scan_result_policy_service.rb:31:in `block in create_new_approval_rules'
from ee/app/services/security/security_orchestration_policies/process_scan_result_policy_service.rb:28:in `each'
from ee/app/services/security/security_orchestration_policies/process_scan_result_policy_service.rb:28:in `each_with_index'
from ee/app/services/security/security_orchestration_policies/process_scan_result_policy_service.rb:28:in `create_new_approval_rules'
from ee/app/services/security/security_orchestration_policies/process_scan_result_policy_service.rb:17:in `execute'
from ee/app/workers/concerns/update_orchestration_policy_configuration.rb:24:in `block (2 levels) in update_policy_configuration'
from ee/app/workers/concerns/update_orchestration_policy_configuration.rb:21:in `each'
from ee/app/workers/concerns/update_orchestration_policy_configuration.rb:21:in `each_with_index'
from ee/app/workers/concerns/update_orchestration_policy_configuration.rb:21:in `block in update_policy_configuration'
from app/models/concerns/cross_database_modification.rb:93:in `block in transaction'
