2025-08-12 20:16:44 +02:00
|
|
|
<?php
|
|
|
|
|
|
2025-08-15 16:39:18 +02:00
|
|
|
namespace Domains\Platform\Jobs;
|
2025-08-12 20:16:44 +02:00
|
|
|
|
2025-08-15 16:39:18 +02:00
|
|
|
use Domains\Platform\Exceptions\PublishException;
|
|
|
|
|
use Domains\Article\Models\Article;
|
|
|
|
|
use Domains\Article\Services\ArticleFetcher;
|
|
|
|
|
use Domains\Platform\Services\Publishing\ArticlePublishingService;
|
2025-08-12 20:16:44 +02:00
|
|
|
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
|
use Illuminate\Foundation\Queue\Queueable;
|
|
|
|
|
|
|
|
|
|
class PublishNextArticleJob implements ShouldQueue, ShouldBeUnique
|
|
|
|
|
{
|
|
|
|
|
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.
|
|
|
|
|
* @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
|
|
|
{
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger()->info('Publishing next article from scheduled job', [
|
|
|
|
|
'article_id' => $article->id,
|
|
|
|
|
'title' => $article->title,
|
|
|
|
|
'url' => $article->url,
|
2025-08-12 21:07:46 +02: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);
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|