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 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
``` ```
## Подключенные сервисы ## Подключенные сервисы

View File

@ -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,12 +23,21 @@ 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
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: try:
self.client = OpenSearch( self.client = OpenSearch(
hosts=[{'host': ELASTIC_HOST, 'port': ELASTIC_PORT}], hosts=[{'host': ELASTIC_HOST, 'port': ELASTIC_PORT}],
@ -48,14 +55,21 @@ class SearchService:
self.disabled = True self.disabled = True
self.check_index() self.check_index()
else:
self.disabled = True
def info(self): def info(self):
logging.info(f'{self.client}')
try: try:
if self.client:
logger.info(f'{self.client}')
indices = self.client.indices.get_alias('*') indices = self.client.indices.get_alias('*')
logger.debug('List of indices:') logger.debug('List of indices:')
for index in indices: for index in indices:
logger.debug(f'- {index}') 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}')