11import os
22import logging
3- import sys
4-
5- from signal import signal , SIGINT , SIGTERM
63
74from mongodb_consistent_backup .Common import LocalCommand
5+ from mongodb_consistent_backup .Pipeline import PoolThread
86
97
10- class TarThread :
11- def __init__ (self , backup_dir , output_file , do_gzip = False , verbose = False , binary = "tar" ):
12- self .backup_dir = backup_dir
13- self .output_file = output_file
14- self .do_gzip = do_gzip
15- self .verbose = verbose
16- self .binary = binary
17-
18- self ._command = None
8+ class TarThread (PoolThread ):
9+ def __init__ (self , backup_dir , output_file , compression = 'none' , verbose = False , binary = "tar" ):
10+ super (TarThread , self ).__init__ (self .__class__ .__name__ , compression )
11+ self .compression_method = compression
12+ self .backup_dir = backup_dir
13+ self .output_file = output_file
14+ self .verbose = verbose
15+ self .binary = binary
1916
20- signal (SIGINT , self .close )
21- signal (SIGTERM , self .close )
17+ self ._command = None
2218
2319 def close (self , exit_code = None , frame = None ):
24- if self ._command :
25- logging .debug ("Killing running subprocess/ command: %s" % self ._command .command )
20+ if self ._command and not self . stopped :
21+ logging .debug ("Stopping running tar command: %s" % self ._command .command )
2622 del exit_code
2723 del frame
2824 self ._command .close ()
25+ self .stopped = True
2926
3027 def run (self ):
3128 if os .path .isdir (self .backup_dir ):
@@ -37,17 +34,20 @@ def run(self):
3734 log_msg = "Archiving directory: %s" % self .backup_dir
3835 cmd_flags = ["-C" , backup_base_dir , "-cf" , self .output_file , "--remove-files" , backup_base_name ]
3936
40- if self .do_gzip :
37+ if self .do_gzip () :
4138 log_msg = "Archiving and compressing directory: %s" % self .backup_dir
4239 cmd_flags = ["-C" , backup_base_dir , "-czf" , self .output_file , "--remove-files" , backup_base_name ]
4340
4441 logging .info (log_msg )
42+ self .running = True
4543 self ._command = LocalCommand (self .binary , cmd_flags , self .verbose )
46- self ._command .run ()
44+ self .exit_code = self . _command .run ()
4745 except Exception , e :
4846 logging .fatal ("Failed archiving file: %s! Error: %s" % (self .output_file , e ))
49- sys .exit (1 )
47+ finally :
48+ self .running = False
49+ self .stopped = True
50+ self .completed = True
5051 else :
5152 logging .fatal ("Output file: %s already exists!" % self .output_file )
52- sys .exit (1 )
5353 return self .backup_dir
0 commit comments