73 lines
2 KiB
PHP
73 lines
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(): void
|
||
|
|
{
|
||
|
|
// Get the oldest approved article that hasn't been published yet
|
||
|
|
$article = Article::where('is_approved', true)
|
||
|
|
->where('is_valid', true)
|
||
|
|
->whereDoesntHave('articlePublication')
|
||
|
|
->oldest('approved_at')
|
||
|
|
->first();
|
||
|
|
|
||
|
|
if (! $article) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
logger()->info('Publishing next article from scheduled job', [
|
||
|
|
'article_id' => $article->id,
|
||
|
|
'title' => $article->title,
|
||
|
|
'url' => $article->url,
|
||
|
|
'approved_at' => $article->approved_at
|
||
|
|
]);
|
||
|
|
|
||
|
|
// Fetch article data
|
||
|
|
$extractedData = ArticleFetcher::fetchArticleData($article);
|
||
|
|
|
||
|
|
/** @var ArticlePublishingService $publishingService */
|
||
|
|
$publishingService = resolve(ArticlePublishingService::class);
|
||
|
|
|
||
|
|
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;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|