Browse Source

almost complete package upload

tangs 6 years ago
parent
commit
3d93920bc1
1 changed files with 25 additions and 11 deletions
  1. 25 11
      upload.py

+ 25 - 11
upload.py

@@ -6,6 +6,7 @@ import logging
 import datetime
 from os import path
 import time
+import shutil
 
 
 class Handle:
@@ -60,17 +61,20 @@ class Handle:
         return content
 
     def to_file(self, name):
-        # file_name = path.join(self.official_dir, name)
-        # f = open(file_name, encoding='utf-8')
-        #
-        # copy temp file to official file.
-        return 'error'
+        try:
+            shutil.copy(self.temp_file, name)
+        except Exception as e:
+            return e
+        return None
 
     def listen(self):
         temp_file_mode = 'a+'
         temp_file_ptr = self.create_temp_file(temp_file_mode)
+        beg = int(datetime.datetime.now().timestamp())
+        end = beg
         while True:
             self.sleep()
+            end = int(datetime.datetime.now().timestamp())
             self.lock.acquire()
             q = self.Q
             self.lock.release()
@@ -91,28 +95,38 @@ class Handle:
                 except Exception as e:
                     logging.error("[HANDLE] write item(%s) error :%s", item_value, e)
 
+            logging.debug('[HANDLE] listen and ')
+
             # If the currently recorded data line is greater than or equal to the largest data line,
             # the temporary file is written to the official file.
-            if self.current_row >= self.max_item_length:
+            # If the file has not been saved for more than three minutes, and the temporary file is
+            # not empty, an official file is also saved.
+            official_file = ''
+            if self.current_row >= self.max_item_length or (end - beg >= 3 * 60 and self.current_row > 0):
                 try:
                     now = int(datetime.datetime.now().timestamp() * 1000)
-                    file_name = str(now) + '.tok'
-                    msg = self.to_file(file_name)
+                    official_file = path.join(self.official_dir, str(now) + '.tok')
+                    msg = self.to_file(official_file)
                     if msg is not None:
-                        logging.error('[HANDLE] to official file(%s) error: %s', file_name, msg)
+                        logging.error('[HANDLE] to official file(%s) error: %s', official_file, msg)
+                        temp_file_mode = 'a+'
                         continue
 
                     temp_file_ptr.close()
                     temp_file_mode = 'w+'
+                    self.current_row = 0
+
+                    beg = int(datetime.datetime.now().timestamp())
                 except Exception as e:
-                    print(e)
+                    logging.error('[HANDLE] copy temp file(%s) to official file(%s) error: %s',
+                                  self.temp_file, official_file, e)
 
     def create_temp_file(self, mode):
         try:
             f = open(self.temp_file, mode=mode)
             return f
         except Exception as e:
-            logging.error("[HANDLE] create temp file(%s) error: %s", self.temp_file, e)
+            logging.error('[HANDLE] create temp file(%s) error: %s', self.temp_file, e)
             return None
 
     @staticmethod