Update FetchNodeinfoPipeline, use more efficient dispatch

This commit is contained in:
Daniel Supernault 2024-02-07 02:51:50 -07:00
parent 289cad470b
commit ac01f51ab6
No known key found for this signature in database
GPG key ID: 23740873EE6F76A1

View file

@ -4,6 +4,7 @@ namespace App\Jobs\InstancePipeline;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique; use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
@ -12,45 +13,71 @@ use Illuminate\Support\Facades\Http;
use App\Instance; use App\Instance;
use App\Profile; use App\Profile;
use App\Services\NodeinfoService; use App\Services\NodeinfoService;
use Illuminate\Contracts\Cache\Repository;
use Illuminate\Support\Facades\Cache;
class FetchNodeinfoPipeline implements ShouldQueue class FetchNodeinfoPipeline implements ShouldQueue, ShouldBeUniqueUntilProcessing
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $instance; protected $instance;
/** /**
* Create a new job instance. * Create a new job instance.
* *
* @return void * @return void
*/ */
public function __construct(Instance $instance) public function __construct(Instance $instance)
{ {
$this->instance = $instance; $this->instance = $instance;
} }
/** /**
* Execute the job. * The number of seconds after which the job's unique lock will be released.
* *
* @return void * @var int
*/ */
public function handle() public $uniqueFor = 14400;
{
$instance = $this->instance;
$ni = NodeinfoService::get($instance->domain); /**
if($ni) { * Get the unique ID for the job.
if(isset($ni['software']) && is_array($ni['software']) && isset($ni['software']['name'])) { */
$software = $ni['software']['name']; public function uniqueId(): string
$instance->software = strtolower(strip_tags($software)); {
$instance->last_crawled_at = now(); return $this->instance->id;
$instance->user_count = Profile::whereDomain($instance->domain)->count(); }
$instance->save();
} /**
} else { * Execute the job.
$instance->user_count = Profile::whereDomain($instance->domain)->count(); *
$instance->last_crawled_at = now(); * @return void
$instance->save(); */
} public function handle()
} {
$instance = $this->instance;
if( $instance->nodeinfo_last_fetched &&
$instance->nodeinfo_last_fetched->gt(now()->subHours(12)) ||
$instance->delivery_timeout &&
$instance->delivery_next_after->gt(now())
) {
return;
}
$ni = NodeinfoService::get($instance->domain);
$instance->last_crawled_at = now();
if($ni) {
if(isset($ni['software']) && is_array($ni['software']) && isset($ni['software']['name'])) {
$software = $ni['software']['name'];
$instance->software = strtolower(strip_tags($software));
$instance->user_count = Profile::whereDomain($instance->domain)->count();
$instance->nodeinfo_last_fetched = now();
$instance->save();
}
} else {
$instance->delivery_timeout = 1;
$instance->delivery_next_after = now()->addHours(14);
$instance->save();
}
}
} }