epicyon/threads.py

155 lines
4.6 KiB
Python
Raw Normal View History

2020-04-04 12:09:57 +00:00
__filename__ = "threads.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2021-01-26 10:07:42 +00:00
__version__ = "1.2.0"
2020-04-04 12:09:57 +00:00
__maintainer__ = "Bob Mottram"
2021-09-10 16:14:50 +00:00
__email__ = "bob@libreserver.org"
2020-04-04 12:09:57 +00:00
__status__ = "Production"
2021-06-26 11:16:41 +00:00
__module_group__ = "Core"
2019-06-30 16:36:58 +00:00
import threading
import sys
2019-06-30 16:50:43 +00:00
import time
2019-10-16 15:17:00 +00:00
import datetime
2019-06-30 16:36:58 +00:00
2020-04-04 12:09:57 +00:00
2019-10-16 15:17:00 +00:00
class threadWithTrace(threading.Thread):
2019-09-03 10:24:15 +00:00
def __init__(self, *args, **keywords):
2020-04-04 12:09:57 +00:00
self.startTime = datetime.datetime.utcnow()
self.isStarted = False
tries = 0
while tries < 3:
2019-10-14 20:32:00 +00:00
try:
2020-04-04 12:09:57 +00:00
self._args, self._keywords = args, keywords
threading.Thread.__init__(self, *self._args, **self._keywords)
self.killed = False
2019-10-14 20:32:00 +00:00
break
2021-12-25 15:28:52 +00:00
except Exception as ex:
print('ERROR: threads.py/__init__ failed - ' + str(ex))
2019-10-14 20:32:00 +00:00
time.sleep(1)
2020-04-04 12:09:57 +00:00
tries += 1
2020-03-22 21:16:02 +00:00
def start(self):
2020-04-04 12:09:57 +00:00
tries = 0
while tries < 3:
2019-10-14 20:32:00 +00:00
try:
2020-04-04 12:09:57 +00:00
self.__run_backup = self.run
self.run = self.__run
2019-10-14 20:32:00 +00:00
threading.Thread.start(self)
break
2021-12-25 15:28:52 +00:00
except Exception as ex:
print('ERROR: threads.py/start failed - ' + str(ex))
2019-10-14 20:32:00 +00:00
time.sleep(1)
2020-04-04 12:09:57 +00:00
tries += 1
2019-10-16 18:19:18 +00:00
# note that this is set True even if all tries failed
2020-04-04 12:09:57 +00:00
self.isStarted = True
2019-10-16 18:19:18 +00:00
2019-10-14 20:32:00 +00:00
def __run(self):
2019-10-19 18:08:47 +00:00
sys.settrace(self.globaltrace)
2021-09-15 16:08:41 +00:00
try:
self.__run_backup()
self.run = self.__run_backup
2021-12-25 15:28:52 +00:00
except Exception as ex:
print('ERROR: threads.py/__run failed - ' + str(ex))
2021-09-15 16:08:41 +00:00
pass
2019-10-14 20:32:00 +00:00
2020-03-22 21:16:02 +00:00
def globaltrace(self, frame, event, arg):
if event == 'call':
2019-06-30 16:36:58 +00:00
return self.localtrace
else:
return None
2020-03-22 21:16:02 +00:00
def localtrace(self, frame, event, arg):
if self.killed:
if event == 'line':
raise SystemExit()
return self.localtrace
def kill(self):
2020-04-04 12:09:57 +00:00
self.killed = True
2019-09-03 10:24:15 +00:00
2020-04-04 12:09:57 +00:00
def clone(self, fn):
return threadWithTrace(target=fn,
args=self._args,
2020-03-22 21:16:02 +00:00
daemon=True)
2019-10-16 10:08:21 +00:00
2020-04-04 12:09:57 +00:00
2021-12-25 16:17:53 +00:00
def removeDormantThreads(base_dir: str, threadsList: [], debug: bool,
timeoutMins: int) -> None:
2019-10-16 10:08:21 +00:00
"""Removes threads whose execution has completed
"""
2020-04-04 12:09:57 +00:00
if len(threadsList) == 0:
2019-10-16 10:08:21 +00:00
return
timeoutSecs = int(timeoutMins * 60)
2020-04-04 12:09:57 +00:00
dormantThreads = []
currTime = datetime.datetime.utcnow()
changed = False
2019-10-16 10:08:21 +00:00
# which threads are dormant?
2020-04-04 12:09:57 +00:00
noOfActiveThreads = 0
2019-10-16 10:08:21 +00:00
for th in threadsList:
2020-04-04 12:09:57 +00:00
removeThread = False
2019-10-16 15:17:00 +00:00
2019-10-16 18:19:18 +00:00
if th.isStarted:
if not th.is_alive():
2020-04-04 12:09:57 +00:00
if (currTime - th.startTime).total_seconds() > 10:
2019-10-23 10:23:43 +00:00
if debug:
2020-04-04 12:09:57 +00:00
print('DEBUG: ' +
'thread is not alive ten seconds after start')
removeThread = True
# timeout for started threads
if (currTime - th.startTime).total_seconds() > timeoutSecs:
if debug:
print('DEBUG: started thread timed out')
2020-04-04 12:09:57 +00:00
removeThread = True
2019-10-23 10:21:41 +00:00
else:
# timeout for threads which havn't been started
if (currTime - th.startTime).total_seconds() > timeoutSecs:
2019-10-23 10:21:41 +00:00
if debug:
print('DEBUG: unstarted thread timed out')
2020-04-04 12:09:57 +00:00
removeThread = True
2019-10-16 15:17:00 +00:00
if removeThread:
2019-10-16 10:08:21 +00:00
dormantThreads.append(th)
else:
2020-04-04 12:09:57 +00:00
noOfActiveThreads += 1
2019-10-16 10:08:21 +00:00
if debug:
2020-04-04 12:09:57 +00:00
print('DEBUG: ' + str(noOfActiveThreads) +
' active threads out of ' + str(len(threadsList)))
2019-10-16 10:08:21 +00:00
# remove the dormant threads
2020-04-04 12:09:57 +00:00
dormantCtr = 0
2019-10-16 10:08:21 +00:00
for th in dormantThreads:
if debug:
2020-04-04 12:09:57 +00:00
print('DEBUG: Removing dormant thread ' + str(dormantCtr))
dormantCtr += 1
2019-10-16 15:17:00 +00:00
threadsList.remove(th)
th.kill()
2020-04-04 12:09:57 +00:00
changed = True
2019-10-16 18:19:18 +00:00
# start scheduled threads
2020-04-04 12:09:57 +00:00
if len(threadsList) < 10:
ctr = 0
2019-10-16 18:19:18 +00:00
for th in threadsList:
if not th.isStarted:
2020-04-04 12:09:57 +00:00
print('Starting new send thread ' + str(ctr))
2019-10-16 18:19:18 +00:00
th.start()
2020-04-04 12:09:57 +00:00
changed = True
2019-10-16 18:19:18 +00:00
break
2020-04-04 12:09:57 +00:00
ctr += 1
2019-10-16 18:19:18 +00:00
if not changed:
return
if debug:
2021-12-25 16:17:53 +00:00
sendLogFilename = base_dir + '/send.csv'
try:
2021-07-13 14:40:49 +00:00
with open(sendLogFilename, 'a+') as logFile:
logFile.write(currTime.strftime("%Y-%m-%dT%H:%M:%SZ") +
',' + str(noOfActiveThreads) +
',' + str(len(threadsList)) + '\n')
2021-11-25 22:22:54 +00:00
except OSError:
2021-10-29 18:48:15 +00:00
print('EX: removeDormantThreads unable to write ' +
sendLogFilename)