stability-fail
All checks were successful
Deploy to core / deploy (push) Successful in 1m35s

This commit is contained in:
Untone 2024-01-29 05:56:28 +03:00
parent 9e18697cac
commit cf23d343d1
2 changed files with 43 additions and 29 deletions

View File

@ -29,7 +29,7 @@ mkdir .venv
python3.12 -m venv .venv
poetry env use .venv/bin/python3.12
poetry update
poetry run main.py
poetry run server.py
```
## Подключенные сервисы

View File

@ -1,20 +1,18 @@
import json
import logging
import os
from multiprocessing import Lock
from multiprocessing import Manager
from opensearchpy import OpenSearch
from services.rediscache import redis
logger = logging.getLogger('[services.search] ')
logger = logging.getLogger('\t[services.search]\t')
logger.setLevel(logging.DEBUG)
ELASTIC_HOST = (
os.environ.get('ELASTIC_HOST', 'localhost')
.replace('https://', '')
.replace('http://', '')
os.environ.get('ELASTIC_HOST', '').replace('https://', '').replace('http://', '')
)
ELASTIC_USER = os.environ.get('ELASTIC_USER', '')
ELASTIC_PASSWORD = os.environ.get('ELASTIC_PASSWORD', '')
@ -25,12 +23,21 @@ ELASTIC_URL = os.environ.get(
)
REDIS_TTL = 86400 # 1 day in seconds
class SearchService:
lock = Lock()
def __init__(self, index_name='posts'):
logger.info('initialized')
self.index_name = index_name
self.disabled = False
self.manager = Manager()
self.client = None
# Используем менеджер для создания Lock и Value
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}],
@ -48,14 +55,21 @@ class SearchService:
self.disabled = True
self.check_index()
else:
self.disabled = True
def info(self):
logging.info(f'{self.client}')
try:
if self.client:
logger.info(f'{self.client}')
indices = self.client.indices.get_alias('*')
logger.debug('List of indices:')
for index in indices:
logger.debug(f'- {index}')
else:
logger.info(
' * Задайте переменные среды для подключения к серверу поиска'
)
except Exception as e:
logger.error(f'Error while listing indices: {e}')