改进 工单推送
This commit is contained in:
parent
98dd634ea6
commit
286d28140b
@ -5,9 +5,9 @@
|
||||
use App\Models\WorkOrder\WorkOrder;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Http\Client\RequestException;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
// use Illuminate\Contracts\Queue\ShouldBeUnique;
|
||||
@ -34,7 +34,7 @@ public function __construct()
|
||||
public function handle(): void
|
||||
{
|
||||
//
|
||||
(new WorkOrder)->whereIn('status', ['pending', 'error'])->with(['module', 'user', 'host', 'replies'])->chunk(100, function ($workOrders) {
|
||||
(new WorkOrder)->whereIn('status', ['pending'])->with(['module', 'user', 'host', 'replies'])->chunk(100, function ($workOrders) {
|
||||
foreach ($workOrders as $workOrder) {
|
||||
|
||||
if ($workOrder->host) {
|
||||
@ -45,27 +45,29 @@ public function handle(): void
|
||||
|
||||
|
||||
if ($workOrder->status === 'error') {
|
||||
// 如果超过 3 次错误,使用 Redis
|
||||
$count = Cache::get('work_order_error_count_' . $workOrder->id, 0);
|
||||
if ($count > 3) {
|
||||
$workOrder->delete();
|
||||
continue;
|
||||
} else {
|
||||
Cache::increment('work_order_error_count_' . $workOrder->id);
|
||||
}
|
||||
}
|
||||
|
||||
$workOrder->status = 'open';
|
||||
|
||||
$response = $workOrder->module->http()->post('work-orders', $workOrder->toArray());
|
||||
|
||||
$success = false;
|
||||
|
||||
try {
|
||||
$response = $workOrder->module->http()->retry(3, 100)->post('work-orders', $workOrder->toArray());
|
||||
|
||||
if (!$response->successful()) {
|
||||
Log::error('推送工单失败', [
|
||||
Log::warning('推送工单失败', [
|
||||
'work_order_id' => $workOrder->id,
|
||||
'response' => $response->body(),
|
||||
]);
|
||||
$workOrder->status = 'error';
|
||||
}
|
||||
} catch (RequestException $e) {
|
||||
Log::warning($e->getMessage());
|
||||
$workOrder->status = 'error';
|
||||
}
|
||||
|
||||
|
||||
$workOrder->save();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user