where('status', PostStatus::Publishing) ->where('updated_at', '<=', now()->subHour()) ->each(function (Post $post) use (&$count) { // Mark stuck platforms as failed $post->postPlatforms() ->where('enabled', true) ->whereIn('status', [PlatformStatus::Publishing, PlatformStatus::Pending]) ->where('updated_at', '<=', now()->subHour()) ->update([ 'status' => PlatformStatus::Failed, 'error_message' => 'Publishing timed out. Please try again.', 'error_context' => json_encode([ 'category' => 'timeout', 'failed_at' => now()->toIso8601String(), ]), ]); // Recalculate post status $enabledPlatforms = $post->postPlatforms()->where('enabled', true)->get(); $total = $enabledPlatforms->count(); $publishedCount = $enabledPlatforms->where('status', PlatformStatus::Published)->count(); $failedCount = $enabledPlatforms->where('status', PlatformStatus::Failed)->count(); if ($publishedCount === $total) { $post->markAsPublished(); } elseif ($publishedCount > 0) { $post->markAsPartiallyPublished(); } else { $post->markAsFailed(); } $count++; }); $this->info("Recovered {$count} stuck posts."); } }