Add (serp ai gen)
Add (scheduler)
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace App\Console;
|
||||
|
||||
use App\Jobs\AISerpGenArticleJob;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
|
||||
|
||||
@@ -12,7 +14,21 @@ class Kernel extends ConsoleKernel
|
||||
*/
|
||||
protected function schedule(Schedule $schedule): void
|
||||
{
|
||||
// $schedule->command('inspire')->hourly();
|
||||
|
||||
// AI Gen Scheduler
|
||||
|
||||
$launched_date = Carbon::parse(config('platform.global.launched_epoch'));
|
||||
$days_since_launch = now()->diffInDays($launched_date) + 1;
|
||||
|
||||
$posts_to_generate = get_exponential_posts_gen_by_day($days_since_launch);
|
||||
$mins_betwween_posts = floor((24 * 60) / $posts_to_generate);
|
||||
|
||||
$schedule->call(function () {
|
||||
AISerpGenArticleJob::dispatch()->onQueue('default')->onConnection('default');
|
||||
|
||||
})->everyMinutes($mins_betwween_posts)->when(function () use ($mins_betwween_posts) {
|
||||
return now()->minute % $mins_betwween_posts === 0;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -11,3 +11,23 @@ function get_slack_channel_by_env($slack_channel = 'slack')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('get_exponential_posts_gen_by_day')) {
|
||||
|
||||
// Function to calculate the value for a given day
|
||||
function get_exponential_posts_gen_by_day($day, $period_days = 45)
|
||||
{
|
||||
|
||||
$min_value = 4;
|
||||
$max_value = 150;
|
||||
|
||||
$growthRate = log($max_value / $min_value) / $period_days; // Calculate the growth rate
|
||||
$value = round($min_value * exp($growthRate * $day));
|
||||
|
||||
if ($value > $max_value) {
|
||||
return $max_value;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
55
app/Jobs/AISerpGenArticleJob.php
Normal file
55
app/Jobs/AISerpGenArticleJob.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Jobs\Tasks\ParseNewsSerpDomainsTask;
|
||||
use App\Models\Category;
|
||||
use App\Models\SerpUrl;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class AISerpGenArticleJob implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
$category = Category::orderBy('serp_at', 'asc')
|
||||
->orWhereNull('serp_at')
|
||||
->first();
|
||||
|
||||
$news_serp_result = GetNewsSerpTask::handle($category, 'US');
|
||||
|
||||
if (is_null($news_serp_result)) {
|
||||
Log::error(json_encode($category->toArray()));
|
||||
throw Exception('Failed to execute GetNewsSerpTask');
|
||||
}
|
||||
|
||||
// We only take 1 record at a time
|
||||
if (ParseNewsSerpDomainsTask::handle($news_serp_result)) {
|
||||
$serp_url = SerpUrl::latest()->first();
|
||||
|
||||
if (is_null($serp_url)) {
|
||||
Log::error(json_encode($serp_url->toArray()));
|
||||
throw Exception('Failed to execute ParseNewsSerpDomainsTask');
|
||||
}
|
||||
|
||||
return GenerateArticleJob::dispatch($serp_url)->onQueue('default')->onConnection('default');
|
||||
}
|
||||
}
|
||||
}
|
||||
35
app/Jobs/PublishIndexPostJob.php
Normal file
35
app/Jobs/PublishIndexPostJob.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Models\Post;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class PublishIndexPostJob implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
protected $post;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*/
|
||||
public function __construct(Post $post)
|
||||
{
|
||||
$this->post = $post;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
if (! is_null($this->post)) {
|
||||
PublishIndexPostTask::handle($this->post);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
use App\Helpers\FirstParty\OSSUploader\OSSUploader;
|
||||
use App\Helpers\ThirdParty\DFS\SettingSerpLiveAdvanced;
|
||||
use App\Jobs\PublishIndexPostJob;
|
||||
use App\Models\NewsSerpResult;
|
||||
use DFSClientV3\DFSClient;
|
||||
use Exception;
|
||||
@@ -239,6 +240,8 @@ public static function handle($post)
|
||||
$post->status = 'publish';
|
||||
$post->save();
|
||||
|
||||
PublishIndexPostJob::dispatch($post)->onQueue('default')->onConnection('default');
|
||||
|
||||
return $post;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Jobs\Tasks;
|
||||
|
||||
use App\Helpers\FirstParty\OpenAI\OpenAI;
|
||||
use App\Jobs\GenerateArticleFeaturedImageJob;
|
||||
use App\Models\Author;
|
||||
use App\Models\Post;
|
||||
use App\Models\PostCategory;
|
||||
@@ -71,6 +72,8 @@ public static function handle(SerpUrl $serp_url)
|
||||
$post_category->category_id = $serp_url->category->id;
|
||||
|
||||
if ($post_category->save()) {
|
||||
GenerateArticleFeaturedImageJob::dispatch($post)->onQueue('default')->onConnection('default');
|
||||
|
||||
return self::saveAndReturnSerpProcessStatus($serp_url, 1);
|
||||
} else {
|
||||
return self::saveAndReturnSerpProcessStatus($serp_url, -5);
|
||||
|
||||
34
app/Jobs/Tasks/PublishIndexPostTask.php
Normal file
34
app/Jobs/Tasks/PublishIndexPostTask.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Tasks;
|
||||
|
||||
use App\Models\Post;
|
||||
use Exception;
|
||||
use IndexNow;
|
||||
use LaravelGoogleIndexing;
|
||||
|
||||
class PublishIndexPostTask
|
||||
{
|
||||
public static function handle(Post $post)
|
||||
{
|
||||
$post->published_at = now();
|
||||
|
||||
if ($post->save()) {
|
||||
if (app()->environment() == 'production') {
|
||||
$post_url = route('front.post', ['slug' => $post->slug]);
|
||||
|
||||
try {
|
||||
IndexNow::submit($post_url);
|
||||
} catch (Exception) {
|
||||
}
|
||||
|
||||
try {
|
||||
LaravelGoogleIndexing::create()->update($post_url);
|
||||
} catch (Exception) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -36,6 +36,7 @@ class Category extends Model
|
||||
'_lft' => 'int',
|
||||
'_rgt' => 'int',
|
||||
'parent_id' => 'int',
|
||||
'serp_at' => 'datetime',
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
@@ -46,6 +47,7 @@ class Category extends Model
|
||||
'_lft',
|
||||
'_rgt',
|
||||
'parent_id',
|
||||
'serp_at',
|
||||
];
|
||||
|
||||
protected static function boot()
|
||||
|
||||
Reference in New Issue
Block a user