Lae/app/Jobs/WorkOrder/PushWorkOrderJob.php

87 lines
2.4 KiB
PHP
Raw Normal View History

2022-08-19 09:51:52 +00:00
<?php
2023-01-13 14:11:56 +00:00
namespace App\Jobs\WorkOrder;
2022-08-19 09:51:52 +00:00
use App\Models\WorkOrder\WorkOrder;
2022-11-06 11:28:22 +00:00
use Illuminate\Bus\Queueable;
2022-08-19 09:51:52 +00:00
use Illuminate\Contracts\Queue\ShouldQueue;
2023-01-17 04:39:33 +00:00
use Illuminate\Http\Client\RequestException;
2022-11-06 11:28:22 +00:00
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
2022-09-08 16:12:02 +00:00
use Illuminate\Support\Facades\Log;
2022-12-28 13:17:54 +00:00
class PushWorkOrderJob implements ShouldQueue
2022-08-19 09:51:52 +00:00
{
2022-09-08 16:12:02 +00:00
use InteractsWithQueue, Queueable, SerializesModels;
2022-08-19 09:51:52 +00:00
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* @return void
*/
2022-12-31 11:28:21 +00:00
public function handle(): void
2022-08-19 09:51:52 +00:00
{
2023-01-17 04:39:33 +00:00
(new WorkOrder)->whereIn('status', ['pending'])->with(['module', 'user', 'host', 'replies'])->chunk(100, function ($workOrders) {
2022-08-19 09:51:52 +00:00
foreach ($workOrders as $workOrder) {
if ($workOrder->isPlatform()) {
if ($workOrder->status == 'pending') {
$workOrder->update(['status' => 'open']);
}
continue;
}
2022-08-19 09:51:52 +00:00
2022-11-23 13:15:10 +00:00
if ($workOrder->host) {
if ($workOrder->host->status === 'pending') {
continue;
}
2022-08-19 09:51:52 +00:00
}
2022-09-13 14:33:26 +00:00
if ($workOrder->status === 'error') {
2023-01-17 04:39:33 +00:00
continue;
2022-09-13 14:33:26 +00:00
}
2022-08-19 09:51:52 +00:00
$workOrder->status = 'open';
2023-01-17 04:39:33 +00:00
$success = false;
try {
$response = $workOrder->module->http()->retry(3, 100)->post('work-orders', $workOrder->toArray());
2023-01-30 16:14:07 +00:00
if (! $response->successful()) {
2023-01-17 04:39:33 +00:00
Log::warning('推送工单失败', [
'work_order_id' => $workOrder->id,
'response' => $response->body(),
]);
2023-01-17 04:39:59 +00:00
} else {
$success = true;
2023-01-17 04:39:33 +00:00
}
} catch (RequestException $e) {
Log::warning($e->getMessage());
2023-01-17 04:39:59 +00:00
}
2023-01-30 16:14:07 +00:00
if (! $success) {
2022-08-19 09:51:52 +00:00
$workOrder->status = 'error';
}
$workOrder->save();
2022-08-19 15:27:57 +00:00
}
});
2022-08-19 09:51:52 +00:00
2023-01-10 13:42:27 +00:00
(new \App\Models\WorkOrder\Reply)->where('is_pending', 1)->chunk(100, function ($replies) {
2022-08-19 15:27:57 +00:00
foreach ($replies as $reply) {
2023-01-10 13:42:27 +00:00
dispatch(new Reply($reply));
2022-08-19 09:51:52 +00:00
}
});
}
}