@@ -13,21 +13,21 @@ class FileDoesntExistException < Exception; end
1313 class DeploymentCommandTracker
1414 DEPLOYMENT_EVENT_FILE_STALE_TIMELIMIT_SECONDS = 86400 # 24 hour limit in secounds
1515
16- def self . create_ongoing_deployment_tracking_file ( deployment_id )
16+ def self . create_ongoing_deployment_tracking_file ( deployment_id , host_command_identifier )
1717 FileUtils . mkdir_p ( deployment_dir_path ( ) )
18- FileUtils . touch ( deployment_event_tracking_file_path ( deployment_id ) ) ;
18+ File . write ( deployment_event_tracking_file_path ( deployment_id ) , host_command_identifier )
1919 end
2020
2121 def self . delete_deployment_tracking_file_if_stale? ( deployment_id , timeout )
22- if ( Time . now - File . ctime ( deployment_event_tracking_file_path ( deployment_id ) ) > timeout )
22+ if ( Time . now - File . mtime ( deployment_event_tracking_file_path ( deployment_id ) ) > timeout )
2323 delete_deployment_command_tracking_file ( deployment_id )
2424 return true ;
2525 end
2626 return false ;
2727 end
2828
2929 def self . check_deployment_event_inprogress?
30- if ( File . exists? deployment_dir_path ( ) )
30+ if ( File . exist? ( deployment_dir_path ( ) ) )
3131 return directories_and_files_inside ( deployment_dir_path ( ) ) . any? { |deployment_id | check_if_lifecycle_event_is_stale? ( deployment_id ) }
3232 else
3333 return false
@@ -36,7 +36,7 @@ def self.check_deployment_event_inprogress?
3636
3737 def self . delete_deployment_command_tracking_file ( deployment_id )
3838 ongoing_deployment_event_file_path = deployment_event_tracking_file_path ( deployment_id )
39- if File . exists? ongoing_deployment_event_file_path
39+ if File . exist? ( ongoing_deployment_event_file_path )
4040 File . delete ( ongoing_deployment_event_file_path ) ;
4141 else
4242 InstanceAgent ::Log . warn ( "the tracking file does not exist" )
@@ -46,8 +46,18 @@ def self.delete_deployment_command_tracking_file(deployment_id)
4646 def self . directories_and_files_inside ( directory )
4747 Dir . entries ( directory ) - %w( .. . )
4848 end
49-
50- private
49+
50+ def self . most_recent_host_command_identifier
51+ # check_deployment_event_inprogress handles deleting stale files for us.
52+ if check_deployment_event_inprogress? then
53+ most_recent_id = directories_and_files_inside ( deployment_dir_path ( ) ) . max_by { |filename | File . mtime ( deployment_event_tracking_file_path ( filename ) ) }
54+ most_recent_file = deployment_event_tracking_file_path ( most_recent_id )
55+ return File . read ( most_recent_file )
56+ else
57+ return nil
58+ end
59+ end
60+
5161 def self . deployment_dir_path
5262 File . join ( InstanceAgent ::Config . config [ :root_dir ] , InstanceAgent ::Config . config [ :ongoing_deployment_tracking ] )
5363 end
@@ -57,8 +67,12 @@ def self.check_if_lifecycle_event_is_stale?(deployment_id)
5767 end
5868
5969 def self . deployment_event_tracking_file_path ( deployment_id )
60- ongoing_deployment_file_path = File . join ( deployment_dir_path ( ) , deployment_id )
61- end
70+ return File . join ( deployment_dir_path ( ) , deployment_id )
71+ end
72+
73+ def self . clean_ongoing_deployment_dir
74+ FileUtils . rm_r ( InstanceAgent ::Plugins ::CodeDeployPlugin ::DeploymentCommandTracker . deployment_dir_path ( ) ) rescue Errno ::ENOENT
75+ end
6276 end
6377 end
6478 end
0 commit comments