Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use queued job for "unsearching" when Scout queue is enabled #471

Merged
merged 6 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/Jobs/RemoveFromSearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Laravel\Scout\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Queue\SerializesModels;

class RemoveFromSearch implements ShouldQueue
{
use Queueable, SerializesModels;

/**
* The models to be removed from the search index.
*
* @var \Illuminate\Database\Eloquent\Collection
*/
public $models;

/**
* Create a new job instance.
*
* @param \Illuminate\Database\Eloquent\Collection $models
* @return void
*/
public function __construct($models)
{
$this->models = $models;
}

/**
* Handle the job.
*
* @return void
*/
public function handle()
{
if ($this->models->isNotEmpty()) {
$this->models->first()->searchableUsing()->delete($this->models);
}
}

/**
* Restore a queueable collection instance.
*
* @param \Illuminate\Contracts\Database\ModelIdentifier $value
* @return \Illuminate\Database\Eloquent\Collection
*/
protected function restoreCollection($value)
{
if (! $value->class || count($value->id) === 0) {
return new EloquentCollection;
}

return new EloquentCollection(
collect($value->id)->map(function ($id) use ($value) {
return tap(new $value->class, function ($model) use ($id) {
$model->forceFill([$model->getKeyName() => $id]);
});
})
);
}
}
9 changes: 8 additions & 1 deletion src/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection as BaseCollection;
use Laravel\Scout\Jobs\MakeSearchable;
use Laravel\Scout\Jobs\RemoveFromSearch;

trait Searchable
{
Expand Down Expand Up @@ -80,7 +81,13 @@ public function queueRemoveFromSearch($models)
return;
}

return $models->first()->searchableUsing()->delete($models);
if (! config('scout.queue')) {
return $models->first()->searchableUsing()->delete($models);
}

dispatch(new RemoveFromSearch($models))
->onQueue($models->first()->syncWithSearchUsingQueue())
->onConnection($models->first()->syncWithSearchUsing());
}

/**
Expand Down
49 changes: 49 additions & 0 deletions tests/Unit/RemoveFromSearchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Laravel\Scout\Tests\Unit;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Config;
use Laravel\Scout\Jobs\RemoveFromSearch;
use Laravel\Scout\Tests\Fixtures\SearchableModel;
use Mockery as m;
use PHPUnit\Framework\TestCase;

class RemoveFromSearchTest extends TestCase
{
protected function setUp(): void
{
Config::shouldReceive('get')->with('scout.after_commit', m::any())->andReturn(false);
}

protected function tearDown(): void
{
m::close();
}

public function test_handle_passes_the_collection_to_engine()
{
$job = new RemoveFromSearch($collection = Collection::make([
$model = m::mock(),
]));

$model->shouldReceive('searchableUsing->delete')->with($collection);

$job->handle();
}

public function test_models_are_deserialized_without_the_database()
{
$job = new RemoveFromSearch($collection = Collection::make([
$model = new SearchableModel(['id' => 1234]),
]));

$job = unserialize(serialize($job));

$this->assertInstanceOf(Collection::class, $job->models);
$this->assertCount(1, $job->models);
$this->assertInstanceOf(SearchableModel::class, $job->models->first());
$this->assertTrue($model->is($job->models->first()));
$this->assertEquals(1234, $job->models->first()->getScoutKey());
}
}