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

Commit 350c539

Browse files
author
Robert Kummer
committed
delete job on error
1 parent 3e533ca commit 350c539

File tree

3 files changed

+129
-102
lines changed

3 files changed

+129
-102
lines changed

app/Jobs/Items/CreateItem.php

+53-44
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,72 @@
22

33
namespace Ipunkt\LaravelIndexer\Jobs\Items;
44

5+
use Exception;
56
use Illuminate\Bus\Queueable;
6-
use Illuminate\Queue\SerializesModels;
7-
use Illuminate\Queue\InteractsWithQueue;
87
use Illuminate\Contracts\Queue\ShouldQueue;
98
use Illuminate\Foundation\Bus\Dispatchable;
9+
use Illuminate\Queue\InteractsWithQueue;
10+
use Illuminate\Queue\SerializesModels;
11+
use RuntimeException;
1012
use Solarium\Client;
1113
use Solarium\Exception\ExceptionInterface;
1214
use Solarium\Exception\HttpException;
1315

1416
class CreateItem implements ShouldQueue
1517
{
16-
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
18+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
1719

18-
/**
19-
* @var array
20-
*/
21-
private $data;
20+
/**
21+
* @var array
22+
*/
23+
private $data;
2224

23-
/**
24-
* Create a new job instance.
25-
*
26-
* @param array $data
27-
*/
28-
public function __construct(array $data)
29-
{
30-
$this->data = $data;
31-
}
25+
/**
26+
* Create a new job instance.
27+
*
28+
* @param array $data
29+
*/
30+
public function __construct(array $data)
31+
{
32+
$this->data = $data;
33+
}
3234

33-
/**
34-
* Execute the job.
35-
*
36-
* @param Client $client
37-
* @throws \Exception
38-
*/
39-
public function handle(Client $client)
40-
{
41-
// send data to solr
42-
try {
43-
$update = $client->createUpdate();
35+
/**
36+
* Execute the job.
37+
*
38+
* @param Client $client
39+
* @throws \Exception
40+
*/
41+
public function handle(Client $client)
42+
{
43+
// send data to solr
44+
try {
45+
$update = $client->createUpdate();
4446

45-
$doc = $update->createDocument($this->data);
46-
$update->addDocument($doc)
47-
->addCommit();
47+
$doc = $update->createDocument($this->data);
48+
$update->addDocument($doc)
49+
->addCommit();
4850

49-
$result = $client->update($update);
50-
} catch (HttpException $e) {
51-
$errorMessage = $e->getMessage();
52-
if (+$e->getCode() === 400) {
53-
$error = json_decode($e->getBody(), true);
54-
$errorMessage = array_get($error, 'error.msg', $e->getMessage());
51+
$result = $client->update($update);
52+
} catch (HttpException $exception) {
53+
$errorMessage = $exception->getMessage();
54+
if (+$exception->getCode() === 400) {
55+
$error = json_decode($exception->getBody(), true);
56+
$errorMessage = array_get($error, 'error.msg', $exception->getMessage());
57+
}
58+
$this->deleteJob($exception);
59+
throw new RuntimeException($errorMessage, $exception->getCode(), $exception);
60+
} catch (ExceptionInterface $exception) {
61+
$this->deleteJob($exception);
62+
throw new RuntimeException('Document could not be inserted to solr', $exception->getCode(), $exception);
63+
} catch (Exception $exception) {
64+
$this->deleteJob($exception);
65+
}
66+
}
5567

56-
$this->job->failed($e);
57-
$this->job->delete();
58-
}
59-
throw new \RuntimeException($errorMessage, $e->getCode(), $e);
60-
} catch (ExceptionInterface $e) {
61-
throw new \RuntimeException('Document could not be inserted to solr', $e->getCode(), $e);
62-
}
63-
}
68+
private function deleteJob($exception)
69+
{
70+
$this->job->failed($exception);
71+
$this->job->delete();
72+
}
6473
}

app/Jobs/Items/DeleteByQuery.php

+21-12
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33

44
namespace Ipunkt\LaravelIndexer\Jobs\Items;
55

6+
use Exception;
67
use Illuminate\Bus\Queueable;
78
use Illuminate\Contracts\Queue\ShouldQueue;
89
use Illuminate\Foundation\Bus\Dispatchable;
910
use Illuminate\Queue\InteractsWithQueue;
1011
use Illuminate\Queue\SerializesModels;
12+
use RuntimeException;
1113
use Solarium\Client;
1214
use Solarium\Exception\ExceptionInterface;
1315
use Solarium\Exception\HttpException;
@@ -46,18 +48,25 @@ public function handle(Client $client)
4648
->addCommit();
4749

