user gets notified interatively or by email in case of a CIS error
[living-lab-site.git] / cis / cis.py
index 3ae47cf..8bd6575 100755 (executable)
@@ -113,13 +113,18 @@ class CIWorker(threading.Thread):
             # Create torrent file.
             bt.create_torrent(transcode_config['output_file'])
             
-            # The torrent file is created in the same directory with the
-            # source file. Move it to the torrents directory.
-            shutil.move(transcode_config['output_file'] + '.tstream', \
-                    config.TORRENTS_PATH)
-
             output_file = transcode_config['output_file'] + '.tstream'
             output_file = output_file[(output_file.rindex('/') + 1):]
+            
+            # The torrent file is created in the same directory with the
+            # source file. Move it to the torrents directory.
+            #if not os.path.exists(
+            #        os.path.join(output_file, config.TORRENTS_PATH)):
+            try:
+                shutil.move(transcode_config['output_file'] + '.tstream', \
+                        config.TORRENTS_PATH)
+            except:
+                pass
 
             # * SEED TORRENTS
             Server.bit_torrent.start_torrent( \
@@ -151,11 +156,29 @@ class CIWorker(threading.Thread):
         for f in files:
             os.unlink(os.path.join(path, f))
 
-    def notify_completion(self):
+    def notify_completion(self, code):
         logger.log_msg('#%s: notifying web server about the job completion...'\
                 % self.job_id)
         
-        f = urllib.urlopen(config.WS_COMPLETION)
+        if config.WS_COMPLETION[len(config.WS_COMPLETION) - 1] == '/':
+            url = config.WS_COMPLETION + code
+        else:
+            url = config.WS_COMPLETION + '/' + code
+        
+        f = urllib.urlopen(url)
+        f.read()
+        
+    def notify_error(self, code):
+        logger.log_msg('#%s: notifying web server about the error...'\
+                % self.job_id)
+        
+        if config.WS_ERROR[len(config.WS_ERROR) - 1] == '/':
+            url = config.WS_ERROR + code
+        else:
+            url = config.WS_ERROR + '/' + code
+        url = url + '/' + 'internal_error'
+        
+        f = urllib.urlopen(url)
         f.read()
     
     def run(self):
@@ -169,10 +192,12 @@ class CIWorker(threading.Thread):
             except cis_exceptions.FileAlreadyExistsException as e:
                 logger.log_msg('#%s: %s' \
                         % (job['code'], repr(e)), logger.LOG_LEVEL_ERROR)
+                self.notify_error(job['code'])
                 continue
             except Exception as e:
                 logger.log_msg('#%s: error while transferring in: %s' \
-                        % (job['code'], repr(e)), logger.LOG_LEVEL_FATAL) 
+                        % (job['code'], repr(e)), logger.LOG_LEVEL_FATAL)
+                self.notify_error(job['code'])
                 continue
 
             # * TRANSCODE RAW VIDEO
@@ -182,10 +207,12 @@ class CIWorker(threading.Thread):
             except cis_exceptions.FileAlreadyExistsException as e:
                 logger.log_msg('#%s: %s' \
                         % (job['code'], repr(e)), logger.LOG_LEVEL_ERROR)
+                self.notify_error(job['code'])
                 continue
             except Exception as e:
                 logger.log_msg('#%s: error while transcoding: %s' \
-                        % (job['code'], repr(e)), logger.LOG_LEVEL_FATAL) 
+                        % (job['code'], repr(e)), logger.LOG_LEVEL_FATAL)
+                self.notify_error(job['code'])
                 continue
 
             # * EXTRACT THUMBNAIL IMAGES
@@ -196,11 +223,13 @@ class CIWorker(threading.Thread):
                 except cis_exceptions.FileAlreadyExistsException as e:
                     logger.log_msg('#%s: %s' \
                             % (job['code'], repr(e)), logger.LOG_LEVEL_ERROR)
+                    self.notify_error(job['code'])
                     continue
                 except Exception as e:
                     logger.log_msg( \
                             '#%s: error while extracting thumbnail images: %s' \
-                            % (job['code'], repr(e)), logger.LOG_LEVEL_FATAL) 
+                            % (job['code'], repr(e)), logger.LOG_LEVEL_FATAL)
+                    self.notify_error(job['code'])
                     continue
 
             # * CREATE TORRENTS AND START SEEDING OF TRANSCODED VIDEOS
@@ -227,16 +256,17 @@ class CIWorker(threading.Thread):
             except Exception as e:
                 logger.log_msg('#%s: error while transferring out: %s' \
                         % (job['code'], repr(e)), logger.LOG_LEVEL_FATAL) 
+                self.notify_error(job['code'])
                 continue
             
             # * NOTIFY WEB SERVER ABOUT CONTENT INGESTION COMPLETION
-            # TODO in the future web server should also be notified about errors
             try:
-                self.notify_completion()
+                self.notify_completion(job['code'])
             except Exception as e:
                 logger.log_msg(
                         '#%s: error while notifying web server about the job completion: %s' \
-                        % (job['code'], repr(e)), logger.LOG_LEVEL_FATAL) 
+                        % (job['code'], repr(e)), logger.LOG_LEVEL_FATAL)
+                self.notify_error(job['code'])
                 continue
             
             # * CLEANUP RAW VIDEOS AND THUMBNAIL IMAGES
@@ -246,6 +276,8 @@ class CIWorker(threading.Thread):
             # * JOB FINISHED
             Server.queue.task_done()
             Server.load -= job['weight']
+            logger.log_msg('#%s: finished' \
+                        % job['code'], logger.LOG_LEVEL_INFO)                     
 
 
 class Server: