2025-08-12 20:16:44 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
2026-03-08 17:53:43 +01:00
|
|
|
use App\Enums\LogLevelEnum;
|
|
|
|
|
use App\Events\ActionPerformed;
|
2025-08-12 20:16:44 +02:00
|
|
|
use App\Exceptions\PublishException;
|
|
|
|
|
use App\Models\Article;
|
2026-03-08 11:25:50 +01:00
|
|
|
use App\Models\ArticlePublication;
|
|
|
|
|
use App\Models\Setting;
|
2025-08-12 20:16:44 +02:00
|
|
|
use App\Services\Article\ArticleFetcher;
|
|
|
|
|
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
|
|
|
|
|
*/
|
2025-08-15 02:50:42 +02:00
|
|
|
public function handle(ArticleFetcher $articleFetcher, ArticlePublishingService $publishingService): 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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-12 20:16:44 +02:00
|
|
|
// Get the oldest approved article that hasn't been published yet
|
2025-08-12 20:55:57 +02:00
|
|
|
$article = Article::where('approval_status', 'approved')
|
2025-08-12 20:16:44 +02:00
|
|
|
->whereDoesntHave('articlePublication')
|
2025-08-12 21:07:46 +02:00
|
|
|
->oldest('created_at')
|
2025-08-12 20:16:44 +02:00
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
if (! $article) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
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-08 14:17:55 +01:00
|
|
|
'created_at' => $article->created_at,
|
2025-08-12 20:16:44 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// Fetch article data
|
2025-08-15 02:50:42 +02:00
|
|
|
$extractedData = $articleFetcher->fetchArticleData($article);
|
2025-08-12 20:16:44 +02:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$publishingService->publishToRoutedChannels($article, $extractedData);
|
2026-03-08 14:17:55 +01:00
|
|
|
|
2026-03-08 17:53:43 +01:00
|
|
|
ActionPerformed::dispatch('Successfully published article', LogLevelEnum::INFO, [
|
2025-08-12 20:16:44 +02:00
|
|
|
'article_id' => $article->id,
|
2026-03-08 14:17:55 +01:00
|
|
|
'title' => $article->title,
|
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
|
|
|
|
2025-08-12 20:16:44 +02:00
|
|
|
throw $e;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-08 14:17:55 +01:00
|
|
|
}
|