test without lockfile
This commit is contained in:
parent
2540e1536a
commit
268949722e
55
file2mail.py
55
file2mail.py
@ -8,44 +8,29 @@ import mail
|
|||||||
import os
|
import os
|
||||||
import psutil
|
import psutil
|
||||||
import report
|
import report
|
||||||
|
import signal
|
||||||
import time
|
import time
|
||||||
|
|
||||||
LOCK_FILE = os.path.join(config.TARGET_FOLDER, "lock")
|
CYCLE_TIME = 60
|
||||||
PID = os.getpid()
|
SLEEP_TIME = .5
|
||||||
|
|
||||||
logger = logging.getLogger('root')
|
logger = logging.getLogger('root')
|
||||||
report.stdoutLoggingConfigure(log_name_lvl=[('root', config.LOG_LEVEL), ])
|
report.stdoutLoggingConfigure(log_name_lvl=[('root', config.LOG_LEVEL), ])
|
||||||
|
|
||||||
|
|
||||||
def has_handle(fpath):
|
class GracefulKiller:
|
||||||
for proc in psutil.process_iter():
|
kill_now = False
|
||||||
try:
|
def __init__(self):
|
||||||
for item in proc.open_files():
|
signal.signal(signal.SIGINT, self.exit_gracefully)
|
||||||
if fpath == item.path:
|
signal.signal(signal.SIGTERM, self.exit_gracefully)
|
||||||
return True
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
|
|
||||||
return False
|
def exit_gracefully(self, signum, frame):
|
||||||
|
self.kill_now = True
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
def file2mail():
|
||||||
while True:
|
|
||||||
# Check if LOCK_FILE EXISTS
|
|
||||||
if not os.path.exists(LOCK_FILE):
|
|
||||||
# Create LOCK_FILE
|
|
||||||
with open(LOCK_FILE, 'w') as fh:
|
|
||||||
fh.write(str(PID))
|
|
||||||
logger.debug("Creating lock-file %s with PID %d", LOCK_FILE, PID)
|
|
||||||
# Read LOCK_FILE content
|
|
||||||
with open(LOCK_FILE, 'r') as fh:
|
|
||||||
lf_pid = json.loads(fh.read())
|
|
||||||
# Check if LOCK_FILE has my PID
|
|
||||||
if lf_pid == PID:
|
|
||||||
# Scan for files to send
|
# Scan for files to send
|
||||||
for filename in fstools.filelist(config.TARGET_FOLDER):
|
for filename in fstools.filelist(config.TARGET_FOLDER):
|
||||||
# Exclude LOCK_FILE
|
|
||||||
if filename != LOCK_FILE and not has_handle(filename):
|
|
||||||
logger.info("Found %s to send via E-Mail to %s.", filename, config.SEND_TO)
|
logger.info("Found %s to send via E-Mail to %s.", filename, config.SEND_TO)
|
||||||
# Send File as E-Mail
|
# Send File as E-Mail
|
||||||
try:
|
try:
|
||||||
@ -65,11 +50,15 @@ if __name__ == "__main__":
|
|||||||
# Remove file
|
# Remove file
|
||||||
logger.debug("Removing file %s", filename)
|
logger.debug("Removing file %s", filename)
|
||||||
os.remove(filename)
|
os.remove(filename)
|
||||||
# Remove LOCK_FILE
|
|
||||||
logger.debug("Removing lock-file %s", LOCK_FILE)
|
|
||||||
os.remove(LOCK_FILE)
|
if __name__ == '__main__':
|
||||||
|
killer = GracefulKiller()
|
||||||
|
i = 0
|
||||||
|
while not killer.kill_now:
|
||||||
|
time.sleep(.5)
|
||||||
|
if i >= CYCLE_TIME / SLEEP_TIME:
|
||||||
|
cyclic_job()
|
||||||
|
i = 0
|
||||||
else:
|
else:
|
||||||
logger.warning("LOCK_FILE has PID %d and this proces %d. No action!", lf_pid, PID)
|
i += 1
|
||||||
else:
|
|
||||||
logger.warning("LOCK_FILE %s already exists. No action!", LOCK_FILE)
|
|
||||||
time.sleep(60)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user