Skip to content

Commit 18a9152

Browse files
committed
#22 fix shutdown logic
1 parent 7ee23e2 commit 18a9152

File tree

1 file changed

+43
-29
lines changed

1 file changed

+43
-29
lines changed

src/main/groovy/repo/build/maven/Build.groovy

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import repo.build.RepoBuildException
99
import java.util.concurrent.ConcurrentHashMap
1010
import java.util.concurrent.ConcurrentMap
1111
import java.util.concurrent.Executors
12+
import java.util.concurrent.LinkedBlockingQueue
13+
import java.util.concurrent.ThreadPoolExecutor
14+
import java.util.concurrent.TimeUnit
1215

1316
/**
1417
* @author Markelov Ruslan markelov@jet.msk.su
@@ -60,39 +63,50 @@ class Build {
6063

6164
Set<MavenArtifactRef> tasks = new HashSet<>(buildStates.keySet())
6265

63-
def pool = Executors.newFixedThreadPool(context.getParallel())
64-
while (!tasks.isEmpty()) {
65-
def iter = tasks.iterator()
66-
while (iter.hasNext()) {
67-
def key = iter.next()
68-
def component = componentMap.get(key)
69-
def deps = buildDeps.get(key)
70-
if (!deps.any { buildStates.get(it) != BuildState.SUCCESS }) {
71-
pool.execute {
72-
// build component
73-
try {
74-
def pomFile = new File(component.basedir, 'pom.xml')
75-
Maven.execute(context, pomFile, ['clean', 'install'])
76-
buildStates.put(key, BuildState.SUCCESS)
77-
} catch (Exception e) {
78-
buildStates.put(key, BuildState.ERROR)
79-
context.setErrorFlag()
80-
context.addError(new RepoBuildException(" component ${component.path} build ERROR", e))
66+
def executionQueue = new LinkedBlockingQueue<Runnable>()
67+
def pool = new ThreadPoolExecutor(context.getParallel(), context.getParallel(),
68+
0L, TimeUnit.MILLISECONDS,
69+
executionQueue)
70+
try {
71+
while (!tasks.isEmpty()) {
72+
def iter = tasks.iterator()
73+
while (iter.hasNext()) {
74+
def key = iter.next()
75+
def component = componentMap.get(key)
76+
def deps = buildDeps.get(key)
77+
if (!deps.any { buildStates.get(it) != BuildState.SUCCESS }) {
78+
pool.execute {
79+
// build component
80+
try {
81+
def pomFile = new File(component.basedir, 'pom.xml')
82+
try {
83+
Maven.execute(context, pomFile, ['clean'])
84+
} catch (RepoBuildException ignore) {
85+
}
86+
Maven.execute(context, pomFile, ['install'])
87+
buildStates.put(key, BuildState.SUCCESS)
88+
} catch (Exception e) {
89+
buildStates.put(key, BuildState.ERROR)
90+
context.setErrorFlag()
91+
context.addError(new RepoBuildException(" component ${component.path} build ERROR", e))
92+
}
8193
}
94+
iter.remove()
95+
} else if (deps.any {
96+
buildStates.get(it) == BuildState.DEPS_ERROR ||
97+
buildStates.get(it) == BuildState.ERROR
98+
}) {
99+
buildStates.put(key, BuildState.DEPS_ERROR)
100+
context.setErrorFlag()
101+
context.addError(new RepoBuildException(" component ${component.path} build DEPS_ERROR"))
102+
iter.remove()
82103
}
83-
iter.remove()
84-
} else if (deps.any {
85-
buildStates.get(it) == BuildState.DEPS_ERROR ||
86-
buildStates.get(it) == BuildState.ERROR
87-
}) {
88-
buildStates.put(key, BuildState.DEPS_ERROR)
89-
context.setErrorFlag()
90-
context.addError(new RepoBuildException(" component ${component.path} build DEPS_ERROR"))
91-
iter.remove()
92104
}
105+
// throttle cpu
106+
Thread.sleep(500L)
93107
}
94-
// throttle cpu
95-
Thread.sleep(500L)
108+
} finally {
109+
pool.shutdown()
96110
}
97111
return !context.getErrorFlag()
98112
}

0 commit comments

Comments
 (0)