Skip to content
This repository was archived by the owner on May 30, 2023. It is now read-only.

Commit 0f7fdad

Browse files
author
Robert Kummer
committed
delete job on error
1 parent 350c539 commit 0f7fdad

File tree

1 file changed

+60
-51
lines changed

1 file changed

+60
-51
lines changed

app/Jobs/Solr/Optimize.php

+60-51
Original file line numberDiff line numberDiff line change
@@ -3,72 +3,81 @@
33
namespace Ipunkt\LaravelIndexer\Jobs\Solr;
44

55
use Carbon\Carbon;
6+
use Exception;
67
use Illuminate\Bus\Queueable;
78
use Illuminate\Contracts\Cache\Repository;
89
use Illuminate\Contracts\Queue\ShouldQueue;
910
use Illuminate\Foundation\Bus\Dispatchable;
1011
use Illuminate\Queue\InteractsWithQueue;
1112
use Illuminate\Queue\SerializesModels;
13+
use RuntimeException;
1214
use Solarium\Client;
1315
use Solarium\Exception\ExceptionInterface;
1416
use Solarium\Exception\HttpException;
1517

1618
class Optimize implements ShouldQueue
1719
{
18-
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
20+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
1921

20-
/**
21-
* @var string
22-
*/
23-
public $cacheKey;
22+
/**
23+
* @var string
24+
*/
25+
public $cacheKey;
2426

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+
}
3335

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+
}
5557

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+
}
6577

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+
}
7483
}

0 commit comments

Comments
 (0)