BrokerPushLevelRecordSendJob.php
2.1 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
61
62
63
64
65
66
<?php
namespace App\Jobs;
use App\Enums\BrokerPushLevelRecordStatusEnum;
use App\Enums\BrokerPushMatchRecordStatusEnum;
use App\Models\BrokerPushLevelRecord;
use App\Models\BrokerPushLevelRecordItem;
use Illuminate\Bus\Batch;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\Middleware\WithoutOverlapping;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Bus;
class BrokerPushLevelRecordSendJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public BrokerPushLevelRecord $record;
public function middleware(): array
{
return [new WithoutOverlapping($this->record->getKey())];
}
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(BrokerPushLevelRecord $record)
{
$this->record = $record;
$this->queue = 'im';
}
/**
* Execute the job.
*
* @return void
* @throws \Throwable
*/
public function handle(): void
{
$this->record->update(['status' => BrokerPushLevelRecordStatusEnum::PROCESSING]);
$batch = Bus::batch([]);
BrokerPushLevelRecordItem::query()->where('record_id', $this->record->getKey())
->eachById(fn(BrokerPushLevelRecordItem $item) => $batch->add(new BrokerPushLevelRecordItemSendJob($this->record, $item)));
$batch->name('BrokerPushLevelRecordSend-' . $this->record->getKey())
->withOption('record_id', $this->record->getKey())
->finally(function (Batch $batch) {
$isSuccess = BrokerPushLevelRecordItem::query()->where('record_id', $batch->options['record_id'])->where('status', 2)->exists();
BrokerPushLevelRecord::query()->whereKey($batch->options['record_id'])->update([
'status' => $isSuccess ? BrokerPushLevelRecordStatusEnum::SUCCESS : BrokerPushMatchRecordStatusEnum::FAIL
]);
})
->allowFailures()->dispatch();
}
}