Lae/app/Jobs/Module/PushWorkOrder.php

81 lines
2.1 KiB
PHP
Raw Normal View History

2022-08-19 09:51:52 +00:00
<?php
2022-11-16 02:29:50 +00:00
namespace App\Jobs\Module;
2022-08-19 09:51:52 +00:00
2022-08-19 15:27:57 +00:00
use App\Models\WorkOrder\Reply;
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;
2022-11-06 11:28:22 +00:00
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http;
2022-09-08 16:12:02 +00:00
use Illuminate\Support\Facades\Log;
// use Illuminate\Contracts\Queue\ShouldBeUnique;
2022-08-19 09:51:52 +00:00
class PushWorkOrder implements ShouldQueue
{
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
*/
public function handle()
{
2022-09-08 16:12:02 +00:00
//
2022-08-19 15:27:57 +00:00
WorkOrder::whereIn('status', ['pending', 'error'])->with(['module', 'user', 'host', 'replies'])->chunk(100, function ($workOrders) {
2022-08-19 09:51:52 +00:00
foreach ($workOrders as $workOrder) {
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-11-23 13:15:10 +00:00
2022-09-13 14:33:26 +00:00
if ($workOrder->status === 'error') {
// 如果 created_at 超过 3 天 use Carbon
if (now()->diffInDays($workOrder->created_at) > 3) {
$workOrder->delete();
continue;
}
}
2022-08-19 09:51:52 +00:00
$workOrder->status = 'open';
2022-11-30 07:57:31 +00:00
$response = $workOrder->module->http()->post('work-orders', $workOrder->toArray());
2022-08-19 09:51:52 +00:00
if (!$response->successful()) {
2022-09-08 16:12:02 +00:00
Log::error('推送工单失败', [
'work_order_id' => $workOrder->id,
'response' => $response->body(),
]);
2022-08-19 09:51:52 +00:00
$workOrder->status = 'error';
}
$workOrder->save();
2022-09-08 16:12:02 +00:00
2022-08-19 15:27:57 +00:00
}
});
2022-08-19 09:51:52 +00:00
2022-08-19 15:27:57 +00:00
Reply::where('is_pending', 1)->chunk(100, function ($replies) {
foreach ($replies as $reply) {
2022-11-16 02:29:50 +00:00
dispatch(new \App\Jobs\Module\WorkOrder\Reply($reply));
2022-08-19 09:51:52 +00:00
}
});
}
}