Здравствуйте!
Переписал скрипт мониторинга, с использованием недавно изученных классов. Почему-то отвалилась многопоточность. Подскажите, пожалуйста, в чем проблема
import requests, datetime, time, os, json
from platform import system
from settings import *
def worktime():
hour = datetime.datetime.now().strftime("%H")
if "08" < hour < "23":
return True
def system_path():
if system() == 'Windows':
system_path = 'C:/pymon_logs/'
else:
system_path = '/var/log/pymon/'
if not os.path.exists(system_path):
os.makedirs(system_path)
return system_path
class Monitor:
def __init__(self, service, *request_params):
self.request_params = request_params
self.service = service
self.silence = 0
self.night_await = 0
self.night_raised = 0
self.req_status = ''
self.path = system_path()
def request(self):
try:
if len(self.request_params) == 1:
self.req_status = requests.get(self.request_params[0])
else:
self.req_status = requests.post(self.request_params[0],
headers=self.request_params[1],
data=json.dumps(self.request_params[2]))
if self.req_status.ok:
self.night_raised = 0
return self.req_status.status_code
except Exception as error:
self.req_status = error
return self.req_status
def log(self):
now = round(time.time())
today = str(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S"))
if not worktime():
if self.night_raised == 0:
self.night_await = now + 180
self.night_raised = 1
if now >= self.silence:
if not worktime():
bot.send_message(chat_id, f'{self.service} is Down')
self.silence = now + 120
else:
if now >= self.night_await:
bot.send_message(chat_id, f'{self.service} is Down')
self.night_raised = 0
with open(f"{self.path}{self.service.lower()}_log.txt", "a") as file:
file.write(f'{today} {self.service.upper()}: \n {self.req_status} \n\n')
run = True
def start_mon(service):
while 1:
while run:
value = service.request()
if value != 200:
service.log()
time.sleep(2)
else:
continue
def start_cabinet():
cabinet = Monitor('Cabinet', cabinetUrl)
start_mon(cabinet)
def start_site():
site = Monitor('sitename', site_url)
start_mon(site)
def start_back():
back = Monitor('Back', back_url, back_headers, back_data)
start_mon(back)
def start_auth_solo():
auth_solo = Monitor('Auth', auth_url, auth_headers, auth_data)
start_mon(auth_solo)
def start_api():
api = Monitor('API', api_url)
start_mon(api)
Мэин
import threading
from logic import *
if __name__ == "__main__":
threads = list()
auth = threading.Thread(target=start_mon(start_auth_solo))
threads.append(auth)
auth.start()
backend = threading.Thread(target=(start_back))
threads.append(backend)
backend.start()
cabinet = threading.Thread(target=start_mon(start_cabinet))
threads.append(cabinet)
cabinet.start()
site = threading.Thread(target=start_mon(start_sit))
threads.append(site)
site.start()
''' IF START POLLING WITH PRODUCTION RUNNING
IT WILL CRASH THE PRODUCTION MONITORING '''
bot = threading.Thread(target=bot.polling)
threads.append(bot)
bot.start()
api = threading.Thread(target=start_mon(start_api))
threads.append(api)
api.start()