I wrote a script in Python that is running for some time and performing a clean up as soon as it terminates regularly like:
import time
def cleanUp():
#delete some temporary files and so on
print("Clean up done")
if __name__=="__main__":
startTime = time.time()
while time.time() - startTime < 60:
# Performing some commands
print("Still running")
cleanUp()
I'm running this code as a Jenkins job. In order to clean up no matter what, i.e. also when I abort the Jenkins job, I used signal.signal. Doing research on the signal that is sent by Jenkins in order to terminate a job (https://gist.github.com/datagrok/dfe9604cb907523f4a2f) resulted that Jenkins sends "TERM" signal upon stopping the process. Therefore I added the following + a control mechanism to see whether it works, as Jenkins will not show the rest of the log after stopping a job (will immediately show "ABORTED", see link):
import time
import signal
pythonObj = None
def cleanUp():
# object that I defined globally, reconstructing it in a separate script is quite cumbersome
global pythonObj
#delete some temporary files and so on
print("Clean up done")
# Write some text to a file, to see if this part was actually run
with open(r"C:\temp\abx.txt", 'w') as file:
file.write("Process has been finished prematurely")
signal.signal(signal.SIGTERM, cleanUp)
if __name__=="__main__":
startTime = time.time()
while time.time() - startTime < 60:
# Creation of python object
pythonObj = someFunct()
print("Still running")
cleanUp()
Sadly, I cannot find the "control" file in C:\temp of the PC on which the Jenkins Job is run.
My question: Is it a proper approach to use signal.signal(signal.SIGTERM, cleanUp) (or should I use something else?) or did I make mistake in the usage of signal.signal?
I also tried to register cleanUp with atexit but this didn't give me the results I hoped for either.
EDIT 1: One thing that I didn't mention above is the fact, that I need to hand over some complex inputs to the cleanUp function. I have already thought about using post build actions within a pipeline.
EDIT 2: As mentioned below, in theory it would be possible to hand over the inputs to recreate the object. However, this recreation is quite cumbersome and since I only want to have a quick clean up, which is not possible if I need to recreate the whole process, I try to avoid this option.
Thanks a lot!