diff --git a/backend/app/Events/ArticleReadyToPublish.php b/backend/app/Events/ArticleReadyToPublish.php deleted file mode 100644 index 9bcb7a7..0000000 --- a/backend/app/Events/ArticleReadyToPublish.php +++ /dev/null @@ -1,18 +0,0 @@ -onQueue('publishing'); + } + + /** + * Execute the job. + * @throws PublishException + */ + public function handle(): void + { + // Get the oldest approved article that hasn't been published yet + $article = Article::where('is_approved', true) + ->where('is_valid', true) + ->whereDoesntHave('articlePublication') + ->oldest('approved_at') + ->first(); + + if (! $article) { + return; + } + + logger()->info('Publishing next article from scheduled job', [ + 'article_id' => $article->id, + 'title' => $article->title, + 'url' => $article->url, + 'approved_at' => $article->approved_at + ]); + + // Fetch article data + $extractedData = ArticleFetcher::fetchArticleData($article); + + /** @var ArticlePublishingService $publishingService */ + $publishingService = resolve(ArticlePublishingService::class); + + try { + $publishingService->publishToRoutedChannels($article, $extractedData); + + logger()->info('Successfully published article', [ + 'article_id' => $article->id, + 'title' => $article->title + ]); + } catch (PublishException $e) { + logger()->error('Failed to publish article', [ + 'article_id' => $article->id, + 'error' => $e->getMessage() + ]); + + throw $e; + } + } +} \ No newline at end of file diff --git a/backend/app/Jobs/PublishToLemmyJob.php b/backend/app/Jobs/PublishToLemmyJob.php deleted file mode 100644 index 9a436b1..0000000 --- a/backend/app/Jobs/PublishToLemmyJob.php +++ /dev/null @@ -1,38 +0,0 @@ -onQueue('publishing'); - } - - public function handle(): void - { - $extractedData = ArticleFetcher::fetchArticleData($this->article); - - /** @var ArticlePublishingService $publishingService */ - $publishingService = resolve(ArticlePublishingService::class); - - try { - $publishingService->publishToRoutedChannels($this->article, $extractedData); - } catch (PublishException $e) { - $this->fail($e); - } - } -} diff --git a/backend/app/Listeners/PublishApprovedArticle.php b/backend/app/Listeners/PublishApprovedArticle.php deleted file mode 100644 index 8ff3e31..0000000 --- a/backend/app/Listeners/PublishApprovedArticle.php +++ /dev/null @@ -1,27 +0,0 @@ -article; - - // Skip if already has publication (prevents duplicate processing) - if ($article->articlePublication()->exists()) { - return; - } - - // Only publish if the article is valid and approved - if ($article->isValid() && $article->isApproved()) { - event(new ArticleReadyToPublish($article)); - } - } -} diff --git a/backend/app/Listeners/PublishArticle.php b/backend/app/Listeners/PublishArticle.php deleted file mode 100644 index 8f37ce4..0000000 --- a/backend/app/Listeners/PublishArticle.php +++ /dev/null @@ -1,39 +0,0 @@ -article; - - if ($article->articlePublication()->exists()) { - logger()->info('Article already published, skipping job dispatch', [ - 'article_id' => $article->id, - 'url' => $article->url - ]); - - return; - } - - logger()->info('Article queued for publishing to Lemmy', [ - 'article_id' => $article->id, - 'url' => $article->url - ]); - - PublishToLemmyJob::dispatch($article); - } -} diff --git a/backend/app/Providers/AppServiceProvider.php b/backend/app/Providers/AppServiceProvider.php index 544ba68..1ac3ea4 100644 --- a/backend/app/Providers/AppServiceProvider.php +++ b/backend/app/Providers/AppServiceProvider.php @@ -30,16 +30,6 @@ public function boot(): void \App\Listeners\ValidateArticleListener::class, ); - Event::listen( - \App\Events\ArticleApproved::class, - \App\Listeners\PublishApprovedArticle::class, - ); - - Event::listen( - \App\Events\ArticleReadyToPublish::class, - \App\Listeners\PublishArticle::class, - ); - app()->make(ExceptionHandler::class) ->reportable(function (Throwable $e) { diff --git a/backend/routes/console.php b/backend/routes/console.php index cd608fd..68d174c 100644 --- a/backend/routes/console.php +++ b/backend/routes/console.php @@ -1,8 +1,15 @@ everyTenMinutes()->name('sync-lemmy-channel-posts'); + +Schedule::job(new PublishNextArticleJob) + ->everyFiveMinutes() + ->name('publish-next-article') + ->withoutOverlapping() + ->onOneServer();