Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public function handle(): int
*/
protected function unstuckProcessingRequestInsurances(): void
{
RequestInsurance::query()->where('state', State::PROCESSING)
RequestInsurance::query()
->forceIndex('request_insurances_state_priority_index')
->where('state', State::PROCESSING)
->where('state_changed_at', '<', Carbon::now('UTC')->subMinutes(10))
->cursor()
->each(function (RequestInsurance $requestInsurance) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class UnlockBlockedRequestInsurances extends Command
public function handle(): int
{
RequestInsurance::query()
->forceIndex('request_insurances_state_priority_index')
->where('state', State::PENDING)
->where('state_changed_at', '<', Carbon::now('UTC')->subMinutes(5))
->update(['state' => State::READY, 'state_changed_at' => Carbon::now('UTC')]);
Expand Down
10 changes: 10 additions & 0 deletions src/RequestInsurance/Models/RequestInsurance.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use GuzzleHttp\TransferStats;
use Cego\RequestInsurance\Events;
use Illuminate\Support\Enumerable;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\Crypt;
Expand Down Expand Up @@ -580,6 +581,15 @@ protected function usesEncryption(): bool
return $this->encrypted_fields != null;
}

public function scopeForceIndex(Builder $query, string $indexName)
{
if ($this->getConnection()->getDriverName() !== 'mysql') {
return $query;
}

return $query->from(DB::raw(sprintf('`%s` FORCE INDEX (`%s`)', $this->getTable(), $indexName)));
}

/**
* Query scope for getting all RequestInsurances that are ready to be processed
*
Expand Down
2 changes: 2 additions & 0 deletions src/RequestInsurance/RequestInsuranceWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ protected function atMostOnceEverySecond(Closure $closure): void
protected function readyWaitingRequestInsurances(): void
{
RequestInsurance::query()
->forceIndex('covering_index')
->where('state', State::WAITING)
->where('retry_at', '<=', Carbon::now('UTC'))
->update(['state' => State::READY, 'state_changed_at' => Carbon::now('UTC'), 'retry_at' => null]);
Expand Down Expand Up @@ -425,6 +426,7 @@ public function acquireLockOnRowsToProcess(): Collection
public function getIdsOfReadyRequests()
{
$builder = resolve(RequestInsurance::class)::query()
->forceIndex('request_insurances_state_priority_index')
->select('id')
->readyToBeProcessed()
->take(Config::get('request-insurance.batchSize'));
Expand Down
Loading