2022-12-07 06:41:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs\FollowPipeline;
|
|
|
|
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
2023-08-23 09:45:51 +00:00
|
|
|
use Illuminate\Queue\Middleware\WithoutOverlapping;
|
2022-12-07 06:41:05 +00:00
|
|
|
use App\Services\AccountService;
|
|
|
|
use App\Services\FollowerService;
|
|
|
|
use Cache;
|
|
|
|
use DB;
|
2023-08-23 09:45:51 +00:00
|
|
|
use Storage;
|
|
|
|
use App\Follower;
|
2022-12-07 06:41:05 +00:00
|
|
|
use App\Profile;
|
|
|
|
|
|
|
|
class FollowServiceWarmCache implements ShouldQueue
|
|
|
|
{
|
2023-03-03 11:08:29 +00:00
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
2022-12-07 06:41:05 +00:00
|
|
|
|
2023-03-03 11:08:29 +00:00
|
|
|
public $profileId;
|
|
|
|
public $tries = 5;
|
|
|
|
public $timeout = 5000;
|
|
|
|
public $failOnTimeout = false;
|
2022-12-07 06:41:05 +00:00
|
|
|
|
2023-08-23 09:45:51 +00:00
|
|
|
/**
|
|
|
|
* Get the middleware the job should pass through.
|
|
|
|
*
|
|
|
|
* @return array<int, object>
|
|
|
|
*/
|
|
|
|
public function middleware(): array
|
|
|
|
{
|
|
|
|
return [(new WithoutOverlapping($this->profileId))->dontRelease()];
|
|
|
|
}
|
|
|
|
|
2023-03-03 11:08:29 +00:00
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct($profileId)
|
|
|
|
{
|
|
|
|
$this->profileId = $profileId;
|
|
|
|
}
|
2022-12-07 06:41:05 +00:00
|
|
|
|
2023-03-03 11:08:29 +00:00
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
$id = $this->profileId;
|
2022-12-07 06:41:05 +00:00
|
|
|
|
2023-08-23 09:45:51 +00:00
|
|
|
if(Cache::has(FollowerService::FOLLOWERS_SYNC_KEY . $id) && Cache::has(FollowerService::FOLLOWING_SYNC_KEY . $id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-03 11:08:29 +00:00
|
|
|
$account = AccountService::get($id, true);
|
2022-12-07 06:41:05 +00:00
|
|
|
|
2023-03-03 11:08:29 +00:00
|
|
|
if(!$account) {
|
|
|
|
Cache::put(FollowerService::FOLLOWERS_SYNC_KEY . $id, 1, 604800);
|
|
|
|
Cache::put(FollowerService::FOLLOWING_SYNC_KEY . $id, 1, 604800);
|
|
|
|
return;
|
|
|
|
}
|
2022-12-07 06:41:05 +00:00
|
|
|
|
2023-08-23 09:45:51 +00:00
|
|
|
$hasFollowerPostProcessing = false;
|
|
|
|
$hasFollowingPostProcessing = false;
|
|
|
|
|
|
|
|
if(Follower::whereProfileId($id)->orWhere('following_id', $id)->count()) {
|
|
|
|
$following = [];
|
|
|
|
$followers = [];
|
2023-12-03 10:05:00 +00:00
|
|
|
foreach(Follower::where('following_id', $id)->orWhere('profile_id', $id)->lazyById(500) as $follow) {
|
2023-08-23 09:45:51 +00:00
|
|
|
if($follow->following_id != $id && $follow->profile_id != $id) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if($follow->profile_id == $id) {
|
|
|
|
$following[] = $follow->following_id;
|
|
|
|
} else {
|
|
|
|
$followers[] = $follow->profile_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(count($followers) > 100) {
|
|
|
|
// store follower ids and process in another job
|
|
|
|
Storage::put('follow-warm-cache/' . $id . '/followers.json', json_encode($followers));
|
|
|
|
$hasFollowerPostProcessing = true;
|
|
|
|
} else {
|
|
|
|
foreach($followers as $follower) {
|
|
|
|
FollowerService::add($follower, $id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(count($following) > 100) {
|
|
|
|
// store following ids and process in another job
|
|
|
|
Storage::put('follow-warm-cache/' . $id . '/following.json', json_encode($following));
|
|
|
|
$hasFollowingPostProcessing = true;
|
|
|
|
} else {
|
|
|
|
foreach($following as $following) {
|
|
|
|
FollowerService::add($id, $following);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-07 06:41:05 +00:00
|
|
|
|
2023-03-03 11:08:29 +00:00
|
|
|
Cache::put(FollowerService::FOLLOWERS_SYNC_KEY . $id, 1, 604800);
|
|
|
|
Cache::put(FollowerService::FOLLOWING_SYNC_KEY . $id, 1, 604800);
|
2022-12-07 06:41:05 +00:00
|
|
|
|
2023-03-03 11:08:29 +00:00
|
|
|
$profile = Profile::find($id);
|
|
|
|
if($profile) {
|
|
|
|
$profile->following_count = DB::table('followers')->whereProfileId($id)->count();
|
|
|
|
$profile->followers_count = DB::table('followers')->whereFollowingId($id)->count();
|
|
|
|
$profile->save();
|
|
|
|
}
|
2022-12-07 06:41:05 +00:00
|
|
|
|
2023-03-03 11:08:29 +00:00
|
|
|
AccountService::del($id);
|
2022-12-07 06:41:05 +00:00
|
|
|
|
2023-08-23 09:45:51 +00:00
|
|
|
if($hasFollowingPostProcessing) {
|
|
|
|
FollowServiceWarmCacheLargeIngestPipeline::dispatch($id, 'following')->onQueue('follow');
|
|
|
|
}
|
|
|
|
|
|
|
|
if($hasFollowerPostProcessing) {
|
|
|
|
FollowServiceWarmCacheLargeIngestPipeline::dispatch($id, 'followers')->onQueue('follow');
|
|
|
|
}
|
|
|
|
|
2023-03-03 11:08:29 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-12-07 06:41:05 +00:00
|
|
|
}
|