This commit is contained in:
parent
9e18697cac
commit
cf23d343d1
|
@ -29,7 +29,7 @@ mkdir .venv
|
||||||
python3.12 -m venv .venv
|
python3.12 -m venv .venv
|
||||||
poetry env use .venv/bin/python3.12
|
poetry env use .venv/bin/python3.12
|
||||||
poetry update
|
poetry update
|
||||||
poetry run main.py
|
poetry run server.py
|
||||||
```
|
```
|
||||||
## Подключенные сервисы
|
## Подключенные сервисы
|
||||||
|
|
||||||
|
|
|
@ -1,20 +1,18 @@
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
from multiprocessing import Lock
|
from multiprocessing import Manager
|
||||||
|
|
||||||
from opensearchpy import OpenSearch
|
from opensearchpy import OpenSearch
|
||||||
|
|
||||||
from services.rediscache import redis
|
from services.rediscache import redis
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger('[services.search] ')
|
logger = logging.getLogger('\t[services.search]\t')
|
||||||
logger.setLevel(logging.DEBUG)
|
logger.setLevel(logging.DEBUG)
|
||||||
|
|
||||||
ELASTIC_HOST = (
|
ELASTIC_HOST = (
|
||||||
os.environ.get('ELASTIC_HOST', 'localhost')
|
os.environ.get('ELASTIC_HOST', '').replace('https://', '').replace('http://', '')
|
||||||
.replace('https://', '')
|
|
||||||
.replace('http://', '')
|
|
||||||
)
|
)
|
||||||
ELASTIC_USER = os.environ.get('ELASTIC_USER', '')
|
ELASTIC_USER = os.environ.get('ELASTIC_USER', '')
|
||||||
ELASTIC_PASSWORD = os.environ.get('ELASTIC_PASSWORD', '')
|
ELASTIC_PASSWORD = os.environ.get('ELASTIC_PASSWORD', '')
|
||||||
|
@ -25,37 +23,53 @@ ELASTIC_URL = os.environ.get(
|
||||||
)
|
)
|
||||||
REDIS_TTL = 86400 # 1 day in seconds
|
REDIS_TTL = 86400 # 1 day in seconds
|
||||||
|
|
||||||
|
|
||||||
class SearchService:
|
class SearchService:
|
||||||
lock = Lock()
|
|
||||||
def __init__(self, index_name='posts'):
|
def __init__(self, index_name='posts'):
|
||||||
logger.info('initialized')
|
|
||||||
self.index_name = index_name
|
self.index_name = index_name
|
||||||
self.disabled = False
|
self.disabled = False
|
||||||
try:
|
self.manager = Manager()
|
||||||
self.client = OpenSearch(
|
self.client = None
|
||||||
hosts=[{'host': ELASTIC_HOST, 'port': ELASTIC_PORT}],
|
|
||||||
http_compress=True,
|
|
||||||
http_auth=(ELASTIC_USER, ELASTIC_PASSWORD),
|
|
||||||
use_ssl=True,
|
|
||||||
verify_certs=False,
|
|
||||||
ssl_assert_hostname=False,
|
|
||||||
ssl_show_warn=False,
|
|
||||||
# ca_certs = ca_certs_path
|
|
||||||
)
|
|
||||||
|
|
||||||
except Exception as exc:
|
# Используем менеджер для создания Lock и Value
|
||||||
logger.error(exc)
|
self.lock = self.manager.Lock()
|
||||||
|
self.initialized_flag = self.manager.Value('i', 0)
|
||||||
|
|
||||||
|
# Only initialize the instance if it's not already initialized
|
||||||
|
if not self.initialized_flag.value and ELASTIC_HOST:
|
||||||
|
logger.info(' инициализация клиента OpenSearch.org')
|
||||||
|
try:
|
||||||
|
self.client = OpenSearch(
|
||||||
|
hosts=[{'host': ELASTIC_HOST, 'port': ELASTIC_PORT}],
|
||||||
|
http_compress=True,
|
||||||
|
http_auth=(ELASTIC_USER, ELASTIC_PASSWORD),
|
||||||
|
use_ssl=True,
|
||||||
|
verify_certs=False,
|
||||||
|
ssl_assert_hostname=False,
|
||||||
|
ssl_show_warn=False,
|
||||||
|
# ca_certs = ca_certs_path
|
||||||
|
)
|
||||||
|
|
||||||
|
except Exception as exc:
|
||||||
|
logger.error(exc)
|
||||||
|
self.disabled = True
|
||||||
|
|
||||||
|
self.check_index()
|
||||||
|
else:
|
||||||
self.disabled = True
|
self.disabled = True
|
||||||
|
|
||||||
self.check_index()
|
|
||||||
|
|
||||||
def info(self):
|
def info(self):
|
||||||
logging.info(f'{self.client}')
|
|
||||||
try:
|
try:
|
||||||
indices = self.client.indices.get_alias('*')
|
if self.client:
|
||||||
logger.debug('List of indices:')
|
logger.info(f'{self.client}')
|
||||||
for index in indices:
|
indices = self.client.indices.get_alias('*')
|
||||||
logger.debug(f'- {index}')
|
logger.debug('List of indices:')
|
||||||
|
for index in indices:
|
||||||
|
logger.debug(f'- {index}')
|
||||||
|
else:
|
||||||
|
logger.info(
|
||||||
|
' * Задайте переменные среды для подключения к серверу поиска'
|
||||||
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f'Error while listing indices: {e}')
|
logger.error(f'Error while listing indices: {e}')
|
||||||
|
|
||||||
|
@ -144,7 +158,7 @@ class SearchService:
|
||||||
self.check_index()
|
self.check_index()
|
||||||
|
|
||||||
def index_post(self, shout):
|
def index_post(self, shout):
|
||||||
if not not self.disabled:
|
if not not self.disabled:
|
||||||
id_ = str(shout.id)
|
id_ = str(shout.id)
|
||||||
logger.debug(f'Indexing post id {id_}')
|
logger.debug(f'Indexing post id {id_}')
|
||||||
self.client.index(index=self.index_name, id=id_, body=shout)
|
self.client.index(index=self.index_name, id=id_, body=shout)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user