Skip to content

Commit 5a53eca

Browse files
authored
Merge pull request #35 from justbetter/feature/bulk-stale
Bulk operations stale
2 parents b7103cc + ac51b41 commit 5a53eca

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

config/magento-stock.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@
1111

1212
/* Send stock updates using Magento 2's async endpoints, a configured message queue in Magento is required for this */
1313
'async' => false,
14+
15+
/* Number of hours before async bulk operations are considered stale and prices can be re-queued */
16+
'async_stale_hours' => 24,
1417
];

src/Actions/ProcessStocks.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,16 @@ public function process(): void
4141
$query->where('exists_in_magento', '=', true);
4242
})
4343
->whereDoesntHave('bulkOperations', function (Builder $query): void {
44+
$staleHours = config()->integer('magento-stock.async_stale_hours', 24);
45+
$staleThreshold = now()->subHours($staleHours);
46+
4447
$query
45-
->where('status', '=', OperationStatus::Open)
46-
->orWhereNull('status');
48+
->where(function (Builder $query): void {
49+
$query
50+
->where('status', '=', OperationStatus::Open)
51+
->orWhereNull('status');
52+
})
53+
->where('created_at', '>=', $staleThreshold);
4754
})
4855
->select(['id', 'sku'])
4956
->take($repository->updateLimit())

tests/Actions/ProcessStocksTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ public function it_does_not_dispatch_stocks_with_open_async_operations(): void
9898
$stock = Stock::query()->create([
9999
'sku' => '::sku_1::',
100100
'update' => true,
101+
'created_at' => now()->subMinutes(10),
101102
]);
102103

103104
/** @var BulkRequest $request */
@@ -137,6 +138,57 @@ public function it_does_not_dispatch_stocks_with_open_async_operations(): void
137138
});
138139
}
139140

141+
#[Test]
142+
public function it_dispatches_stocks_with_stale_async_operations(): void
143+
{
144+
Bus::fake();
145+
config()->set('magento-stock.async', true);
146+
config()->set('magento-stock.async_stale_hours', 24);
147+
148+
MagentoProduct::query()->create([
149+
'sku' => '::sku::',
150+
'exists_in_magento' => true,
151+
]);
152+
153+
/** @var Stock $stock */
154+
$stock = Stock::query()->create([
155+
'sku' => '::sku::',
156+
'update' => true,
157+
]);
158+
159+
/** @var BulkRequest $request */
160+
$request = BulkRequest::query()->create([
161+
'magento_connection' => '::magento-connection::',
162+
'store_code' => '::store-code::',
163+
'method' => 'POST',
164+
'path' => '::path::',
165+
'bulk_uuid' => '::bulk-uuid::',
166+
'request' => [
167+
[
168+
'call-1',
169+
],
170+
],
171+
'response' => [],
172+
'created_at' => now()->subHours(25),
173+
]);
174+
175+
$request->operations()->create([
176+
'operation_id' => 0,
177+
'subject_type' => $stock->getMorphClass(),
178+
'subject_id' => $stock->getKey(),
179+
'status' => OperationStatus::Open,
180+
'created_at' => now()->subHours(25),
181+
]);
182+
183+
/** @var ProcessStocks $action */
184+
$action = app(ProcessStocks::class);
185+
$action->process();
186+
187+
Bus::assertDispatched(UpdateStockAsyncJob::class, function (UpdateStockAsyncJob $job): bool {
188+
return $job->stocks->count() === 1 && $job->stocks->first()?->sku === '::sku::';
189+
});
190+
}
191+
140192
#[Test]
141193
public function it_does_not_dispatch_update_jobs_if_magento_is_unavailable(): void
142194
{

0 commit comments

Comments
 (0)