From fd185ab8131d80f9a4a5f3ac27f2508b54f330c6 Mon Sep 17 00:00:00 2001 From: m105 Date: Tue, 21 Jul 2026 19:29:44 +0300 Subject: [PATCH] Guard PublishPostJob against duplicate/stale dispatches Skip if the post is no longer status=publishing when the job runs, so a stray duplicate (e.g. clicking Publish twice while no worker was running) can't publish the same post to WordPress a second time. Also root-caused a stuck-in-"publishing" post reported live: the queue worker started manually last session was a bare background process, not a real service, and had died. Replaced it with a systemd unit (muninn-queue-worker.service, enabled + running) so the Publish button keeps working across reboots/restarts. Co-Authored-By: Claude Sonnet 5 --- app/Jobs/PublishPostJob.php | 7 +++++++ tests/Feature/Jobs/PublishPostJobTest.php | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/app/Jobs/PublishPostJob.php b/app/Jobs/PublishPostJob.php index 0172eef..8221846 100644 --- a/app/Jobs/PublishPostJob.php +++ b/app/Jobs/PublishPostJob.php @@ -27,6 +27,13 @@ class PublishPostJob implements ShouldQueue return; } + // A duplicate/stale job for a post already resolved by an earlier + // run of this same job (e.g. a double-dispatch while no worker was + // running) — skip it rather than risk publishing twice. + if ($post->status !== 'publishing') { + return; + } + $publisher = $publishers->resolve($post); if (! $publisher) { diff --git a/tests/Feature/Jobs/PublishPostJobTest.php b/tests/Feature/Jobs/PublishPostJobTest.php index 293a400..d86b806 100644 --- a/tests/Feature/Jobs/PublishPostJobTest.php +++ b/tests/Feature/Jobs/PublishPostJobTest.php @@ -60,6 +60,20 @@ test('it reverts to draft when no publisher supports the post type', function () ->publish_error->toContain('No publisher available'); }); +test('it skips a duplicate job for a post no longer in publishing state', function () { + Http::fake([ + '*/wp-json/wp/v2/posts' => Http::response(['link' => 'https://wp.example.com/post'], 201), + ]); + + $site = Site::factory()->wordpress()->create(); + $post = Post::factory()->create(['site_id' => $site->id, 'type' => 'blog', 'title' => 'Hi', 'status' => 'published', 'published_url' => 'https://wp.example.com/already-there']); + + (new PublishPostJob($post->id))->handle(app(PublisherManager::class)); + + Http::assertNothingSent(); + expect($post->fresh()->published_url)->toBe('https://wp.example.com/already-there'); +}); + test('it publishes a reddit post via the sidecar', function () { Process::fake([ '*' => Process::result(output: json_encode(['success' => true, 'url' => 'https://reddit.com/r/test/comments/abc'])),