|
3 | 3 | namespace Ipunkt\LaravelIndexer\Jobs\Solr;
|
4 | 4 |
|
5 | 5 | use Carbon\Carbon;
|
| 6 | +use Exception; |
6 | 7 | use Illuminate\Bus\Queueable;
|
7 | 8 | use Illuminate\Contracts\Cache\Repository;
|
8 | 9 | use Illuminate\Contracts\Queue\ShouldQueue;
|
9 | 10 | use Illuminate\Foundation\Bus\Dispatchable;
|
10 | 11 | use Illuminate\Queue\InteractsWithQueue;
|
11 | 12 | use Illuminate\Queue\SerializesModels;
|
| 13 | +use RuntimeException; |
12 | 14 | use Solarium\Client;
|
13 | 15 | use Solarium\Exception\ExceptionInterface;
|
14 | 16 | use Solarium\Exception\HttpException;
|
15 | 17 |
|
16 | 18 | class Optimize implements ShouldQueue
|
17 | 19 | {
|
18 |
| - use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
| 20 | + use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
19 | 21 |
|
20 |
| - /** |
21 |
| - * @var string |
22 |
| - */ |
23 |
| - public $cacheKey; |
| 22 | + /** |
| 23 | + * @var string |
| 24 | + */ |
| 25 | + public $cacheKey; |
24 | 26 |
|
25 |
| - /** |
26 |
| - * Optimize constructor. |
27 |
| - * @param string $cacheKey |
28 |
| - */ |
29 |
| - public function __construct(string $cacheKey) |
30 |
| - { |
31 |
| - $this->cacheKey = $cacheKey; |
32 |
| - } |
| 27 | + /** |
| 28 | + * Optimize constructor. |
| 29 | + * @param string $cacheKey |
| 30 | + */ |
| 31 | + public function __construct(string $cacheKey) |
| 32 | + { |
| 33 | + $this->cacheKey = $cacheKey; |
| 34 | + } |
33 | 35 |
|
34 |
| - /** |
35 |
| - * Execute the job. |
36 |
| - * |
37 |
| - * @param Repository $cache |
38 |
| - * @param Client $client |
39 |
| - * @return void |
40 |
| - * @throws \RuntimeException |
41 |
| - */ |
42 |
| - public function handle(Repository $cache, Client $client) |
43 |
| - { |
44 |
| - if ($cache->has($this->cacheKey)) { |
45 |
| - /** @var Carbon $nextPossibleOptimize */ |
46 |
| - $nextPossibleOptimize = $cache->get($this->cacheKey); |
47 |
| - if ($nextPossibleOptimize instanceof Carbon |
48 |
| - && $nextPossibleOptimize->isFuture()) { |
49 |
| - // another modification call was done, so another optimize job was already set up |
50 |
| - // so kill this job without optimizing |
51 |
| - $this->job->delete(); |
52 |
| - return; |
53 |
| - } |
54 |
| - } |
| 36 | + /** |
| 37 | + * Execute the job. |
| 38 | + * |
| 39 | + * @param Repository $cache |
| 40 | + * @param Client $client |
| 41 | + * @return void |
| 42 | + * @throws \RuntimeException |
| 43 | + */ |
| 44 | + public function handle(Repository $cache, Client $client) |
| 45 | + { |
| 46 | + if ($cache->has($this->cacheKey)) { |
| 47 | + /** @var Carbon $nextPossibleOptimize */ |
| 48 | + $nextPossibleOptimize = $cache->get($this->cacheKey); |
| 49 | + if ($nextPossibleOptimize instanceof Carbon |
| 50 | + && $nextPossibleOptimize->isFuture()) { |
| 51 | + // another modification call was done, so another optimize job was already set up |
| 52 | + // so kill this job without optimizing |
| 53 | + $this->job->delete(); |
| 54 | + return; |
| 55 | + } |
| 56 | + } |
55 | 57 |
|
56 |
| - try { |
57 |
| - $update = $client->createUpdate(); |
58 |
| - $update->addOptimize(true, false); |
59 |
| - $result = $client->update($update); |
60 |
| - } catch (HttpException $e) { |
61 |
| - $errorMessage = $e->getMessage(); |
62 |
| - if (+$e->getCode() === 400) { |
63 |
| - $error = json_decode($e->getBody(), true); |
64 |
| - $errorMessage = array_get($error, 'error.msg', $e->getMessage()); |
| 58 | + try { |
| 59 | + $update = $client->createUpdate(); |
| 60 | + $update->addOptimize(true, false); |
| 61 | + $result = $client->update($update); |
| 62 | + } catch (HttpException $exception) { |
| 63 | + $errorMessage = $exception->getMessage(); |
| 64 | + if (+$exception->getCode() === 400) { |
| 65 | + $error = json_decode($exception->getBody(), true); |
| 66 | + $errorMessage = array_get($error, 'error.msg', $exception->getMessage()); |
| 67 | + } |
| 68 | + $this->deleteJob($exception); |
| 69 | + throw new RuntimeException($errorMessage, $exception->getCode(), $exception); |
| 70 | + } catch (ExceptionInterface $exception) { |
| 71 | + $this->deleteJob($exception); |
| 72 | + throw new RuntimeException('No optimize command could be sent to solr', $exception->getCode(), $exception); |
| 73 | + } catch (Exception $exception) { |
| 74 | + $this->deleteJob($exception); |
| 75 | + } |
| 76 | + } |
65 | 77 |
|
66 |
| - $this->job->failed($e); |
67 |
| - $this->job->delete(); |
68 |
| - } |
69 |
| - throw new \RuntimeException($errorMessage, $e->getCode(), $e); |
70 |
| - } catch (ExceptionInterface $e) { |
71 |
| - throw new \RuntimeException('No optimize command could be sent to solr', $e->getCode(), $e); |
72 |
| - } |
73 |
| - } |
| 78 | + private function deleteJob($exception) |
| 79 | + { |
| 80 | + $this->job->failed($exception); |
| 81 | + $this->job->delete(); |
| 82 | + } |
74 | 83 | }
|
0 commit comments