Skip to content

Commit 0b7473b

Browse files
alekseykorostelevnqxcode
authored andcommitted
Added support for queue.
1 parent 73df5ec commit 0b7473b

File tree

8 files changed

+150
-26
lines changed

8 files changed

+150
-26
lines changed

src/Nqxcode/LuceneSearch/Console/RebuildCommand.php

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
use App;
44
use Config;
5+
use File;
56
use Illuminate\Console\Command;
67
use Nqxcode\LuceneSearch\Locker\Locker;
78
use Nqxcode\LuceneSearch\Search;
89
use Symfony\Component\Console\Helper\ProgressBar;
910
use Symfony\Component\Console\Input\InputOption;
1011
use Symfony\Component\Console\Output\NullOutput;
12+
use Queue;
1113

1214
class RebuildCommand extends Command
1315
{
@@ -68,14 +70,28 @@ private function rebuild()
6870
continue;
6971
}
7072

71-
$progress = new ProgressBar($this->getOutput(), $count);
73+
$chunkCount = Config::get('laravel-lucene-search::chunk');
74+
$progress = new ProgressBar($this->getOutput(), $count / $chunkCount);
7275
$progress->start();
7376

74-
$modelRepository->chunk(1000, function ($chunk) use ($progress) {
75-
foreach ($chunk as $model) {
76-
$this->search->update($model);
77-
$progress->advance();
77+
$modelRepository->chunk($chunkCount, function ($chunk) use ($progress) {
78+
$queue = Config::get('laravel-lucene-search::queue');
79+
if ($queue) {
80+
Queue::push(
81+
'Nqxcode\LuceneSearch\Job\MassUpdateSearchIndex',
82+
[
83+
'modelClass' => get_class($chunk[0]),
84+
'modelKeys' => $chunk->lists($chunk[0]->getKeyName()),
85+
],
86+
$queue);
87+
88+
} else {
89+
foreach ($chunk as $model) {
90+
$this->search->update($model);
91+
}
7892
}
93+
94+
$progress->advance();
7995
});
8096

8197
$progress->finish();
@@ -89,19 +105,20 @@ private function rebuild()
89105

90106
private function softRebuild()
91107
{
92-
$oldIndexPath = \Config::get('laravel-lucene-search::index.path');
108+
$oldIndexPath = Config::get('laravel-lucene-search::index.path');
93109
$newIndexPath = sys_get_temp_dir() . '/laravel-lucene-search/' . uniqid('index-', true);
94110

95-
\Config::set('laravel-lucene-search::index.path', $newIndexPath);
111+
Config::set('laravel-lucene-search::index.path', $newIndexPath);
96112

97113
$this->rebuild();
98114

99115
$this->search->destroyConnection();
100116

101-
\File::cleanDirectory($oldIndexPath);
102-
\File::copyDirectory($newIndexPath, $oldIndexPath);
117+
File::cleanDirectory($oldIndexPath);
118+
File::copyDirectory($newIndexPath, $oldIndexPath);
119+
File::cleanDirectory($newIndexPath);
103120

104-
\Config::set('laravel-lucene-search::index.path', $oldIndexPath);
121+
Config::set('laravel-lucene-search::index.path', $oldIndexPath);
105122

106123
}
107124

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php namespace Nqxcode\LuceneSearch\Job;
2+
3+
/**
4+
* Class DeleteSearchIndex
5+
*/
6+
class DeleteSearchIndex
7+
{
8+
public function fire($job, array $jobData)
9+
{
10+
$model = $jobData['modelClass']::find($jobData['modelKey']);
11+
if (!is_null($model)) {
12+
app('search')->delete($model);
13+
}
14+
15+
$job->delete();
16+
}
17+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php namespace Nqxcode\LuceneSearch\Job;
2+
3+
use App\Models\CatalogProduct;
4+
5+
/**
6+
* Class MassUpdateSearchIndex
7+
*/
8+
class MassUpdateSearchIndex
9+
{
10+
public function fire($job, array $jobData)
11+
{
12+
$modelClass = $jobData['modelClass'];
13+
$modelKeys = $jobData['modelKeys'];
14+
15+
foreach ($modelKeys as $modelKey) {
16+
$model = $modelClass::find($modelKey);
17+
if (!is_null($model)) {
18+
app('search')->update($model);
19+
}
20+
}
21+
22+
$job->delete();
23+
}
24+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php namespace Nqxcode\LuceneSearch\Job;
2+
3+
use App\Models\CatalogProduct;
4+
5+
/**
6+
* Class UpdateSearchIndex
7+
*/
8+
class UpdateSearchIndex
9+
{
10+
public function fire($job, array $jobData)
11+
{
12+
$model = $jobData['modelClass']::find($jobData['modelKey']);
13+
if (!is_null($model)) {
14+
app('search')->update($model);
15+
}
16+
17+
$job->delete();
18+
}
19+
}

src/Nqxcode/LuceneSearch/Model/SearchObserver.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php namespace Nqxcode\LuceneSearch\Model;
22

33
use App;
4+
use Queue;
45

56
/**
67
* Class SearchObserver
@@ -11,6 +12,9 @@ class SearchObserver
1112
/** @var bool */
1213
private static $enabled = true;
1314

15+
/** @var string|null */
16+
private static $queue = null;
17+
1418
/**
1519
* @param bool $enabled
1620
*/
@@ -19,17 +23,49 @@ public static function setEnabled($enabled)
1923
self::$enabled = $enabled;
2024
}
2125

26+
/**
27+
* @param bool $queue
28+
*/
29+
public static function setQueue($queue)
30+
{
31+
self::$queue = $queue;
32+
}
33+
2234
public function saved($model)
2335
{
2436
if (self::$enabled) {
25-
App::offsetGet('search')->update($model);
37+
if (self::$queue) {
38+
Queue::push(
39+
'Nqxcode\LuceneSearch\Job\UpdateSearchIndex',
40+
[
41+
'modelClass' => get_class($model),
42+
'modelKey' => $model->getKey()
43+
],
44+
self::$queue
45+
);
46+
47+
} else {
48+
App::offsetGet('search')->update($model);
49+
}
2650
}
2751
}
2852

2953
public function deleting($model)
3054
{
3155
if (self::$enabled) {
32-
App::offsetGet('search')->delete($model);
56+
if (self::$queue) {
57+
Queue::push(
58+
'Nqxcode\LuceneSearch\Job\DeleteSearchIndex',
59+
[
60+
'modelClass' => get_class($model),
61+
'modelKey' => $model->getKey()
62+
],
63+
self::$queue
64+
);
65+
66+
} else {
67+
App::offsetGet('search')->delete($model);
68+
}
3369
}
3470
}
3571
}

src/Nqxcode/LuceneSearch/ServiceProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Nqxcode\LuceneSearch\Analyzer\Stopwords\FilterFactory;
66
use Nqxcode\LuceneSearch\Index\Connection;
77
use Nqxcode\LuceneSearch\Model\Config as ModelsConfig;
8+
use Nqxcode\LuceneSearch\Model\SearchObserver;
89
use ZendSearch\Lucene\Analysis\Analyzer\Common\Utf8Num\CaseInsensitive;
910
use ZendSearch\Lucene\Search\QueryParser;
1011

@@ -30,6 +31,8 @@ class ServiceProvider extends \Illuminate\Support\ServiceProvider
3031
public function boot()
3132
{
3233
$this->package('nqxcode/laravel-lucene-search');
34+
35+
SearchObserver::setQueue(Config::get('laravel-lucene-search::queue'));
3336
}
3437

3538
/**

src/config/config.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,24 @@
5151

5252
'stopwords' => Nqxcode\LuceneSearch\Analyzer\Stopwords\Files::get(),
5353
],
54+
55+
/*
56+
|--------------------------------------------------------------------------
57+
| Queue for jobs.
58+
|--------------------------------------------------------------------------
59+
|
60+
| Define queue that will be used for jobs of search index updating.
61+
|
62+
*/
63+
'queue' => null,
64+
65+
/*
66+
|--------------------------------------------------------------------------
67+
| Chunk Size
68+
|--------------------------------------------------------------------------
69+
|
70+
| This option allow you to control the maximum chunk size when you are rebuild search index.
71+
|
72+
*/
73+
'chunk' => 50,
5474
];

tests/functional/Console/RebuildCommandTest.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,8 @@ public function getOutputDataProvider()
6262
return [
6363
[
6464
'Creating index for model: "tests\models\Product"
65-
0/13 [>---------------------------] 0%
66-
1/13 [==>-------------------------] 7%
67-
2/13 [====>-----------------------] 15%
68-
3/13 [======>---------------------] 23%
69-
4/13 [========>-------------------] 30%
70-
5/13 [==========>-----------------] 38%
71-
6/13 [============>---------------] 46%
72-
7/13 [===============>------------] 53%
73-
8/13 [=================>----------] 61%
74-
9/13 [===================>--------] 69%
75-
10/13 [=====================>------] 76%
76-
11/13 [=======================>----] 84%
77-
12/13 [=========================>--] 92%
78-
13/13 [============================] 100%
65+
0 [>---------------------------]
66+
1 [->--------------------------]
7967
8068
Creating index for model: "tests\models\Tool"
8169
No available models found.

0 commit comments

Comments
 (0)