Skip to content

Instantly share code, notes, and snippets.

@1f604
Created January 23, 2022 15:07
Show Gist options
  • Save 1f604/bc10d5ca058e0c4c1ccfa59b144fb2c6 to your computer and use it in GitHub Desktop.
Save 1f604/bc10d5ca058e0c4c1ccfa59b144fb2c6 to your computer and use it in GitHub Desktop.
Launch elasticsearch in a loop
# change these as needed
MAX_RAM_USAGE = 4 # GB
ELASTIC_BINARY_LOCATION = "/home/x/elasticsearch-7.16.3/bin/elasticsearch"
LOG_FILE = "./log.txt"
from multiprocessing import Process
import subprocess
import os
import requests
from time import sleep
def launch_process():
command_str = ELASTIC_BINARY_LOCATION
my_env = os.environ.copy()
my_env["ES_JAVA_OPTS"] = '-Xms'+str(MAX_RAM_USAGE)+'g -Xmx'+str(MAX_RAM_USAGE)+'g'
f = open("log.txt", "w")
process = subprocess.Popen(command_str, env=my_env, stdout=f)
return process
def launch_loop():
while True:
print("Launching new process")
p = launch_process()
try:
print('Running in process', p.pid)
p.wait(timeout=15)
except subprocess.TimeoutExpired:
print('Timed out - killing', p.pid)
p.kill()
print("killed")
sleep(15)
def monitor():
failed_count = 0
while True:
sleep(1)
try:
r = requests.get('http://localhost:9200/_cluster/health?pretty=true')
print("=====RESPONSE:", r.json())
if failed_count != 0:
print("\nsuccessful cluster health status query!")
failed_count = 0
except:
failed_count += 1
if failed_count == 1:
print("failed cluster health status polls: 1", end=' ', flush=True)
else:
print(failed_count, end=' ', flush=True)
pass
if __name__ == '__main__':
p = Process(target=launch_loop)
m = Process(target=monitor)
p.start()
m.start()
sleep(5)
p.join()
print("joined!")
sleep(100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment