public function rebuildStoreProductIndex($storeId, $productIds) { if ($this->config->isEnabledBackend($storeId) === false) { $this->logger->log('INDEXING IS DISABLED FOR ' . $this->logger->getStoreName($storeId)); return; } $emulationInfo = $this->startEmulation($storeId); try { $collection = $this->product_helper->getProductCollectionQuery($storeId, $productIds, false); $size = $collection->getSize(); if (!empty($productIds)) { $size = max(count($productIds), $size); } $this->logger->log('Store ' . $this->logger->getStoreName($storeId) . ' collection size : ' . $size); if ($size > 0) { $pages = ceil($size / $this->config->getNumberOfElementByPage()); $page = 1; $collection->clear(); while ($page <= $pages) { $this->rebuildStoreProductIndexPage($storeId, $collection, $page, $this->config->getNumberOfElementByPage(), $emulationInfo, $productIds); $page++; } } } catch (Exception $e) { $this->stopEmulation($emulationInfo); throw $e; } $this->stopEmulation($emulationInfo); }
public function run($limit) { $full_reindex = $limit === -1; $limit = $full_reindex ? 1 : $limit; $element_count = 0; $jobs = array(); $offset = 0; $max_size = $this->config->getNumberOfElementByPage() * $limit; while ($element_count < $max_size) { $data = $this->db->query($this->db->select()->from($this->table, '*')->where('pid IS NULL')->order(array('job_id'))->limit($limit, $limit * $offset)); $data = $data->fetchAll(); $offset++; if (count($data) <= 0) { break; } foreach ($data as $job) { $job_size = (int) $job['data_size']; if ($element_count + $job_size <= $max_size) { $jobs[] = $job; $element_count += $job_size; } else { break 2; } } } if (count($jobs) <= 0) { return; } $first_id = $jobs[0]['job_id']; $last_id = $jobs[count($jobs) - 1]['job_id']; $pid = getmypid(); // Reserve all new jobs since last run $this->db->query("UPDATE {$this->db->quoteIdentifier($this->table, true)} SET pid = " . $pid . ' WHERE job_id >= ' . $first_id . " AND job_id <= {$last_id}"); foreach ($jobs as &$job) { $job['data'] = json_decode($job['data'], true); } $jobs = $this->sortAndMergeJob($jobs); // Run all reserved jobs foreach ($jobs as $job) { try { $model = Mage::getSingleton($job['class']); $method = $job['method']; $model->{$method}(new Varien_Object($job['data'])); } catch (Exception $e) { // Increment retries and log error information $this->logger->log("Queue processing {$job['pid']} [KO]: Mage::getSingleton({$job['class']})->{$job['method']}(" . json_encode($job['data']) . ')'); $this->logger->log(date('c') . ' ERROR: ' . get_class($e) . ": '{$e->getMessage()}' in {$e->getFile()}:{$e->getLine()}\n" . "Stack trace:\n" . $e->getTraceAsString()); } } // Delete only when finished to be able to debug the queue if needed $where = $this->db->quoteInto('pid = ?', $pid); $this->db->delete($this->table, $where); if ($full_reindex) { $this->run(-1); } }
protected function _rebuildProductIndex($storeId, $productIds = null, $useTmpIndex = false) { if ($productIds == null || count($productIds) == 0) { $collection = $this->product_helper->getProductCollectionQuery($storeId, $productIds, $useTmpIndex); $size = $collection->getSize(); if (!empty($productIds)) { $size = max(count($productIds), $size); } $by_page = $this->config->getNumberOfElementByPage(); $nb_page = ceil($size / $by_page); for ($i = 1; $i <= $nb_page; $i++) { $data = array('store_id' => $storeId, 'product_ids' => $productIds, 'page_size' => $by_page, 'page' => $i, 'use_tmp_index' => $useTmpIndex); $this->addToQueue('algoliasearch/observer', 'rebuildProductIndex', $data, $by_page); } } else { $this->addToQueue('algoliasearch/observer', 'rebuildProductIndex', array('store_id' => $storeId, 'product_ids' => $productIds), count($productIds)); } return $this; }