-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFailOrReadyProcessingRequestInsurances.php
More file actions
60 lines (52 loc) · 1.89 KB
/
Copy pathFailOrReadyProcessingRequestInsurances.php
File metadata and controls
60 lines (52 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
namespace Cego\RequestInsurance\Commands;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Cego\RequestInsurance\Enums\State;
use Cego\RequestInsurance\Models\RequestInsurance;
class FailOrReadyProcessingRequestInsurances extends Command
{
/**
* Name and signature of the console command
*
* @var string
*/
protected $signature = 'request-insurance:unstuck-processing';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Sets requests that have been processing for at least 10 minutes into either failed or ready state';
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
$this->unstuckProcessingRequestInsurances();
return 0;
}
/**
* Updates state of request insurances that have been processing for more than 10 minutes and logs for each RI.
* Which state they are updated to is based whether it retries inconsistent states.
*
* @return void
*/
protected function unstuckProcessingRequestInsurances(): void
{
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) {
// State is updated based on retry_inconsistent
$stateChange = $requestInsurance->retry_inconsistent ? State::READY : State::FAILED;
$requestInsurance->update(['state' => $stateChange, 'state_changed_at' => Carbon::now('UTC')]);
Log::info("Request insurance with id $requestInsurance->id was updated to $stateChange due to processing for too long.");
});
}
}