Added ping services

This commit is contained in:
Netkas 2021-01-15 16:36:57 -05:00
parent 5a18d1e978
commit 3c494acd87
5 changed files with 273 additions and 273 deletions

View File

@ -1,161 +1,161 @@
""" """
This script is executed on start-up after starting up the services, this is intended to check if all the services This script is executed on start-up after starting up the services, this is intended to check if all the services
has started correctly and load all the resources that are required are loaded, after this program is done has started correctly and load all the resources that are required are loaded, after this program is done
executing, the bootstrap should start the ping service so that programs know that CoffeeHouse-Utils is ready. executing, the bootstrap should start the ping service so that programs know that CoffeeHouse-Utils is ready.
""" """
import urllib.parse import urllib.parse
import requests import requests
import base64 import base64
import time import time
import sys import sys
import os import os
def core_nlp(): def core_nlp():
print("[CORENLP] Warming up") print("[CORENLP] Warming up")
current_tries = 0 current_tries = 0
max_tries = 30 # 5 Minutes max_tries = 30 # 5 Minutes
while True: while True:
if current_tries > max_tries: if current_tries > max_tries:
print("[CORENLP] ERROR! Too many attempts") print("[CORENLP] ERROR! Too many attempts")
sys.exit(1) sys.exit(1)
try: try:
r = requests.post("http://127.0.0.1:5604/?properties={\"annotators\": \"tokenize,ssplit,pos,ner,regexner," r = requests.post("http://127.0.0.1:5604/?properties={\"annotators\": \"tokenize,ssplit,pos,ner,regexner,"
"sentiment\"}&pipelineLanguage=en", "sentiment\"}&pipelineLanguage=en",
headers={"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"}, headers={"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"},
data=urllib.parse.quote_plus("The quick brown fox jumped over the lazy dog") data=urllib.parse.quote_plus("The quick brown fox jumped over the lazy dog")
) )
if r.status_code == 200: if r.status_code == 200:
print("[CORENLP] OK") print("[CORENLP] OK")
break break
else: else:
print("[CORENLP] Failed, Returned status code {0}, '{1}'".format(str(r.status_code), r.text)) print("[CORENLP] Failed, Returned status code {0}, '{1}'".format(str(r.status_code), r.text))
except: except:
print("[CORENLP] Failed, request not completed") print("[CORENLP] Failed, request not completed")
pass pass
print("[CORENLP] Waiting 10 seconds") print("[CORENLP] Waiting 10 seconds")
time.sleep(10) time.sleep(10)
current_tries += 1 current_tries += 1
print("[CORENLP] Started successfully") print("[CORENLP] Started successfully")
def langdetect(): def langdetect():
print("[LANGDETECT] Warming up") print("[LANGDETECT] Warming up")
current_tries = 0 current_tries = 0
max_tries = 30 # 5 Minutes max_tries = 30 # 5 Minutes
while True: while True:
if current_tries > max_tries: if current_tries > max_tries:
print("[LANGDETECT] ERROR! Too many attempts") print("[LANGDETECT] ERROR! Too many attempts")
sys.exit(1) sys.exit(1)
try: try:
r = requests.post("http://127.0.0.1:5606/", data={"input": "The quick brown fox jumped over the lazy dog"}) r = requests.post("http://127.0.0.1:5606/", data={"input": "The quick brown fox jumped over the lazy dog"})
if r.status_code == 200: if r.status_code == 200:
print("[LANGDETECT] OK") print("[LANGDETECT] OK")
break break
else: else:
print("[LANGDETECT] Failed, Returned status code {0}, '{1}'".format(str(r.status_code), r.text)) print("[LANGDETECT] Failed, Returned status code {0}, '{1}'".format(str(r.status_code), r.text))
except: except:
print("[LANGDETECT] Failed, request not completed") print("[LANGDETECT] Failed, request not completed")
pass pass
print("[LANGDETECT] Waiting 10 seconds") print("[LANGDETECT] Waiting 10 seconds")
time.sleep(10) time.sleep(10)
current_tries += 1 current_tries += 1
print("[LANGDETECT] Started successfully") print("[LANGDETECT] Started successfully")
def spamdetect(): def spamdetect():
print("[SPAMDETECT] Warming up") print("[SPAMDETECT] Warming up")
current_tries = 0 current_tries = 0
max_tries = 30 # 5 Minutes max_tries = 30 # 5 Minutes
while True: while True:
if current_tries > max_tries: if current_tries > max_tries:
print("[SPAMDETECT] ERROR! Too many attempts") print("[SPAMDETECT] ERROR! Too many attempts")
sys.exit(1) sys.exit(1)
try: try:
r = requests.post("http://127.0.0.1:5601/", data={"input": "The quick brown fox jumped over the lazy dog"}) r = requests.post("http://127.0.0.1:5601/", data={"input": "The quick brown fox jumped over the lazy dog"})
if r.status_code == 200: if r.status_code == 200:
print("[SPAMDETECT] OK") print("[SPAMDETECT] OK")
break break
else: else:
print("[SPAMDETECT] Failed, Returned status code {0}, '{1}'".format(str(r.status_code), r.text)) print("[SPAMDETECT] Failed, Returned status code {0}, '{1}'".format(str(r.status_code), r.text))
except: except:
print("[SPAMDETECT] Failed, request not completed") print("[SPAMDETECT] Failed, request not completed")
pass pass
print("[SPAMDETECT] Waiting 10 seconds") print("[SPAMDETECT] Waiting 10 seconds")
time.sleep(10) time.sleep(10)
current_tries += 1 current_tries += 1
print("[SPAMDETECT] Started successfully") print("[SPAMDETECT] Started successfully")
def nsfw(): def nsfw():
print("[NSFW] Warming up") print("[NSFW] Warming up")
current_tries = 0 current_tries = 0
max_tries = 30 # 5 Minutes max_tries = 30 # 5 Minutes
with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), "red.jpg"), "rb") as img_file: with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), "red.jpg"), "rb") as img_file:
image_data = base64.b64encode(img_file.read()).decode('utf-8') image_data = base64.b64encode(img_file.read()).decode('utf-8')
while True: while True:
if current_tries > max_tries: if current_tries > max_tries:
print("[NSFW] ERROR! Too many attempts") print("[NSFW] ERROR! Too many attempts")
sys.exit(1) sys.exit(1)
try: try:
r = requests.post("http://127.0.0.1:5602/", data={ r = requests.post("http://127.0.0.1:5602/", data={
"input": image_data, "input": image_data,
"type": "jpg" "type": "jpg"
}) })
if r.status_code == 200: if r.status_code == 200:
print("[NSFW] OK") print("[NSFW] OK")
break break
else: else:
print("[NSFW] Failed, Returned status code {0}, '{1}'".format(str(r.status_code), r.text)) print("[NSFW] Failed, Returned status code {0}, '{1}'".format(str(r.status_code), r.text))
except: except:
print("[NSFW] Failed, request not completed") print("[NSFW] Failed, request not completed")
pass pass
print("[NSFW] Waiting 10 seconds") print("[NSFW] Waiting 10 seconds")
time.sleep(10) time.sleep(10)
current_tries += 1 current_tries += 1
print("[NSFW] Started successfully") print("[NSFW] Started successfully")
print(" ( ) ( ) )") print(" ( ) ( ) )")
print(" ) ( ) ( (") print(" ) ( ) ( (")
print(" ( ) ( ) )") print(" ( ) ( ) )")
print(" _____________") print(" _____________")
print(" <_____________> ___") print(" <_____________> ___")
print(" | |/ _ \\") print(" | |/ _ \\")
print(" | | | |") print(" | | | |")
print(" | |_| |") print(" | |_| |")
print(" ___| |\___/") print(" ___| |\___/")
print("/ \___________/ \\") print("/ \___________/ \\")
print("\_____________________/") print("\_____________________/")
print() print()
print("CoffeeHouse-Utils Warmup") print("CoffeeHouse-Utils Warmup")
core_nlp() core_nlp()
spamdetect() spamdetect()
langdetect() langdetect()
nsfw() nsfw()
print("OK, CoffeeHouse-Utils seems to be running fine.") print("OK, CoffeeHouse-Utils seems to be running fine.")
print("Exiting with code 0, the ping service should start in the next step.") print("Exiting with code 0, the ping service should start in the next step.")
sys.exit(0) sys.exit(0)

