Update Tizen 2.0 SDK source code
[sdk/tools/sdk-build.git] / src / build_server / JobClean.rb
1 =begin
2  
3  JobClean.rb
4
5 Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6
7 Contact:
8 Taejun Ha <taejun.ha@samsung.com>
9 Jiil Hyoun <jiil.hyoun@samsung.com>
10 Donghyuk Yang <donghyuk.yang@samsung.com>
11 DongHee Yang <donghee.yang@samsung.com>
12
13 Licensed under the Apache License, Version 2.0 (the "License");
14 you may not use this file except in compliance with the License.
15 You may obtain a copy of the License at
16
17 http://www.apache.org/licenses/LICENSE-2.0
18
19 Unless required by applicable law or agreed to in writing, software
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
24
25 Contributors:
26 - S-Core Co., Ltd
27 =end
28
29 require "fileutils"
30 require "thread"
31 $LOAD_PATH.unshift File.dirname(__FILE__)
32 $LOAD_PATH.unshift File.dirname(File.dirname(__FILE__))+"/common"
33 require "BuildServer.rb"
34 require "Action.rb"
35 require "ScheduledActionHandler.rb"
36
37 $access_listfile = Mutex.new
38
39 class JobCleanAction < Action
40         
41         def initialize( time, job_path, list_file, server )
42                 super(time,0)
43
44                 @job_path = job_path
45                 @job_id = @job_path.split("/")[-1]
46                 @list_file = list_file
47                 @server = server
48         end
49
50
51         def init
52                 $access_listfile.synchronize {
53                         File.open(@list_file, "a") do |f|
54                                 f.puts "#{@job_id},#{time.year},#{time.month},#{time.day},#{time.hour},#{time.min},#{time.sec}" 
55                         end
56                 }
57         end
58
59
60         def execute
61                 # Start to clean job
62                 @server.log.info "Executing clean action for the job #{@job_id}"
63                 begin
64                         execute_internal()
65                 rescue => e
66                         @server.log.error e.message
67                         @server.log.error e.backtrace.inspect
68                 end
69         end
70
71
72         private
73         def execute_internal()
74                 # remove directories
75                 if File.exist? "#{@job_path}/buildroot" then
76                         FileUtils.rm_rf "#{@job_path}/buildroot"
77                 end
78                 if File.exist? "#{@job_path}/temp" then 
79                         FileUtils.rm_rf "#{@job_path}/temp"
80                 end
81                 if File.exist? "#{@job_path}/external_pkgs" then 
82                         FileUtils.rm_rf "#{@job_path}/external_pkgs"
83                 end
84
85                 # remove line for the job
86                 $access_listfile.synchronize {
87                         lines = []
88                         # get all lines
89                         if File.exist? @list_file then
90                                 File.open(@list_file,"r") do |f|
91                                         f.each_line do |l|
92                                                 lines.push l
93                                         end
94                                 end
95                         end
96
97                         # write the line except my job_id
98                         File.open(@list_file,"w") do |f|
99                                 lines.each do |l|
100                                         if l.split(",")[0].eql? @job_id then next end
101                                         f.puts l
102                                 end
103                         end
104                 }
105         end     
106 end
107
108
109 class JobCleaner 
110         attr_accessor :quit
111
112         # init
113         def initialize( server )
114                 @server = server
115                 @handler = ScheduledActionHandler.new
116                 @list_file = "#{BuildServer::CONFIG_ROOT}/#{@server.id}/clean"
117         end
118
119
120         # start thread
121         def start()
122
123                 list_file2 = "#{BuildServer::CONFIG_ROOT}/#{@server.id}/clean_backup"
124                 jobs_path = "#{@server.path}/jobs"
125                 if not File.exist? jobs_path then
126                         FileUtils.mkdir_p jobs_path
127                 end
128
129                 # read clean list
130                 clean_list = []
131                 if File.exist? @list_file then
132                         FileUtils.mv(@list_file,list_file2)
133                         File.open(list_file2, "r") do |f|
134                                 f.each_line do |l|
135                                         id = l.split(",")[0]    
136                                         year = l.split(",")[1]  
137                                         month = l.split(",")[2] 
138                                         day = l.split(",")[3]   
139                                         hour = l.split(",")[4]  
140                                         min = l.split(",")[5]   
141                                         sec = l.split(",")[6]
142                         
143                                         # create job and register       
144                                         job_path = "#{jobs_path}/#{id}" 
145                                         time = Time.mktime(year.to_i, month.to_i, day.to_i, hour.to_i, min.to_i, sec.to_i)      
146                                         @server.log.info "Registered clean-action for the job in list : #{id}"
147                                         @handler.register(JobCleanAction.new(time,job_path,@list_file, @server))
148
149                                         # add clean list
150                                         clean_list.push id      
151                                 end
152                         end
153                 end
154
155
156                 # scan all jobs
157                 Dir.new(jobs_path).entries.each do |id|
158                         # skip . or ..
159                         if id.eql? "." or id.eql? ".." then next end
160
161                         if not clean_list.include? id then
162                                 job_path = "#{jobs_path}/#{id}" 
163                                 time = Time.now
164                                 @server.log.info "Registered clean-action for old job : #{id}"
165                                 @handler.register(JobCleanAction.new(time,job_path,@list_file, @server))
166                         end
167                 end
168
169                 # start handler
170                 @handler.start 
171         end
172
173         
174         # clean after some time
175         def clean_afterwards(job_id)
176                 time = Time.now + @server.keep_time
177                 job_path = "#{@server.path}/jobs/#{job_id}"
178                 @handler.register(JobCleanAction.new(time, job_path, @list_file, @server))
179
180                 @server.log.info "Registered delayed clean-action for the job #{job_id}"
181         end
182
183
184         # clean directly 
185         def clean(job_id)
186                 time = Time.now
187                 job_path = "#{@server.path}/jobs/#{job_id}"
188                 @handler.register(JobCleanAction.new(time, job_path, @list_file, @server))
189
190                 @server.log.info "Registered clean-action for the job #{job_id}"
191         end
192 end