4850
$result = $client->update($update);
49-
} catch (HttpException $e) {
50-
$errorMessage = $e->getMessage();
51-
if (+$e->getCode() === 400) {
52-
$error = json_decode($e->getBody(), true);
53-
$errorMessage = array_get($error, 'error.msg', $e->getMessage());
54-
55-
$this->job->failed($e);
56-
$this->job->delete();
51+
} catch (HttpException $exception) {
52+
$errorMessage = $exception->getMessage();
53+
if (+$exception->getCode() === 400) {
54+
$error = json_decode($exception->getBody(), true);
55+
$errorMessage = array_get($error, 'error.msg', $exception->getMessage());
5756
}
58-
throw new \RuntimeException($errorMessage, $e->getCode(), $e);
59-
} catch (ExceptionInterface $e) {
60-
throw new \RuntimeException('Documents for query could not be deleted on solr', $e->getCode(), $e);
57+
$this->deleteJob($exception);
58+
throw new RuntimeException($errorMessage, $exception->getCode(), $exception);
59+
} catch (ExceptionInterface $exception) {
60+
$this->deleteJob($exception);
61+
throw new RuntimeException('Documents for query could not be deleted on solr', $exception->getCode(), $exception);
62+
} catch (Exception $exception) {
63+
$this->deleteJob($exception);
6164
}
6265
}
63-
}
66+
67+
private function deleteJob($exception)
68+
{
69+
$this->job->failed($exception);
70+
$this->job->delete();
71+
}
72+
}

app/Jobs/Items/DeleteItem.php

+55-46
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,70 @@
22

33
namespace Ipunkt\LaravelIndexer\Jobs\Items;
44

5+
use Exception;
56
use Illuminate\Bus\Queueable;
67
use Illuminate\Contracts\Queue\ShouldQueue;
78
use Illuminate\Foundation\Bus\Dispatchable;
89
use Illuminate\Queue\InteractsWithQueue;
910
use Illuminate\Queue\SerializesModels;
11+
use RuntimeException;
1012
use Solarium\Client;
1113
use Solarium\Exception\ExceptionInterface;
1214
use Solarium\Exception\HttpException;
1315

1416
class DeleteItem implements ShouldQueue
1517
{
16-
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
17-
18-
/**
19-
* @var string|int
20-
*/
21-
private $id;
22-
23-
/**
24-
* Create a new job instance.
25-
*
26-
* @param string|int $id
27-
*/
28-
public function __construct($id)
29-
{
30-
$this->id = $id;
31-
}
32-
33-
/**
34-
* Execute the job.
35-
*
36-
* @param Client $client
37-
* @return void
38-
* @throws \RuntimeException
39-
*/
40-
public function handle(Client $client)
41-
{
42-
try {
43-
$update = $client->createUpdate();
44-
$update->addDeleteById($this->id)
45-
->addCommit();
46-
47-
$result = $client->update($update);
48-
} catch (HttpException $e) {
49-
$errorMessage = $e->getMessage();
50-
if (+$e->getCode() === 400) {
51-
$error = json_decode($e->getBody(), true);
52-
$errorMessage = array_get($error, 'error.msg', $e->getMessage());
53-
54-
$this->job->failed($e);
55-
$this->job->delete();
56-
}
57-
throw new \RuntimeException($errorMessage, $e->getCode(), $e);
58-
} catch (ExceptionInterface $e) {
59-
throw new \RuntimeException('Document could not be deleted on solr', $e->getCode(), $e);
60-
}
61-
}
18+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
19+
20+
/**
21+
* @var string|int
22+
*/
23+
private $id;
24+
25+
/**
26+
* Create a new job instance.
27+
*
28+
* @param string|int $id
29+
*/
30+
public function __construct($id)
31+
{
32+
$this->id = $id;
33+
}
34+
35+
/**
36+
* Execute the job.
37+
*
38+
* @param Client $client
39+
* @return void
40+
* @throws \RuntimeException
41+
*/
42+
public function handle(Client $client)
43+
{
44+
try {
45+
$update = $client->createUpdate();
46+
$update->addDeleteById($this->id)
47+
->addCommit();
48+
49+
$result = $client->update($update);
50+
} catch (HttpException $exception) {
51+
$errorMessage = $exception->getMessage();
52+
if (+$exception->getCode() === 400) {
53+
$error = json_decode($exception->getBody(), true);
54+
$errorMessage = array_get($error, 'error.msg', $exception->getMessage());
55+
}
56+
$this->deleteJob($exception);
57+
throw new RuntimeException($errorMessage, $exception->getCode(), $exception);
58+
} catch (ExceptionInterface $exception) {
59+
$this->deleteJob($exception);
60+
throw new RuntimeException('Document could not be deleted on solr', $exception->getCode(), $exception);
61+
} catch (Exception $exception) {
62+
$this->deleteJob($exception);
63+
}
64+
}
65+
66+
private function deleteJob($exception)
67+
{
68+
$this->job->failed($exception);
69+
$this->job->delete();
70+
}
6271
}

0 commit comments

Comments
 (0)