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