trypost/app/Console/Commands/ProcessScheduledPosts.php

34 lines
909 B
PHP
Raw Permalink Normal View History

2026-01-15 01:13:44 +00:00
<?php
declare(strict_types=1);
2026-01-19 00:49:13 +00:00
namespace App\Console\Commands;
2026-01-15 01:13:44 +00:00
use App\Enums\Post\Status as PostStatus;
2026-01-19 00:49:13 +00:00
use App\Jobs\PublishPost;
2026-01-15 01:13:44 +00:00
use App\Models\Post;
2026-01-19 00:49:13 +00:00
use Illuminate\Console\Command;
2026-01-15 01:13:44 +00:00
2026-01-19 00:49:13 +00:00
class ProcessScheduledPosts extends Command
2026-01-15 01:13:44 +00:00
{
2026-01-19 00:49:13 +00:00
protected $signature = 'posts:process-scheduled';
protected $description = 'Process scheduled posts that are due for publishing';
2026-01-15 01:13:44 +00:00
public function handle(): void
{
Post::query()
->due()
->each(function (Post $post) {
// Atomically claim the post — only dispatch if we successfully change its status
$claimed = Post::where('id', $post->id)
->where('status', PostStatus::Scheduled)
->update(['status' => PostStatus::Publishing]);
if ($claimed) {
2026-01-15 01:13:44 +00:00
PublishPost::dispatch($post);
}
});
}
}