forked from erp-dev/erp
fix: retry_failed_external_order_sync remove limit default
This commit is contained in:
@@ -15,7 +15,7 @@ class Command(BaseCommand):
|
||||
help = '基于失败记录重试外部印染 records 同步'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('--limit', type=int, default=100, help='最多重试多少条失败记录')
|
||||
parser.add_argument('--limit', type=int, default=None, help='最多重试多少条失败记录;不传则重试全部')
|
||||
parser.add_argument(
|
||||
'--run-date',
|
||||
type=str,
|
||||
@@ -31,7 +31,9 @@ class Command(BaseCommand):
|
||||
)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
limit = max(1, int(options.get('limit') or 100))
|
||||
limit = options.get('limit')
|
||||
if limit is not None:
|
||||
limit = max(1, int(limit))
|
||||
run_date_text = options.get('run_date')
|
||||
record_ids = options.get('record_id') or []
|
||||
|
||||
@@ -59,7 +61,7 @@ class Command(BaseCommand):
|
||||
continue
|
||||
selected_failures.append(failure)
|
||||
selected_record_ids.add(failure.external_record_id)
|
||||
if len(selected_failures) >= limit:
|
||||
if limit is not None and len(selected_failures) >= limit:
|
||||
break
|
||||
|
||||
if not selected_failures:
|
||||
@@ -111,7 +113,10 @@ class Command(BaseCommand):
|
||||
external_record_id__in=succeeded_ids
|
||||
).delete()
|
||||
|
||||
remaining_failures = api_models.PrintingExternalSyncFailure.objects.count()
|
||||
|
||||
result = {
|
||||
'limit': limit,
|
||||
'retried_records': len(retry_records),
|
||||
'skipped_records': skipped_records,
|
||||
'orders_created': batch_result['orders_created'],
|
||||
@@ -121,5 +126,6 @@ class Command(BaseCommand):
|
||||
'failed_records': batch_result['failed_records'],
|
||||
'failed_record_ids': batch_result['failed_record_ids'],
|
||||
'deleted_failures': deleted_failures,
|
||||
'remaining_failures': remaining_failures,
|
||||
}
|
||||
self.stdout.write(self.style.SUCCESS(str(result)))
|
||||
@@ -290,6 +290,34 @@ class ExternalPrintingRecordsSyncTaskTest(TestCase):
|
||||
api_models.PrintingExternalSyncFailure.objects.filter(external_record_id=1000003).exists()
|
||||
)
|
||||
self.assertIn("'retried_records': 1", output.getvalue())
|
||||
self.assertIn("'limit': None", output.getvalue())
|
||||
self.assertIn("'remaining_failures': 0", output.getvalue())
|
||||
|
||||
def test_retry_command_without_limit_retries_all_failures(self):
|
||||
first_record = self._build_record(record_id=1000010, product_name=self.existing_product.name)
|
||||
first_record['customer']['KhName'] = '补建客户甲'
|
||||
second_record = self._build_record(record_id=1000011, product_name=self.existing_product.name)
|
||||
second_record['customer']['KhName'] = '补建客户乙'
|
||||
|
||||
for record in [first_record, second_record]:
|
||||
api_models.PrintingExternalSyncFailure.objects.create(
|
||||
run_date=timezone.localdate(),
|
||||
external_record_id=record['ID'],
|
||||
external_order_id=record['BianHaoID'],
|
||||
product_name=record['YanSe'],
|
||||
error=f"未找到客户: {record['customer']['KhName']}",
|
||||
raw=record,
|
||||
attempts=1,
|
||||
last_attempt_at=timezone.now(),
|
||||
)
|
||||
|
||||
output = StringIO()
|
||||
call_command('retry_external_printing_sync_failures', stdout=output)
|
||||
|
||||
self.assertIn("'limit': None", output.getvalue())
|
||||
self.assertIn("'retried_records': 2", output.getvalue())
|
||||
self.assertIn("'remaining_failures': 0", output.getvalue())
|
||||
self.assertFalse(api_models.PrintingExternalSyncFailure.objects.exists())
|
||||
|
||||
@patch('api_v1.tasks._advance_external_printing_cursor')
|
||||
@patch('api_v1.tasks._fetch_external_product_image')
|
||||
|
||||
Reference in New Issue
Block a user