This commit is contained in:
parent
b574673f00
commit
9e18697cac
|
@ -1,6 +1,7 @@
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
from multiprocessing import Lock
|
||||||
|
|
||||||
from opensearchpy import OpenSearch
|
from opensearchpy import OpenSearch
|
||||||
|
|
||||||
|
@ -24,8 +25,8 @@ 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')
|
logger.info('initialized')
|
||||||
self.index_name = index_name
|
self.index_name = index_name
|
||||||
|
@ -59,7 +60,8 @@ class SearchService:
|
||||||
logger.error(f'Error while listing indices: {e}')
|
logger.error(f'Error while listing indices: {e}')
|
||||||
|
|
||||||
def delete_index(self):
|
def delete_index(self):
|
||||||
self.client.indices.delete(index=self.index_name, ignore_unavailable=True)
|
if not self.disabled:
|
||||||
|
self.client.indices.delete(index=self.index_name, ignore_unavailable=True)
|
||||||
|
|
||||||
def create_index(self):
|
def create_index(self):
|
||||||
index_settings = {
|
index_settings = {
|
||||||
|
@ -96,11 +98,13 @@ class SearchService:
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
try:
|
try:
|
||||||
self.client.indices.create(index=self.index_name, body=index_settings)
|
with self.lock:
|
||||||
self.client.indices.close(index=self.index_name)
|
self.client.indices.create(index=self.index_name, body=index_settings)
|
||||||
self.client.indices.open(index=self.index_name)
|
self.client.indices.close(index=self.index_name)
|
||||||
|
self.client.indices.open(index=self.index_name)
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
logger.warn(error)
|
logger.warn(error)
|
||||||
|
self.disabled = True
|
||||||
|
|
||||||
def put_mapping(self):
|
def put_mapping(self):
|
||||||
mapping = {
|
mapping = {
|
||||||
|
@ -114,7 +118,7 @@ class SearchService:
|
||||||
self.client.indices.put_mapping(index=self.index_name, body=mapping)
|
self.client.indices.put_mapping(index=self.index_name, body=mapping)
|
||||||
|
|
||||||
def check_index(self):
|
def check_index(self):
|
||||||
if not self.client.indices.exists(index=self.index_name):
|
if not self.client.indices.exists(index=self.index_name) and not self.disabled:
|
||||||
logger.debug(f'Creating {self.index_name} index')
|
logger.debug(f'Creating {self.index_name} index')
|
||||||
self.create_index()
|
self.create_index()
|
||||||
self.put_mapping()
|
self.put_mapping()
|
||||||
|
@ -135,13 +139,15 @@ class SearchService:
|
||||||
self.recreate_index()
|
self.recreate_index()
|
||||||
|
|
||||||
def recreate_index(self):
|
def recreate_index(self):
|
||||||
self.delete_index()
|
with self.lock:
|
||||||
self.check_index()
|
self.delete_index()
|
||||||
|
self.check_index()
|
||||||
|
|
||||||
def index_post(self, shout):
|
def index_post(self, shout):
|
||||||
id_ = str(shout.id)
|
if not not self.disabled:
|
||||||
logger.debug(f'Indexing post id {id_}')
|
id_ = str(shout.id)
|
||||||
self.client.index(index=self.index_name, id=id_, body=shout)
|
logger.debug(f'Indexing post id {id_}')
|
||||||
|
self.client.index(index=self.index_name, id=id_, body=shout)
|
||||||
|
|
||||||
def search_post(self, query, limit, offset):
|
def search_post(self, query, limit, offset):
|
||||||
logger.debug(f'query: {query}')
|
logger.debug(f'query: {query}')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user