Skip to content

Commit 194bbb0

Browse files
committed
Add for you feed interest cronjob
1 parent e59231c commit 194bbb0

2 files changed

Lines changed: 69 additions & 11 deletions

File tree

app/Console/Commands/UpdateFeedInterestsCommand.php

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,102 @@
33
namespace App\Console\Commands;
44

55
use App\Jobs\UpdateUserInterestsJob;
6-
use App\Models\Profile;
6+
use App\Models\User;
7+
use App\Services\FrontendService;
78
use Illuminate\Console\Command;
9+
use Illuminate\Support\Facades\Cache;
810

911
class UpdateFeedInterestsCommand extends Command
1012
{
1113
protected $signature = 'feed:update-interests
1214
{--profile= : Specific profile ID to update}
13-
{--active-days=7 : Only update profiles active in last N days}';
15+
{--active-days=7 : Only update profiles active in last N days}
16+
{--cache-ttl=48 : Hours to cache completed updates (default 48)}
17+
{--force : Force update even if recently processed}';
1418

1519
protected $description = 'Update user interests for personalized feed recommendations';
1620

1721
public function handle(): int
1822
{
23+
$force = $this->option('force');
24+
$cacheTtl = (int) $this->option('cache-ttl') ?: 48;
25+
26+
$config = FrontendService::getCache();
27+
$enabled = data_get($config, 'fyf', false);
28+
29+
if (! $enabled) {
30+
return 0;
31+
}
32+
1933
if ($profileId = $this->option('profile')) {
20-
$this->info("Updating interests for profile {$profileId}...");
21-
UpdateUserInterestsJob::dispatch((int) $profileId);
34+
return $this->updateSingleProfile((int) $profileId, $force, $cacheTtl);
35+
}
36+
37+
return $this->updateBulkProfiles($force, $cacheTtl);
38+
}
39+
40+
protected function updateSingleProfile(int $profileId, bool $force, int $cacheTtl): int
41+
{
42+
$cacheKey = "user_interests_updated:{$profileId}";
43+
44+
if (! $force && Cache::has($cacheKey)) {
45+
$this->warn("Profile {$profileId} was recently updated. Use --force to override.");
2246

2347
return 0;
2448
}
2549

50+
$this->info("Updating interests for profile {$profileId}...");
51+
UpdateUserInterestsJob::dispatch($profileId);
52+
Cache::put($cacheKey, now()->timestamp, now()->addHours($cacheTtl));
53+
54+
return 0;
55+
}
56+
57+
protected function updateBulkProfiles(bool $force, int $cacheTtl): int
58+
{
2659
$activeDays = (int) $this->option('active-days') ?: 7;
2760

28-
$profiles = Profile::whereHas('user', function ($query) use ($activeDays) {
29-
$query->where('last_active_at', '>=', now()->subDays($activeDays));
30-
})->pluck('id');
61+
$profiles = User::where('status', 1)
62+
->where('last_active_at', '>=', now()->subDays($activeDays))
63+
->pluck('profile_id');
64+
65+
if ($profiles->isEmpty()) {
66+
$this->info('No active profiles found.');
67+
68+
return 0;
69+
}
70+
71+
$this->info("Found {$profiles->count()} active profiles (last {$activeDays} days)...");
3172

32-
$this->info("Queuing interest updates for {$profiles->count()} active profiles...");
73+
$queued = 0;
74+
$skipped = 0;
3375

3476
$bar = $this->output->createProgressBar($profiles->count());
3577
$bar->start();
3678

3779
foreach ($profiles as $profileId) {
38-
UpdateUserInterestsJob::dispatch((int) $profileId);
80+
$cacheKey = "user_interests_updated:{$profileId}";
81+
82+
if (! $force && Cache::has($cacheKey)) {
83+
$skipped++;
84+
} else {
85+
UpdateUserInterestsJob::dispatch((int) $profileId);
86+
Cache::put($cacheKey, now()->timestamp, now()->addHours($cacheTtl));
87+
$queued++;
88+
}
89+
3990
$bar->advance();
4091
}
4192

4293
$bar->finish();
43-
$this->newLine();
44-
$this->info('Done! Jobs queued for processing.');
94+
$this->newLine(2);
95+
96+
$this->info("✓ Queued: {$queued} profiles");
97+
if ($skipped > 0) {
98+
$this->info("⊘ Skipped: {$skipped} profiles (recently updated)");
99+
$this->comment(' Use --force to process all profiles regardless of cache');
100+
}
101+
$this->info(" Cache TTL: {$cacheTtl} hours");
45102

46103
return 0;
47104
}

routes/console.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Schedule::command('app:expire-user-register-verifications')->everyFiveMinutes()->onOneServer();
88
Schedule::command('horizon:snapshot')->hourly()->onOneServer();
99
Schedule::command('instances:update-stats --create-missing')->daily()->at('04:20')->onOneServer();
10+
Schedule::command('feed:update-interests')->hourly()->onOneServer();
1011

1112
if (config('loops.admin_dashboard.autoUpdate')) {
1213
Schedule::command('admin:refresh-dashboard-30d')->everyThirtyMinutes()->onOneServer();

0 commit comments

Comments
 (0)