Преглед изворни кода

test without lockfile

test_without_lockfile
Dirk Alders пре 11 месеци
родитељ
комит
268949722e
1 измењених фајлова са 43 додато и 54 уклоњено
  1. 43
    54
      file2mail.py

+ 43
- 54
file2mail.py Прегледај датотеку

@@ -8,68 +8,57 @@ import mail
8 8
 import os
9 9
 import psutil
10 10
 import report
11
+import signal
11 12
 import time
12 13
 
13
-LOCK_FILE = os.path.join(config.TARGET_FOLDER, "lock")
14
-PID = os.getpid()
14
+CYCLE_TIME = 60
15
+SLEEP_TIME = .5
15 16
 
16 17
 logger = logging.getLogger('root')
17 18
 report.stdoutLoggingConfigure(log_name_lvl=[('root', config.LOG_LEVEL), ])
18 19
 
19 20
 
20
-def has_handle(fpath):
21
-    for proc in psutil.process_iter():
22
-        try:
23
-            for item in proc.open_files():
24
-                if fpath == item.path:
25
-                    return True
26
-        except Exception:
27
-            pass
21
+class GracefulKiller:
22
+  kill_now = False
23
+  def __init__(self):
24
+    signal.signal(signal.SIGINT, self.exit_gracefully)
25
+    signal.signal(signal.SIGTERM, self.exit_gracefully)
28 26
 
29
-    return False
27
+  def exit_gracefully(self, signum, frame):
28
+    self.kill_now = True
30 29
 
31 30
 
32
-if __name__ == "__main__":
33
-    while True:
34
-        # Check if LOCK_FILE EXISTS
35
-        if not os.path.exists(LOCK_FILE):
36
-            # Create LOCK_FILE
37
-            with open(LOCK_FILE, 'w') as fh:
38
-                fh.write(str(PID))
39
-            logger.debug("Creating lock-file %s with PID %d", LOCK_FILE, PID)
40
-            # Read LOCK_FILE content
41
-            with open(LOCK_FILE, 'r') as fh:
42
-                lf_pid = json.loads(fh.read())
43
-            # Check if LOCK_FILE has my PID
44
-            if lf_pid == PID:
45
-                # Scan for files to send
46
-                for filename in fstools.filelist(config.TARGET_FOLDER):
47
-                    # Exclude LOCK_FILE
48
-                    if filename != LOCK_FILE and not has_handle(filename):
49
-                        logger.info("Found %s to send via E-Mail to %s.", filename, config.SEND_TO)
50
-                        # Send File as E-Mail
51
-                        try:
52
-                            mail.send_mail(
53
-                                config.SEND_FROM,
54
-                                config.SEND_TO,
55
-                                "Gesanntes Dokument - %s" % os.path.basename(filename),
56
-                                "Hier ist das gescannte Dokument vom Scanner in Buchen:",
57
-                                files=[filename],
58
-                                server=config.SMTP_SERVER,
59
-                                username=config.SMTP_USER,
60
-                                password=config.SMTP_PASSWORD
61
-                            )
62
-                        except Exception:
63
-                            logger.exception("Exception while sending an E-Mail")
64
-                        else:
65
-                            # Remove file
66
-                            logger.debug("Removing file %s", filename)
67
-                            os.remove(filename)
68
-                # Remove LOCK_FILE
69
-                logger.debug("Removing lock-file %s", LOCK_FILE)
70
-                os.remove(LOCK_FILE)
71
-            else:
72
-                logger.warning("LOCK_FILE has PID %d and this proces %d. No action!", lf_pid, PID)
31
+def file2mail():
32
+    # Scan for files to send
33
+    for filename in fstools.filelist(config.TARGET_FOLDER):
34
+        logger.info("Found %s to send via E-Mail to %s.", filename, config.SEND_TO)
35
+        # Send File as E-Mail
36
+        try:
37
+            mail.send_mail(
38
+                config.SEND_FROM,
39
+                config.SEND_TO,
40
+                "Gesanntes Dokument - %s" % os.path.basename(filename),
41
+                "Hier ist das gescannte Dokument vom Scanner in Buchen:",
42
+                files=[filename],
43
+                server=config.SMTP_SERVER,
44
+                username=config.SMTP_USER,
45
+                password=config.SMTP_PASSWORD
46
+            )
47
+        except Exception:
48
+            logger.exception("Exception while sending an E-Mail")
73 49
         else:
74
-            logger.warning("LOCK_FILE %s already exists. No action!", LOCK_FILE)
75
-        time.sleep(60)
50
+            # Remove file
51
+            logger.debug("Removing file %s", filename)
52
+            os.remove(filename)
53
+
54
+
55
+if __name__ == '__main__':
56
+  killer = GracefulKiller()
57
+  i = 0
58
+  while not killer.kill_now:
59
+    time.sleep(.5)
60
+    if i >= CYCLE_TIME / SLEEP_TIME:
61
+        cyclic_job()
62
+        i = 0
63
+    else:
64
+        i += 1

Loading…
Откажи
Сачувај