fedi-feed-router/app/Jobs/PublishNextArticleJob.php

115 lines
4 KiB
PHP
Raw Normal View History

2025-08-12 20:16:44 +02:00
<?php
namespace App\Jobs;
use App\Enums\ApprovalStatusEnum;
use App\Enums\LogLevelEnum;
use App\Enums\NotificationSeverityEnum;
use App\Enums\NotificationTypeEnum;
use App\Events\ActionPerformed;
2025-08-12 20:16:44 +02:00
use App\Exceptions\PublishException;
use App\Models\ArticlePublication;
use App\Models\RouteArticle;
use App\Models\Setting;
2025-08-12 20:16:44 +02:00
use App\Services\Article\ArticleFetcher;
use App\Services\Notification\NotificationService;
2025-08-12 20:16:44 +02:00
use App\Services\Publishing\ArticlePublishingService;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
class PublishNextArticleJob implements ShouldBeUnique, ShouldQueue
2025-08-12 20:16:44 +02:00
{
use Queueable;
/**
* The number of seconds after which the job's unique lock will be released.
*/
public int $uniqueFor = 300;
public function __construct()
{
$this->onQueue('publishing');
}
/**
* Execute the job.
*
2025-08-12 20:16:44 +02:00
* @throws PublishException
*/
public function handle(ArticleFetcher $articleFetcher, ArticlePublishingService $publishingService, NotificationService $notificationService): void
2025-08-12 20:16:44 +02:00
{
$interval = Setting::getArticlePublishingInterval();
if ($interval > 0) {
$lastPublishedAt = ArticlePublication::max('published_at');
if ($lastPublishedAt && now()->diffInMinutes($lastPublishedAt, absolute: true) < $interval) {
return;
}
}
// Get the oldest approved route_article that hasn't been published to its channel yet
$routeArticle = RouteArticle::where('approval_status', ApprovalStatusEnum::APPROVED)
->whereDoesntHave('article.articlePublications', function ($query) {
$query->whereColumn('article_publications.platform_channel_id', 'route_articles.platform_channel_id');
})
->oldest('route_articles.created_at')
->with(['article', 'platformChannel.platformInstance', 'platformChannel.activePlatformAccounts'])
2025-08-12 20:16:44 +02:00
->first();
if (! $routeArticle) {
2025-08-12 20:16:44 +02:00
return;
}
$article = $routeArticle->article;
ActionPerformed::dispatch('Publishing next article from scheduled job', LogLevelEnum::INFO, [
2025-08-12 20:16:44 +02:00
'article_id' => $article->id,
'title' => $article->title,
'url' => $article->url,
'route' => $routeArticle->feed_id.'-'.$routeArticle->platform_channel_id,
2025-08-12 20:16:44 +02:00
]);
try {
$extractedData = $articleFetcher->fetchArticleData($article);
$publication = $publishingService->publishRouteArticle($routeArticle, $extractedData);
if ($publication) {
ActionPerformed::dispatch('Successfully published article', LogLevelEnum::INFO, [
'article_id' => $article->id,
'title' => $article->title,
]);
} else {
ActionPerformed::dispatch('No publication created for article', LogLevelEnum::WARNING, [
'article_id' => $article->id,
'title' => $article->title,
]);
$notificationService->send(
NotificationTypeEnum::PUBLISH_FAILED,
NotificationSeverityEnum::WARNING,
"Publish failed: {$article->title}",
'No publication was created for this article. Check channel routing configuration.',
$article,
);
}
2025-08-12 20:16:44 +02:00
} catch (PublishException $e) {
ActionPerformed::dispatch('Failed to publish article', LogLevelEnum::ERROR, [
2025-08-12 20:16:44 +02:00
'article_id' => $article->id,
'error' => $e->getMessage(),
2025-08-12 20:16:44 +02:00
]);
$notificationService->send(
NotificationTypeEnum::PUBLISH_FAILED,
NotificationSeverityEnum::ERROR,
"Publish failed: {$article->title}",
$e->getMessage(),
$article,
);
2025-08-12 20:16:44 +02:00
throw $e;
}
}
}