diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2024-06-18 16:07:00 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2024-06-18 16:07:00 +0700 |
| commit | 938ebeb3e785da9d0f40504932ae574d5f8eb27c (patch) | |
| tree | 64c089df61ab6a58c07c3eef32c1031e9170f550 | |
| parent | 2fec90b4a9040cde79774f61e4e19fff30f2a916 (diff) | |
add try catch while sync solr recommendation
| -rw-r--r-- | indoteknik_custom/models/solr/apache_solr.py | 35 |
1 files changed, 17 insertions, 18 deletions
diff --git a/indoteknik_custom/models/solr/apache_solr.py b/indoteknik_custom/models/solr/apache_solr.py index 7fc9dd5f..2e275698 100644 --- a/indoteknik_custom/models/solr/apache_solr.py +++ b/indoteknik_custom/models/solr/apache_solr.py @@ -295,22 +295,21 @@ class ApacheSolr(models.Model): def _solr_sync_recommendation(self, limit=100): variants = self.env['product.product'].search([('solr_flag', '=', 2)], limit=limit) - counter = 0 for variant in variants: - template_time = time.time() - counter += 1 - document = {} - document.update({ - 'id': variant.id or 0, - 'display_name_s': variant.display_name or '', - 'name_s': variant.name or '', - 'default_code_s': variant.default_code or '', - 'product_rating_f': variant.product_tmpl_id.virtual_rating or 0, - 'template_id_i': variant.product_tmpl_id.id or 0, - 'active_s': str(variant.active) or 'false', - 'type_s': variant.product_tmpl_id.type or '' - }) - variant.solr_flag = 1 - _recommendation_solr.add(document) - _logger.info('[SYNC_VARIANTS_TO_SOLR] {}/{} {:.6f}'.format(counter, limit, time.time() - template_time)) - _logger.info('[SYNC_VARIANTS_TO_SOLR] Success add to solr variants %s' % variant.id) + if variant.product_tmpl_id: # Check if product_tmpl_id exists + try: + document = { + 'id': variant.id or 0, + 'display_name_s': variant.display_name or '', + 'name_s': variant.name or '', + 'default_code_s': variant.default_code or '', + 'product_rating_f': variant.product_tmpl_id.virtual_rating or 0, + 'template_id_i': variant.product_tmpl_id.id or 0, + 'active_s': str(variant.active) or 'false', + 'type_s': variant.product_tmpl_id.type or '' + } + variant.write({'solr_flag': 1}) # Ensure the flag is updated correctly + _recommendation_solr.add(document) + except Exception as e: + _logger.error("Failed to add document to Solr: %s", e) + _logger.debug("Document data: %s", document) |