View File

@ -1,4 +1,4 @@
from . import server from . import server
from .server import * from .server import *
__all__ = ["Server"] __all__ = ["Server"]

View File

@ -1,50 +1,50 @@
import sys import sys
from coffeehouse_ping import Server from coffeehouse_ping import Server
def _real_main(argv=None): def _real_main(argv=None):
""" """
The main command-line processor The main command-line processor
:param argv: :param argv:
:return: :return:
""" """
if argv[1] == '--help': if argv[1] == '--help':
_help_menu(argv) _help_menu(argv)
if argv[1] == '--start-server': if argv[1] == '--start-server':
_start_server(argv) _start_server(argv)
def _start_server(argv=None): def _start_server(argv=None):
""" """
Starts the server Starts the server
:param argv: :param argv:
:return: :return:
""" """
server = Server() server = Server()
server.start() server.start()
def _help_menu(argv=None): def _help_menu(argv=None):
""" """
Displays the help menu and commandline usage Displays the help menu and commandline usage
:param argv: :param argv:
:return: :return:
""" """
print( print(
"CoffeeHouse Ping CLI\n\n" "CoffeeHouse Ping CLI\n\n"
" --help\n" " --help\n"
" --start-server\n" " --start-server\n"
) )
sys.exit() sys.exit()
if __name__ == '__main__': if __name__ == '__main__':
try: try:
_real_main(sys.argv) _real_main(sys.argv)
except KeyboardInterrupt: except KeyboardInterrupt:
print('\nInterrupted by user') print('\nInterrupted by user')

