-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add mgmt command to replay transaction reversal events
This will allow a consumer in enterprise-access to update related assignment record state for reversals that occurred before the producer/consumer logic was running to synchronize reversed state between ledger transactions and assignment records. ENT-9212
- Loading branch information
1 parent
ac28726
commit 70ef663
Showing
4 changed files
with
104 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
enterprise_subsidy/apps/transaction/management/commands/replay_reversal_events.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
""" | ||
Management command to replay openedx transaction reversal events for all | ||
currently-reversed ledger transactions. | ||
""" | ||
import logging | ||
|
||
from django.core.management.base import BaseCommand | ||
from openedx_events.event_bus import get_producer | ||
from openedx_ledger.models import Transaction, TransactionStateChoices | ||
|
||
from enterprise_subsidy.apps.core.event_bus import send_transaction_reversed_event | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Command(BaseCommand): | ||
""" | ||
Management command for writing Transaction Reversals from recent Enterprise unenrollments data. | ||
./manage.py write_reversals_from_enterprise_unenrollments | ||
""" | ||
|
||
def add_arguments(self, parser): | ||
""" | ||
Entry point for subclassed commands to add custom arguments. | ||
""" | ||
parser.add_argument( | ||
'--dry-run', | ||
action='store_true', | ||
dest='dry_run', | ||
help=( | ||
'If set, will only log a message about what events we would have produced,' | ||
'without actually producing them.' | ||
), | ||
) | ||
|
||
def handle(self, *args, **options): | ||
""" | ||
Finds all reversed transactions and emits a reversal event for each. | ||
""" | ||
all_reversed_transactions = Transaction.objects.select_related('reversal').filter( | ||
state=TransactionStateChoices.COMMITTED, | ||
reversal__isnull=False, | ||
reversal__state=TransactionStateChoices.COMMITTED, | ||
) | ||
|
||
for transaction_record in all_reversed_transactions: | ||
if not options.get('dry_run'): | ||
send_transaction_reversed_event(transaction_record) | ||
logger.info(f'Sent reversal event for transaction {transaction_record.uuid}') | ||
else: | ||
logger.info(f'[DRY RUN] Would have sent reversal event for transaction {transaction_record.uuid}') | ||
|
||
if not options.get('dry_run'): | ||
# Retrieve the cached producer and tell it to prepare for shutdown before this command exits. | ||
# This ensures that all messages in the send queue are flushed. Without this, this command | ||
# will exit and drop all produced messages before they can be sent to the broker. | ||
# See: https://github.com/openedx/event-bus-kafka/blob/main/edx_event_bus_kafka/internal/producer.py#L324 | ||
# and https://github.com/openedx/event-bus-kafka/blob/main/docs/decisions/0007-producer-polling.rst | ||
get_producer().prepare_for_shutdown() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters