2025-08-12 20:16:44 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
2026-03-18 16:05:31 +01:00
|
|
|
use App\Enums\ApprovalStatusEnum;
|
2026-03-08 17:53:43 +01:00
|
|
|
use App\Enums\LogLevelEnum;
|
2026-03-09 21:20:41 +01:00
|
|
|
use App\Enums\NotificationSeverityEnum;
|
|
|
|
|
use App\Enums\NotificationTypeEnum;
|
2026-03-08 17:53:43 +01:00
|
|
|
use App\Events\ActionPerformed;
|
2025-08-12 20:16:44 +02:00
|
|
|
use App\Exceptions\PublishException;
|
2026-03-08 11:25:50 +01:00
|
|
|
use App\Models\ArticlePublication;
|
2026-03-18 16:05:31 +01:00
|
|
|
use App\Models\RouteArticle;
|
2026-03-08 11:25:50 +01:00
|
|
|
use App\Models\Setting;
|
2025-08-12 20:16:44 +02:00
|
|
|
use App\Services\Article\ArticleFetcher;
|
2026-03-09 21:20:41 +01:00
|
|
|
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;
|
|
|
|
|
|
2026-03-08 14:17:55 +01:00
|
|
|
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.
|
2026-03-08 14:17:55 +01:00
|
|
|
*
|
2025-08-12 20:16:44 +02:00
|
|
|
* @throws PublishException
|
|
|
|
|
*/
|
2026-03-09 21:20:41 +01:00
|
|
|
public function handle(ArticleFetcher $articleFetcher, ArticlePublishingService $publishingService, NotificationService $notificationService): void
|
2025-08-12 20:16:44 +02:00
|
|
|
{
|
2026-03-08 11:25:50 +01:00
|
|
|
$interval = Setting::getArticlePublishingInterval();
|
|
|
|
|
|
|
|
|
|
if ($interval > 0) {
|
|
|
|
|
$lastPublishedAt = ArticlePublication::max('published_at');
|
|
|
|
|
|
|
|
|
|
if ($lastPublishedAt && now()->diffInMinutes($lastPublishedAt, absolute: true) < $interval) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-18 16:05:31 +01:00
|
|
|
// 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();
|
|
|
|
|
|
2026-03-18 16:05:31 +01:00
|
|
|
if (! $routeArticle) {
|
2025-08-12 20:16:44 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-18 16:05:31 +01:00
|
|
|
$article = $routeArticle->article;
|
|
|
|
|
|
2026-03-08 17:53:43 +01:00
|
|
|
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,
|
2026-03-18 16:05:31 +01:00
|
|
|
'route' => $routeArticle->feed_id.'-'.$routeArticle->platform_channel_id,
|
2025-08-12 20:16:44 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
try {
|
2026-03-09 21:20:41 +01:00
|
|
|
$extractedData = $articleFetcher->fetchArticleData($article);
|
2026-03-18 16:05:31 +01:00
|
|
|
$publication = $publishingService->publishRouteArticle($routeArticle, $extractedData);
|
2026-03-08 14:17:55 +01:00
|
|
|
|
2026-03-18 16:05:31 +01:00
|
|
|
if ($publication) {
|
2026-03-09 21:20:41 +01:00
|
|
|
ActionPerformed::dispatch('Successfully published article', LogLevelEnum::INFO, [
|
|
|
|
|
'article_id' => $article->id,
|
|
|
|
|
'title' => $article->title,
|
|
|
|
|
]);
|
|
|
|
|
} else {
|
2026-03-18 16:05:31 +01:00
|
|
|
ActionPerformed::dispatch('No publication created for article', LogLevelEnum::WARNING, [
|
2026-03-09 21:20:41 +01:00
|
|
|
'article_id' => $article->id,
|
|
|
|
|
'title' => $article->title,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$notificationService->send(
|
|
|
|
|
NotificationTypeEnum::PUBLISH_FAILED,
|
|
|
|
|
NotificationSeverityEnum::WARNING,
|
|
|
|
|
"Publish failed: {$article->title}",
|
2026-03-18 16:05:31 +01:00
|
|
|
'No publication was created for this article. Check channel routing configuration.',
|
2026-03-09 21:20:41 +01:00
|
|
|
$article,
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-08-12 20:16:44 +02:00
|
|
|
} catch (PublishException $e) {
|
2026-03-08 17:53:43 +01:00
|
|
|
ActionPerformed::dispatch('Failed to publish article', LogLevelEnum::ERROR, [
|
2025-08-12 20:16:44 +02:00
|
|
|
'article_id' => $article->id,
|
2026-03-08 14:17:55 +01:00
|
|
|
'error' => $e->getMessage(),
|
2025-08-12 20:16:44 +02:00
|
|
|
]);
|
2026-03-08 14:17:55 +01:00
|
|
|
|
2026-03-09 21:20:41 +01: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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-08 14:17:55 +01:00
|
|
|
}
|