View File

@ -1,44 +1,44 @@
from hyper_internal_service import web from hyper_internal_service import web
__all__ = ['Server'] __all__ = ['Server']
class Server(object): class Server(object):
def __init__(self, port=5600): def __init__(self, port=5600):
""" """
Public Constructor Public Constructor
:param port: :param port:
""" """
self.port = port self.port = port
self.web_application = web.Application() self.web_application = web.Application()
self.web_application.add_routes( self.web_application.add_routes(
[web.post('/', self.ping)] [web.post('/', self.ping)]
) )
async def ping(self, request): async def ping(self, request):
""" """
Handles the predict request "/", usage: Handles the predict request "/", usage:
POST:: "input": str POST:: "input": str
:param request: :param request:
:return: :return:
""" """
post_data = await request.post() post_data = await request.post()
return web.json_response({"status": True}) return web.json_response({"status": True})
def start(self): def start(self):
""" """
Starts the web application Starts the web application
:return: :return:
""" """
web.run_app(app=self.web_application, port=self.port) web.run_app(app=self.web_application, port=self.port)
return True return True
def stop(self): def stop(self):
""" """
Stops the web application Stops the web application
:return: :return:
""" """
self.web_application.shutdown() self.web_application.shutdown()
self.web_application.cleanup() self.web_application.cleanup()
return True return True

View File

@ -1,16 +1,16 @@
from setuptools import setup, find_packages from setuptools import setup, find_packages
from setuptools.command.install import install from setuptools.command.install import install
setup( setup(
name='coffeehouse_ping', name='coffeehouse_ping',
version='1.0.0', version='1.0.0',
description='Provides an endpoint for programs to ping for CoffeeHouse-Utils', description='Provides an endpoint for programs to ping for CoffeeHouse-Utils',
url='https://github.com/Intellivoid/CoffeeHousePy', url='https://github.com/Intellivoid/CoffeeHousePy',
author='Zi Xing Narrakas', author='Zi Xing Narrakas',
author_email='netkas@intellivoid.info', author_email='netkas@intellivoid.info',
classifiers=[ classifiers=[
'Development Status :: 3 - Internal/Alpha' 'Development Status :: 3 - Internal/Alpha'
], ],
packages=find_packages() packages=find_packages()
) )