mirror of
https://github.com/FOSSBilling/FOSSBilling.git
synced 2026-12-01 12:48:08 +09:00
Mark transactions as errored and implement SQL claim fixes (#3782)
* Mark transactions as errored and allow retries * Use IN for claim SQL and fix logger args
This commit is contained in:
1 parent
2afd52b2af
commit
ef2bc70319
2 files changed
+201
-10
No files matched your search
@@ -80,7 +80,13 @@ class ServiceTransaction implements InjectionAwareInterface
|
||||
return $id;
|
||||
}
|
||||
|
||||
$this->processTransaction($id);
|
||||
try {
|
||||
$this->processTransaction($id);
|
||||
} catch (\Throwable $e) {
|
||||
$this->markTransactionError($id, $e);
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return $id;
|
||||
}
|
||||
@@ -420,8 +426,10 @@ class ServiceTransaction implements InjectionAwareInterface
|
||||
* Uses conditional UPDATE to prevent race conditions when multiple
|
||||
* workers attempt to process the same transaction simultaneously.
|
||||
*
|
||||
* Accepts 'received' status immediately, and allows stale 'processing'
|
||||
* transactions to be reclaimed after the recovery timeout.
|
||||
* Accepts 'received' status immediately, allows stale 'processing'
|
||||
* transactions to be reclaimed after the recovery timeout, and allows
|
||||
* 'error' transactions to be retried (e.g. via the admin Process button
|
||||
* or PayPal IPN retries).
|
||||
*
|
||||
* @param int $id Transaction ID
|
||||
*
|
||||
@@ -430,12 +438,13 @@ class ServiceTransaction implements InjectionAwareInterface
|
||||
public function claimForProcessing(int $id): bool
|
||||
{
|
||||
$affectedRows = $this->di['db']->exec(
|
||||
'UPDATE transaction SET status = ?, updated_at = ? WHERE id = ? AND (status = ? OR (status = ? AND (updated_at IS NULL OR updated_at <= ?)))',
|
||||
'UPDATE transaction SET status = ?, updated_at = ? WHERE id = ? AND (status IN (?, ?) OR (status = ? AND (updated_at IS NULL OR updated_at <= ?)))',
|
||||
[
|
||||
\Model_Transaction::STATUS_PROCESSING,
|
||||
date('Y-m-d H:i:s'),
|
||||
$id,
|
||||
\Model_Transaction::STATUS_RECEIVED,
|
||||
\Model_Transaction::STATUS_ERROR,
|
||||
\Model_Transaction::STATUS_PROCESSING,
|
||||
$this->getProcessingRecoveryThreshold(),
|
||||
]
|
||||
@@ -448,12 +457,8 @@ class ServiceTransaction implements InjectionAwareInterface
|
||||
{
|
||||
try {
|
||||
$output = $this->processTransaction($model->id);
|
||||
} catch (\FOSSBilling\Exception $e) {
|
||||
$model->status = \Model_Transaction::STATUS_ERROR;
|
||||
$model->error = $e->getMessage();
|
||||
$model->error_code = $e->getCode();
|
||||
$model->updated_at = date('Y-m-d H:i:s');
|
||||
$this->di['db']->store($model);
|
||||
} catch (\Throwable $e) {
|
||||
$this->markTransactionError((int) $model->id, $e);
|
||||
|
||||
throw $e;
|
||||
}
|
||||
@@ -464,6 +469,29 @@ class ServiceTransaction implements InjectionAwareInterface
|
||||
return !empty($output) ? $output : true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark a transaction as errored due to a processing failure.
|
||||
*
|
||||
* Reloads the transaction from the database so the status is current, and
|
||||
* only marks it as errored if it has not already been processed, ensuring
|
||||
* a successful processing is never clobbered by a stale exception.
|
||||
*/
|
||||
private function markTransactionError(int $id, \Throwable $e): void
|
||||
{
|
||||
$tx = $this->di['db']->load('Transaction', $id);
|
||||
if (!$tx instanceof \Model_Transaction || $tx->status === \Model_Transaction::STATUS_PROCESSED) {
|
||||
return;
|
||||
}
|
||||
|
||||
$tx->status = \Model_Transaction::STATUS_ERROR;
|
||||
$tx->error = $e->getMessage();
|
||||
$tx->error_code = $e->getCode();
|
||||
$tx->updated_at = date('Y-m-d H:i:s');
|
||||
$this->di['db']->store($tx);
|
||||
|
||||
$this->di['logger']->error('Failed to process transaction #%s: %s', $id, $e->getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* New simplified transaction processing logic.
|
||||
*
|
||||
|
||||
@@ -222,3 +222,166 @@ test('counts transactions', function (): void {
|
||||
$result = $service->counter();
|
||||
expect($result)->toBeArray();
|
||||
});
|
||||
|
||||
test('createAndProcess marks transaction as error when processing throws', function (): void {
|
||||
$transactionModel = new Model_Transaction();
|
||||
$transactionModel->loadBean(new Tests\Helpers\DummyBean());
|
||||
$transactionModel->id = 1;
|
||||
$transactionModel->status = Model_Transaction::STATUS_RECEIVED;
|
||||
|
||||
$dbMock = Mockery::mock('\Box_Database');
|
||||
$dbMock->shouldReceive('getExistingModelById')
|
||||
->with('Transaction', 1)
|
||||
->andReturn($transactionModel);
|
||||
$dbMock->shouldReceive('load')
|
||||
->with('Transaction', 1)
|
||||
->andReturn($transactionModel);
|
||||
$dbMock->shouldReceive('store')
|
||||
->with($transactionModel)
|
||||
->once();
|
||||
|
||||
$di = container();
|
||||
$di['db'] = $dbMock;
|
||||
$di['logger'] = new Tests\Helpers\TestLogger();
|
||||
|
||||
$service = Mockery::mock(ServiceTransaction::class)->makePartial();
|
||||
$service->shouldReceive('create')->once()->andReturn(1);
|
||||
$service->shouldReceive('processTransaction')
|
||||
->with(1)
|
||||
->once()
|
||||
->andThrow(new RuntimeException('Processing failed'));
|
||||
$service->setDi($di);
|
||||
|
||||
$thrown = null;
|
||||
|
||||
try {
|
||||
$service->createAndProcess([]);
|
||||
} catch (Throwable $e) {
|
||||
$thrown = $e;
|
||||
}
|
||||
|
||||
expect($thrown)->toBeInstanceOf(RuntimeException::class)
|
||||
->and($thrown->getMessage())->toBe('Processing failed')
|
||||
->and($transactionModel->status)->toBe(Model_Transaction::STATUS_ERROR)
|
||||
->and($transactionModel->error)->toBe('Processing failed');
|
||||
});
|
||||
|
||||
test('createAndProcess skips processing when transaction is already processed', function (): void {
|
||||
$transactionModel = new Model_Transaction();
|
||||
$transactionModel->loadBean(new Tests\Helpers\DummyBean());
|
||||
$transactionModel->id = 1;
|
||||
$transactionModel->status = Model_Transaction::STATUS_PROCESSED;
|
||||
$transactionModel->error = null;
|
||||
|
||||
$dbMock = Mockery::mock('\Box_Database');
|
||||
$dbMock->shouldReceive('getExistingModelById')
|
||||
->with('Transaction', 1)
|
||||
->andReturn($transactionModel);
|
||||
$dbMock->shouldNotReceive('store');
|
||||
|
||||
$di = container();
|
||||
$di['db'] = $dbMock;
|
||||
|
||||
$service = Mockery::mock(ServiceTransaction::class)->makePartial();
|
||||
$service->shouldReceive('create')->once()->andReturn(1);
|
||||
$service->shouldNotReceive('processTransaction');
|
||||
$service->setDi($di);
|
||||
|
||||
$result = $service->createAndProcess([]);
|
||||
|
||||
expect($result)->toBe(1);
|
||||
});
|
||||
|
||||
test('preProcessTransaction marks error on a generic exception', function (): void {
|
||||
$transactionModel = new Model_Transaction();
|
||||
$transactionModel->loadBean(new Tests\Helpers\DummyBean());
|
||||
$transactionModel->id = 5;
|
||||
$transactionModel->status = Model_Transaction::STATUS_PROCESSING;
|
||||
|
||||
$dbMock = Mockery::mock('\Box_Database');
|
||||
$dbMock->shouldReceive('load')
|
||||
->with('Transaction', 5)
|
||||
->andReturn($transactionModel);
|
||||
$dbMock->shouldReceive('store')
|
||||
->with($transactionModel)
|
||||
->once();
|
||||
|
||||
$eventsMock = Mockery::mock('\Box_EventManager');
|
||||
$eventsMock->shouldNotReceive('fire');
|
||||
|
||||
$di = container();
|
||||
$di['db'] = $dbMock;
|
||||
$di['events_manager'] = $eventsMock;
|
||||
$di['logger'] = new Tests\Helpers\TestLogger();
|
||||
|
||||
$service = Mockery::mock(ServiceTransaction::class)->makePartial();
|
||||
$service->shouldReceive('processTransaction')
|
||||
->with(5)
|
||||
->once()
|
||||
->andThrow(new RuntimeException('Unexpected DB error'));
|
||||
$service->setDi($di);
|
||||
|
||||
$thrown = null;
|
||||
|
||||
try {
|
||||
$service->preProcessTransaction($transactionModel);
|
||||
} catch (Throwable $e) {
|
||||
$thrown = $e;
|
||||
}
|
||||
|
||||
expect($thrown)->toBeInstanceOf(RuntimeException::class)
|
||||
->and($transactionModel->status)->toBe(Model_Transaction::STATUS_ERROR)
|
||||
->and($transactionModel->error)->toBe('Unexpected DB error');
|
||||
});
|
||||
|
||||
test('claimForProcessing includes error status in claim query', function (): void {
|
||||
$service = new ServiceTransaction();
|
||||
|
||||
$execArgs = [];
|
||||
$dbMock = Mockery::mock('\Box_Database');
|
||||
$dbMock->shouldReceive('exec')
|
||||
->withArgs(function (string $sql, array $bindings) use (&$execArgs): bool {
|
||||
$execArgs = ['sql' => $sql, 'bindings' => $bindings];
|
||||
|
||||
return true;
|
||||
})
|
||||
->once()
|
||||
->andReturn(1);
|
||||
|
||||
$di = container();
|
||||
$di['db'] = $dbMock;
|
||||
$service->setDi($di);
|
||||
|
||||
$result = $service->claimForProcessing(7);
|
||||
|
||||
expect($result)->toBeTrue()
|
||||
->and($execArgs['bindings'])->toContain(Model_Transaction::STATUS_ERROR)
|
||||
->and($execArgs['bindings'])->toContain(Model_Transaction::STATUS_RECEIVED)
|
||||
->and($execArgs['bindings'])->toContain(Model_Transaction::STATUS_PROCESSING)
|
||||
->and($execArgs['sql'])->toContain('IN (?, ?)');
|
||||
});
|
||||
|
||||
test('markTransactionError does not clobber an already processed transaction', function (): void {
|
||||
$transactionModel = new Model_Transaction();
|
||||
$transactionModel->loadBean(new Tests\Helpers\DummyBean());
|
||||
$transactionModel->id = 3;
|
||||
$transactionModel->status = Model_Transaction::STATUS_PROCESSED;
|
||||
|
||||
$dbMock = Mockery::mock('\Box_Database');
|
||||
$dbMock->shouldReceive('load')
|
||||
->with('Transaction', 3)
|
||||
->andReturn($transactionModel);
|
||||
$dbMock->shouldNotReceive('store');
|
||||
|
||||
$di = container();
|
||||
$di['db'] = $dbMock;
|
||||
|
||||
$service = new ServiceTransaction();
|
||||
$service->setDi($di);
|
||||
|
||||
$refl = new ReflectionClass($service);
|
||||
$method = $refl->getMethod('markTransactionError');
|
||||
$method->invoke($service, 3, new RuntimeException('late error'));
|
||||
|
||||
expect($transactionModel->status)->toBe(Model_Transaction::STATUS_PROCESSED);
|
||||
});
|
||||
Reference in new issue
Block a user