2016-01-17 16:16:16 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pony.fm - A community for pony fan music.
|
2021-02-14 03:39:15 +01:00
|
|
|
* Copyright (C) 2016 Feld0.
|
2016-01-17 16:16:16 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-02-14 03:34:58 +01:00
|
|
|
namespace App\Jobs;
|
2016-01-17 16:16:16 +01:00
|
|
|
|
2021-03-27 04:51:45 +01:00
|
|
|
use Elasticsearch;
|
|
|
|
use Elasticsearch\Common\Exceptions\Missing404Exception;
|
2016-01-17 16:16:16 +01:00
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
|
2016-09-30 00:26:31 +02:00
|
|
|
class UpdateSearchIndexForEntity extends Job implements ShouldQueue
|
2016-01-17 16:16:16 +01:00
|
|
|
{
|
2021-03-27 04:51:45 +01:00
|
|
|
use InteractsWithQueue;
|
2016-01-17 16:16:16 +01:00
|
|
|
|
2021-03-27 04:51:45 +01:00
|
|
|
protected array $elasticsearchBody;
|
|
|
|
protected bool $removeFromIndex;
|
2016-01-17 16:16:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
2021-03-27 04:51:45 +01:00
|
|
|
* @param array $elasticsearchBody
|
|
|
|
* @param bool $removeFromIndex
|
2016-01-17 16:16:16 +01:00
|
|
|
*/
|
2021-03-27 04:51:45 +01:00
|
|
|
public function __construct(array $elasticsearchBody, bool $removeFromIndex = true)
|
2016-01-17 16:16:16 +01:00
|
|
|
{
|
2021-03-27 04:51:45 +01:00
|
|
|
$this->elasticsearchBody = $elasticsearchBody;
|
|
|
|
$this->removeFromIndex = $removeFromIndex;
|
2016-01-17 16:16:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2016-02-22 05:47:52 +01:00
|
|
|
$this->beforeHandle();
|
2021-03-27 04:51:45 +01:00
|
|
|
|
|
|
|
match($this->removeFromIndex) {
|
|
|
|
true => $this->deleteElasticsearchEntry(),
|
|
|
|
false => $this->createOrUpdateElasticsearchEntry(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createOrUpdateElasticsearchEntry()
|
|
|
|
{
|
|
|
|
Elasticsearch::connection()->index($this->elasticsearchBody);
|
2016-01-17 16:16:16 +01:00
|
|
|
}
|
2021-03-27 04:51:45 +01:00
|
|
|
|
|
|
|
private function deleteElasticsearchEntry()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
Elasticsearch::connection()->delete($this->elasticsearchBody);
|
|
|
|
} catch (Missing404Exception $e) {
|
|
|
|
// If the entity we're trying to delete isn't indexed in Elasticsearch,
|
|
|
|
// that's fine.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-17 16:16:16 +01:00
|
|
|
}
|