Skip to content

Commit 2201ddd

Browse files
author
Jan Petr
authored
Merge pull request #916 from algolia/develop
Develop
2 parents 79fc7a6 + b18bb5d commit 2201ddd

File tree

16 files changed

+269
-74
lines changed

16 files changed

+269
-74
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## CHANGE LOG
22

3+
### 1.11.1
4+
5+
- Query rules are preserved during reindex with indexing queue enabled (#913)
6+
- Information about the indexing queue is displayed in admin-wide notifications (#905)
7+
- Information about queue processing are logged to `algoliasearch_queue_log` DB table (#907)
8+
39
### 1.11.0
410

511
## FEATURES

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Algolia Search for Magento 1.6+
22
==================
33

4-
![Latest version](https://img.shields.io/badge/latest-1.11.0-green.svg)
4+
![Latest version](https://img.shields.io/badge/latest-1.11.1-green.svg)
55

66
[![Build Status](https://travis-ci.org/algolia/algoliasearch-magento.svg?branch=master)](https://travis-ci.org/algolia/algoliasearch-magento)
77
![PHP >= 5.3](https://img.shields.io/badge/php-%3E=5.3-green.svg)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
class Algolia_Algoliasearch_Block_Adminhtml_Notifications extends Mage_Adminhtml_Block_Template
4+
{
5+
public function getConfigurationUrl()
6+
{
7+
return $this->getUrl('adminhtml/system_config/edit/section/algoliasearch');
8+
}
9+
10+
public function getQueueInfo()
11+
{
12+
/** @var Algolia_Algoliasearch_Helper_Config $config */
13+
$config = Mage::helper('algoliasearch/config');
14+
15+
/** @var Mage_Core_Model_Resource $resource */
16+
$resource = Mage::getSingleton('core/resource');
17+
$tableName = $resource->getTableName('algoliasearch/queue');
18+
19+
$readConnection = $resource->getConnection('core_read');
20+
21+
$size = (int)$readConnection->query('SELECT COUNT(*) as total_count FROM '.$tableName)->fetchColumn(0);
22+
$maxJobsPerSingleRun = $config->getNumberOfJobToRun();
23+
24+
$etaMinutes = ceil($size / $maxJobsPerSingleRun) * 5; // 5 - assuming the queue runner runs every 5 minutes
25+
26+
$eta = $etaMinutes.' minutes';
27+
if ($etaMinutes > 60) {
28+
$hours = floor($etaMinutes / 60);
29+
$restMinutes = $etaMinutes % 60;
30+
31+
$eta = $hours.' hours '.$restMinutes.' minutes';
32+
}
33+
34+
$queueInfo = array(
35+
'isEnabled' => $config->isQueueActive(),
36+
'currentSize' => $size,
37+
'eta' => $eta,
38+
);
39+
40+
return $queueInfo;
41+
}
42+
}

app/code/community/Algolia/Algoliasearch/Helper/Algoliahelper.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,40 @@ public function copySynonyms($fromIndexName, $toIndexName)
217217
$this->lastTaskId = $res['taskID'];
218218
}
219219

220+
public function copyQueryRules($fromIndexName, $toIndexName)
221+
{
222+
$fromIndex = $this->getIndex($fromIndexName);
223+
$toIndex = $this->getIndex($toIndexName);
224+
225+
$queryRulesToSet = array();
226+
227+
$hitsPerPage = 100;
228+
$page = 0;
229+
do {
230+
$fetchedQueryRules = $fromIndex->searchRules(array(
231+
'page' => $page,
232+
'hitsPerPage' => $hitsPerPage,
233+
));
234+
235+
foreach ($fetchedQueryRules['hits'] as $hit) {
236+
unset($hit['_highlightResult']);
237+
238+
$queryRulesToSet[] = $hit;
239+
}
240+
241+
$page++;
242+
} while (($page * $hitsPerPage) < $fetchedQueryRules['nbHits']);
243+
244+
if (empty($queryRulesToSet)) {
245+
$res = $toIndex->clearRules(true);
246+
} else {
247+
$res = $toIndex->batchRules($queryRulesToSet, true, true);
248+
}
249+
250+
$this->lastUsedIndexName = $toIndex;
251+
$this->lastTaskId = $res['taskID'];
252+
}
253+
220254
public function waitLastTask()
221255
{
222256
if (!isset($this->lastUsedIndexName) || !isset($this->lastTaskId)) {

app/code/community/Algolia/Algoliasearch/Helper/Entity/Producthelper.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,10 @@ public function setSettings($storeId, $saveToTmpIndicesToo = false)
406406
} elseif ($saveToTmpIndicesToo === true) {
407407
$this->algolia_helper->copySynonyms($this->getIndexName($storeId), $this->getIndexName($storeId, $saveToTmpIndicesToo));
408408
}
409+
410+
if ($saveToTmpIndicesToo === true) {
411+
$this->algolia_helper->copyQueryRules($this->getIndexName($storeId), $this->getIndexName($storeId, $saveToTmpIndicesToo));
412+
}
409413
}
410414

411415
protected function getFields($store)

app/code/community/Algolia/Algoliasearch/Model/Queue.php

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Algolia_Algoliasearch_Model_Queue
66
const ERROR_LOG = 'algoliasearch_queue_errors.log';
77

88
protected $table;
9+
protected $logTable;
910

1011
/** @var Magento_Db_Adapter_Pdo_Mysql */
1112
protected $db;
@@ -29,12 +30,16 @@ class Algolia_Algoliasearch_Model_Queue
2930

3031
private $noOfFailedJobs = 0;
3132

33+
private $logRecord = array();
34+
3235
public function __construct()
3336
{
3437
/** @var Mage_Core_Model_Resource $coreResource */
3538
$coreResource = Mage::getSingleton('core/resource');
3639

3740
$this->table = $coreResource->getTableName('algoliasearch/queue');
41+
$this->logTable = $this->table.'_log';
42+
3843
$this->db = $coreResource->getConnection('core_write');
3944

4045
$this->config = Mage::helper('algoliasearch/config');
@@ -47,6 +52,7 @@ public function add($class, $method, $data, $data_size)
4752
{
4853
// Insert a row for the new job
4954
$this->db->insert($this->table, array(
55+
'created' => date('Y-m-d H:i:s'),
5056
'class' => $class,
5157
'method' => $method,
5258
'data' => json_encode($data),
@@ -55,19 +61,38 @@ public function add($class, $method, $data, $data_size)
5561
));
5662
}
5763

58-
public function runCron()
64+
public function runCron($nbJobs = null, $force = false)
5965
{
60-
if (!$this->config->isQueueActive()) {
66+
if (!$this->config->isQueueActive() && $force === false) {
6167
return;
6268
}
6369

64-
$nbJobs = $this->config->getNumberOfJobToRun();
70+
$this->clearOldLogRecords();
71+
72+
$this->logRecord = array(
73+
'started' => date('Y-m-d H:i:s'),
74+
'processed_jobs' => 0,
75+
'with_empty_queue' => 0,
76+
);
77+
78+
$started = time();
79+
80+
if ($nbJobs === null) {
81+
$nbJobs = $this->config->getNumberOfJobToRun();
82+
if (getenv('EMPTY_QUEUE') && getenv('EMPTY_QUEUE') == '1') {
83+
$nbJobs = -1;
6584

66-
if (getenv('EMPTY_QUEUE') && getenv('EMPTY_QUEUE') == '1') {
67-
$nbJobs = -1;
85+
$this->logRecord['with_empty_queue'] = 1;
86+
}
6887
}
6988

7089
$this->run($nbJobs);
90+
91+
$this->logRecord['duration'] = time() - $started;
92+
93+
$this->db->insert($this->logTable, $this->logRecord);
94+
95+
$this->db->closeConnection();
7196
}
7297

7398
public function run($maxJobs)
@@ -77,7 +102,7 @@ public function run($maxJobs)
77102
$jobs = $this->getJobs($maxJobs, $pid);
78103

79104
if (empty($jobs)) {
80-
$this->db->closeConnection();
105+
return;
81106
}
82107

83108
// Run all reserved jobs
@@ -96,6 +121,8 @@ public function run($maxJobs)
96121
$model = Mage::getSingleton($job['class']);
97122
$method = $job['method'];
98123
$model->{$method}(new Varien_Object($job['data']));
124+
125+
$this->logRecord['processed_jobs'] += count($job['merged_ids']);
99126
} catch (\Exception $e) {
100127
$this->noOfFailedJobs++;
101128

@@ -119,8 +146,6 @@ public function run($maxJobs)
119146

120147
return;
121148
}
122-
123-
$this->db->closeConnection();
124149
}
125150

126151
private function getJobs($maxJobs, $pid)
@@ -381,4 +406,14 @@ private function maxValueInArray($array, $keyToSearch)
381406

382407
return $currentMax;
383408
}
409+
410+
private function clearOldLogRecords()
411+
{
412+
$idsToDelete = $this->db->query("SELECT id FROM {$this->logTable} ORDER BY started DESC, id DESC LIMIT 25000, ".PHP_INT_MAX)
413+
->fetchAll(\PDO::FETCH_COLUMN, 0);
414+
415+
if ($idsToDelete) {
416+
$this->db->query("DELETE FROM {$this->logTable} WHERE id IN (" . implode(", ", $idsToDelete) . ")");
417+
}
418+
}
384419
}

app/code/community/Algolia/Algoliasearch/etc/config.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<config>
33
<modules>
44
<Algolia_Algoliasearch>
5-
<version>1.11.0</version>
5+
<version>1.11.1</version>
66
</Algolia_Algoliasearch>
77
</modules>
88
<frontend>

app/code/community/Algolia/Algoliasearch/etc/system.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<algoliasearch translate="label" module="algoliasearch">
55
<label>
66
<![CDATA[
7-
Algolia Search 1.11.0
7+
Algolia Search 1.11.1
88
<style>
99
.algoliasearch-admin-menu span {
1010
padding-left: 38px !important;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/** @var Mage_Core_Model_Resource_Setup $installer */
4+
$installer = $this;
5+
$installer->startSetup();
6+
7+
$tableName = $installer->getTable('algoliasearch/queue');
8+
$installer->run("ALTER TABLE `{$tableName}` ADD `created` DATETIME AFTER `job_id`;");
9+
10+
$installer->run("
11+
CREATE TABLE IF NOT EXISTS `{$tableName}_log` (
12+
`id` INT(20) NOT NULL auto_increment,
13+
`started` DATETIME NOT NULL,
14+
`duration` INT(20) NOT NULL,
15+
`processed_jobs` INT NOT NULL,
16+
`with_empty_queue` INT(1) NOT NULL,
17+
PRIMARY KEY `id` (`id`)
18+
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8 AUTO_INCREMENT=1;
19+
");
20+
21+
$installer->endSetup();

app/design/adminhtml/default/default/layout/algoliasearch.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
<?xml version="1.0"?>
22
<layout>
3+
<default>
4+
<reference name="head">
5+
<action method="addCss"><stylesheet>algoliasearch/algoliasearch.css</stylesheet></action>
6+
</reference>
7+
8+
<reference name="notifications">
9+
<block type="algoliasearch/adminhtml_notifications" name="algoliasearch_notifications" template="algoliasearch/notifications.phtml"/>
10+
</reference>
11+
</default>
12+
313
<algolia_bundle_handle>
414
<reference name="head">
515
<action method="addJs"><script>algoliasearch/internals/adminhtml/algoliaAdminBundle.min.js</script></action>

0 commit comments

Comments
 (0)