108 lines
3.6 KiB
PHP
108 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Enums\LogLevelEnum;
|
|
use App\Enums\NotificationSeverityEnum;
|
|
use App\Enums\NotificationTypeEnum;
|
|
use App\Events\ActionPerformed;
|
|
use App\Exceptions\PublishException;
|
|
use App\Models\Article;
|
|
use App\Models\ArticlePublication;
|
|
use App\Models\Setting;
|
|
use App\Services\Article\ArticleFetcher;
|
|
use App\Services\Notification\NotificationService;
|
|
use App\Services\Publishing\ArticlePublishingService;
|
|
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Queue\Queueable;
|
|
|
|
class PublishNextArticleJob implements ShouldBeUnique, ShouldQueue
|
|
{
|
|
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, NotificationService $notificationService): void
|
|
{
|
|
$interval = Setting::getArticlePublishingInterval();
|
|
|
|
if ($interval > 0) {
|
|
$lastPublishedAt = ArticlePublication::max('published_at');
|
|
|
|
if ($lastPublishedAt && now()->diffInMinutes($lastPublishedAt, absolute: true) < $interval) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
ActionPerformed::dispatch('Publishing next article from scheduled job', LogLevelEnum::INFO, [
|
|
'article_id' => $article->id,
|
|
'title' => $article->title,
|
|
'url' => $article->url,
|
|
'created_at' => $article->created_at,
|
|
]);
|
|
|
|
try {
|
|
$extractedData = $articleFetcher->fetchArticleData($article);
|
|
$publications = $publishingService->publishToRoutedChannels($article, $extractedData);
|
|
|
|
if ($publications->isNotEmpty()) {
|
|
ActionPerformed::dispatch('Successfully published article', LogLevelEnum::INFO, [
|
|
'article_id' => $article->id,
|
|
'title' => $article->title,
|
|
]);
|
|
} else {
|
|
ActionPerformed::dispatch('No publications created for article', LogLevelEnum::WARNING, [
|
|
'article_id' => $article->id,
|
|
'title' => $article->title,
|
|
]);
|
|
|
|
$notificationService->send(
|
|
NotificationTypeEnum::PUBLISH_FAILED,
|
|
NotificationSeverityEnum::WARNING,
|
|
"Publish failed: {$article->title}",
|
|
'No publications were created for this article. Check channel routing configuration.',
|
|
$article,
|
|
);
|
|
}
|
|
} catch (PublishException $e) {
|
|
ActionPerformed::dispatch('Failed to publish article', LogLevelEnum::ERROR, [
|
|
'article_id' => $article->id,
|
|
'error' => $e->getMessage(),
|
|
]);
|
|
|
|
$notificationService->send(
|
|
NotificationTypeEnum::PUBLISH_FAILED,
|
|
NotificationSeverityEnum::ERROR,
|
|
"Publish failed: {$article->title}",
|
|
$e->getMessage(),
|
|
$article,
|
|
);
|
|
|
|
throw $e;
|
|
}
|
|
}
|
|
}
|