فهرست منبع

Update AP helpers, more efficently update post counts

Daniel Supernault 1 سال پیش
والد
کامیت
7caed381fb

+ 57 - 0
app/Console/Commands/AccountPostCountStatUpdate.php

@@ -0,0 +1,57 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use App\Services\AccountService;
+use App\Services\Account\AccountStatService;
+use App\Status;
+use App\Profile;
+
+class AccountPostCountStatUpdate extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'app:account-post-count-stat-update';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Update post counts from recent activities';
+
+    /**
+     * Execute the console command.
+     */
+    public function handle()
+    {
+        $ids = AccountStatService::getAllPostCountIncr();
+        if(!$ids || !count($ids)) {
+            return;
+        }
+        foreach($ids as $id) {
+            $acct = AccountService::get($id, true);
+            if(!$acct) {
+                AccountStatService::removeFromPostCount($id);
+                continue;
+            }
+            $statusCount = Status::whereProfileId($id)->count();
+            if($statusCount != $acct['statuses_count']) {
+                $profile = Profile::find($id);
+                if(!$profile) {
+                    AccountStatService::removeFromPostCount($id);
+                    continue;
+                }
+                $profile->status_count = $statusCount;
+                $profile->save();
+                AccountService::del($id);
+            }
+            AccountStatService::removeFromPostCount($id);
+        }
+        return;
+    }
+}

+ 2 - 1
app/Console/Kernel.php

@@ -38,7 +38,7 @@ class Kernel extends ConsoleKernel
         }
 
         if(config('import.instagram.enabled')) {
-            $schedule->command('app:transform-imports')->everyFourMinutes();
+            $schedule->command('app:transform-imports')->everyTenMinutes();
             $schedule->command('app:import-upload-garbage-collection')->hourlyAt(51);
             $schedule->command('app:import-remove-deleted-accounts')->hourlyAt(37);
             $schedule->command('app:import-upload-clean-storage')->twiceDailyAt(1, 13, 32);
@@ -49,6 +49,7 @@ class Kernel extends ConsoleKernel
         }
         $schedule->command('app:notification-epoch-update')->weeklyOn(1, '2:21');
         $schedule->command('app:hashtag-cached-count-update')->hourlyAt(25);
+        $schedule->command('app:account-post-count-stat-update')->everySixHours(25);
     }
 
     /**

+ 26 - 0
app/Services/Account/AccountStatService.php

@@ -0,0 +1,26 @@
+<?php
+
+namespace App\Services\Account;
+
+use Illuminate\Support\Facades\Cache;
+use Illuminate\Support\Facades\Redis;
+
+class AccountStatService
+{
+    const REFRESH_CACHE_KEY = 'pf:services:accountstats:refresh:daily';
+
+    public static function incrementPostCount($pid)
+    {
+        return Redis::zadd(self::REFRESH_CACHE_KEY, $pid, $pid);
+    }
+
+    public static function removeFromPostCount($pid)
+    {
+        return Redis::zrem(self::REFRESH_CACHE_KEY, $pid);
+    }
+
+    public static function getAllPostCountIncr($limit = -1)
+    {
+        return Redis::zrange(self::REFRESH_CACHE_KEY, 0, $limit);
+    }
+}

+ 2 - 1
app/Util/ActivityPub/Helpers.php

@@ -43,6 +43,7 @@ use App\Jobs\ProfilePipeline\IncrementPostCount;
 use App\Jobs\ProfilePipeline\DecrementPostCount;
 use App\Services\DomainService;
 use App\Services\UserFilterService;
+use App\Services\Account\AccountStatService;
 
 class Helpers {
 
@@ -536,7 +537,7 @@ class Helpers {
             }
         }
 
-        IncrementPostCount::dispatch($pid)->onQueue('low');
+        AccountStatService::incrementPostCount($pid);
 
         if( $status->in_reply_to_id === null &&
             in_array($status->type, ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])