1 package org.apache.continuum.buildagent.taskqueue.manager;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.lang.ArrayUtils;
23 import org.apache.continuum.buildagent.taskqueue.PrepareBuildProjectsTask;
24 import org.apache.continuum.taskqueue.BuildProjectTask;
25 import org.apache.continuum.taskqueue.manager.TaskQueueManagerException;
26 import org.apache.continuum.utils.build.BuildTrigger;
27 import org.codehaus.plexus.PlexusConstants;
28 import org.codehaus.plexus.PlexusContainer;
29 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
30 import org.codehaus.plexus.context.Context;
31 import org.codehaus.plexus.context.ContextException;
32 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
33 import org.codehaus.plexus.taskqueue.Task;
34 import org.codehaus.plexus.taskqueue.TaskQueue;
35 import org.codehaus.plexus.taskqueue.TaskQueueException;
36 import org.codehaus.plexus.taskqueue.execution.TaskQueueExecutor;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 import java.util.List;
41
42
43
44
45 public class DefaultBuildAgentTaskQueueManager
46 implements BuildAgentTaskQueueManager, Contextualizable
47 {
48 private static final Logger log = LoggerFactory.getLogger( DefaultBuildAgentTaskQueueManager.class );
49
50
51
52
53 private TaskQueue buildAgentBuildQueue;
54
55
56
57
58 private TaskQueue buildAgentPrepareBuildQueue;
59
60 private PlexusContainer container;
61
62 public void cancelBuild()
63 throws TaskQueueManagerException
64 {
65 Task task = getBuildTaskQueueExecutor().getCurrentTask();
66
67 if ( task != null )
68 {
69 if ( task instanceof BuildProjectTask )
70 {
71 log.info( "Cancelling current build task of project " + ( (BuildProjectTask) task ).getProjectId() );
72 getBuildTaskQueueExecutor().cancelTask( task );
73 }
74 else
75 {
76 log.warn( "Current task not a BuildProjectTask - not cancelling" );
77 }
78 }
79 else
80 {
81 log.warn( "No task running - not cancelling" );
82 }
83 }
84
85 public TaskQueue getBuildQueue()
86 {
87 return buildAgentBuildQueue;
88 }
89
90 public int getIdOfProjectCurrentlyBuilding()
91 throws TaskQueueManagerException
92 {
93 Task task = getBuildTaskQueueExecutor().getCurrentTask();
94 if ( task != null )
95 {
96 if ( task instanceof BuildProjectTask )
97 {
98 log.debug( "Current project building: {}", ( (BuildProjectTask) task ).getProjectName() );
99 return ( (BuildProjectTask) task ).getProjectId();
100 }
101 }
102 return -1;
103 }
104
105 public TaskQueue getPrepareBuildQueue()
106 {
107 return buildAgentPrepareBuildQueue;
108 }
109
110 private void removeProjectsFromBuildQueue()
111 throws TaskQueueManagerException
112 {
113 try
114 {
115 List<BuildProjectTask> queues = buildAgentBuildQueue.getQueueSnapshot();
116
117 if ( queues != null )
118 {
119 for ( BuildProjectTask task : queues )
120 {
121 if ( task != null )
122 {
123 log.info( "remove project '{}' from build queue", task.getProjectName() );
124 buildAgentBuildQueue.remove( task );
125 }
126 }
127 }
128 else
129 {
130 log.info( "no build task in queue" );
131 }
132 }
133 catch ( TaskQueueException e )
134 {
135 throw new TaskQueueManagerException( "Error while getting build tasks from queue", e );
136 }
137 }
138
139 public TaskQueueExecutor getBuildTaskQueueExecutor()
140 throws TaskQueueManagerException
141 {
142 try
143 {
144 return (TaskQueueExecutor) container.lookup( TaskQueueExecutor.class, "build-agent" );
145 }
146 catch ( ComponentLookupException e )
147 {
148 throw new TaskQueueManagerException( e.getMessage(), e );
149 }
150 }
151
152 public TaskQueueExecutor getPrepareBuildTaskQueueExecutor()
153 throws TaskQueueManagerException
154 {
155 try
156 {
157 return (TaskQueueExecutor) container.lookup( TaskQueueExecutor.class, "prepare-build-agent" );
158 }
159 catch ( ComponentLookupException e )
160 {
161 throw new TaskQueueManagerException( e.getMessage(), e );
162 }
163 }
164
165 public boolean hasBuildTaskInQueue()
166 throws TaskQueueManagerException
167 {
168 try
169 {
170 if ( getBuildQueue().getQueueSnapshot() != null && getBuildQueue().getQueueSnapshot().size() > 0 )
171 {
172 return true;
173 }
174 }
175 catch ( TaskQueueException e )
176 {
177 throw new TaskQueueManagerException( e.getMessage(), e );
178 }
179 return false;
180 }
181
182 public boolean isProjectInBuildQueue( int projectId )
183 throws TaskQueueManagerException
184 {
185 try
186 {
187 List<BuildProjectTask> queues = buildAgentBuildQueue.getQueueSnapshot();
188
189 if ( queues != null )
190 {
191 for ( BuildProjectTask task : queues )
192 {
193 if ( task != null && task.getProjectId() == projectId )
194 {
195 log.debug( "project {} is in build queue", task.getProjectName() );
196 return true;
197 }
198 }
199 }
200 else
201 {
202 log.info( "no build task in queue" );
203 }
204 }
205 catch ( TaskQueueException e )
206 {
207 throw new TaskQueueManagerException( e.getMessage(), e );
208 }
209
210 return false;
211 }
212
213 public boolean isInPrepareBuildQueue( int projectGroupId, BuildTrigger buildTrigger, String scmRootAddress )
214 throws TaskQueueManagerException
215 {
216 try
217 {
218 List<PrepareBuildProjectsTask> queues = buildAgentPrepareBuildQueue.getQueueSnapshot();
219
220 if ( queues != null )
221 {
222 for ( PrepareBuildProjectsTask task : queues )
223 {
224 if ( task != null && task.getProjectGroupId() == projectGroupId &&
225 task.getBuildTrigger().getTrigger() == buildTrigger.getTrigger() &&
226 task.getScmRootAddress().equals( scmRootAddress ) )
227 {
228 log.info( "project group {} in prepare build queue", task.getProjectGroupId() );
229 return true;
230 }
231 }
232 }
233 else
234 {
235 log.info( "no prepare build task in queue" );
236 }
237 }
238 catch ( TaskQueueException e )
239 {
240 throw new TaskQueueManagerException( e.getMessage(), e );
241 }
242
243 return false;
244 }
245
246 public List<PrepareBuildProjectsTask> getProjectsInPrepareBuildQueue()
247 throws TaskQueueManagerException
248 {
249 try
250 {
251 return buildAgentPrepareBuildQueue.getQueueSnapshot();
252 }
253 catch ( TaskQueueException e )
254 {
255 log.error( "Error occurred while retrieving projects in prepare build queue", e );
256 throw new TaskQueueManagerException( "Error occurred while retrieving projects in prepare build queue", e );
257 }
258 }
259
260 public List<BuildProjectTask> getProjectsInBuildQueue()
261 throws TaskQueueManagerException
262 {
263 try
264 {
265 return buildAgentBuildQueue.getQueueSnapshot();
266 }
267 catch ( TaskQueueException e )
268 {
269 log.error( "Error occurred while retrieving projects in build queue", e );
270 throw new TaskQueueManagerException( "Error occurred while retrieving projects in build queue", e );
271 }
272 }
273
274 public PrepareBuildProjectsTask getCurrentProjectInPrepareBuild()
275 throws TaskQueueManagerException
276 {
277 Task task = getPrepareBuildTaskQueueExecutor().getCurrentTask();
278
279 if ( task != null )
280 {
281 log.debug( "Current project group preparing build: {}",
282 ( (PrepareBuildProjectsTask) task ).getProjectGroupId() );
283 return (PrepareBuildProjectsTask) task;
284 }
285 return null;
286 }
287
288 public BuildProjectTask getCurrentProjectInBuilding()
289 throws TaskQueueManagerException
290 {
291 Task task = getBuildTaskQueueExecutor().getCurrentTask();
292
293 if ( task != null )
294 {
295 log.debug( "Current project building: {}", ( (BuildProjectTask) task ).getProjectName() );
296 return (BuildProjectTask) task;
297 }
298
299 return null;
300 }
301
302 public boolean removeFromPrepareBuildQueue( int projectGroupId, int scmRootId )
303 throws TaskQueueManagerException
304 {
305 List<PrepareBuildProjectsTask> tasks = getProjectsInPrepareBuildQueue();
306
307 if ( tasks != null )
308 {
309 for ( PrepareBuildProjectsTask task : tasks )
310 {
311 if ( task != null && task.getProjectGroupId() == projectGroupId && task.getScmRootId() == scmRootId )
312 {
313 log.debug( "Remove project group {} from prepare build queue", projectGroupId );
314 return getPrepareBuildQueue().remove( task );
315 }
316 }
317 }
318
319 return false;
320 }
321
322 public void removeFromPrepareBuildQueue( int[] hashCodes )
323 throws TaskQueueManagerException
324 {
325 List<PrepareBuildProjectsTask> tasks = getProjectsInPrepareBuildQueue();
326
327 if ( tasks != null )
328 {
329 for ( PrepareBuildProjectsTask task : tasks )
330 {
331 if ( task != null && ArrayUtils.contains( hashCodes, task.getHashCode() ) )
332 {
333 log.debug( "Remove project group '{}' from prepare build queue", task.getProjectGroupId() );
334 getPrepareBuildQueue().remove( task );
335 }
336 }
337 }
338 }
339
340 public boolean removeFromBuildQueue( int projectId, int buildDefinitionId )
341 throws TaskQueueManagerException
342 {
343 List<BuildProjectTask> tasks = getProjectsInBuildQueue();
344
345 if ( tasks != null )
346 {
347 for ( BuildProjectTask task : tasks )
348 {
349 if ( task != null && task.getProjectId() == projectId &&
350 task.getBuildDefinitionId() == buildDefinitionId )
351 {
352 log.debug( "Remove project {} with buildDefinition{} from build queue", task.getProjectName(),
353 task.getBuildDefinitionId() );
354 return getBuildQueue().remove( task );
355 }
356 }
357 }
358
359 return false;
360 }
361
362 public void removeFromBuildQueue( int[] hashCodes )
363 throws TaskQueueManagerException
364 {
365 List<BuildProjectTask> tasks = getProjectsInBuildQueue();
366
367 if ( tasks != null )
368 {
369 for ( BuildProjectTask task : tasks )
370 {
371 if ( task != null && ArrayUtils.contains( hashCodes, task.getHashCode() ) )
372 {
373 log.debug( "Remove project '{}' from build queue", task.getProjectName() );
374 getBuildQueue().remove( task );
375 }
376 }
377 }
378 }
379
380 public void contextualize( Context context )
381 throws ContextException
382 {
383 container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
384 }
385
386 }