69 lines
No EOL
2 KiB
PHP
69 lines
No EOL
2 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Exceptions\PublishException;
|
|
use App\Models\Article;
|
|
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;
|
|
|
|
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
|
|
*/
|
|
public function handle(ArticleFetcher $articleFetcher, ArticlePublishingService $publishingService): void
|
|
{
|
|
// Get the oldest approved article that hasn't been published yet
|
|
$article = Article::where('approval_status', 'approved')
|
|
->whereDoesntHave('articlePublication')
|
|
->oldest('created_at')
|
|
->first();
|
|
|
|
if (! $article) {
|
|
return;
|
|
}
|
|
|
|
logger()->info('Publishing next article from scheduled job', [
|
|
'article_id' => $article->id,
|
|
'title' => $article->title,
|
|
'url' => $article->url,
|
|
'created_at' => $article->created_at
|
|
]);
|
|
|
|
// Fetch article data
|
|
$extractedData = $articleFetcher->fetchArticleData($article);
|
|
|
|
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;
|
|
}
|
|
}
|
|
} |