-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhotrun.gradle
240 lines (212 loc) · 7.02 KB
/
hotrun.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright (c) 2018 Gonzalo Müller Bravo.
// Licensed under the MIT License (MIT), see LICENSE.txt
import java.time.Instant
import javax.inject.Inject
import org.gradle.tooling.BuildLauncher
import org.gradle.tooling.GradleConnector
import org.gradle.tooling.ProjectConnection
class ConcurrentTask extends DefaultTask {
private final WorkerExecutor workerExecutor
private final Class concurrentClass
public String projectDir
public String task
public boolean showOutput = false
public boolean showError = false
public String outputFilePath = ''
public String errorFilePath = ''
public Object[] extraParams
ConcurrentTask(final WorkerExecutor workerExecutor, final Class concurrentClass) {
this.workerExecutor = workerExecutor
this.concurrentClass = concurrentClass
}
@TaskAction
void run() {
workerExecutor.submit(concurrentClass) { config ->
config.isolationMode = IsolationMode.PROCESS
if (extraParams != null) {
config.params projectDir, task, showOutput, showError, outputFilePath, errorFilePath, extraParams
} else {
config.params projectDir, task, showOutput, showError, outputFilePath, errorFilePath
}
}
}
}
class SharedOutputStream extends OutputStream {
private final Collection<OutputStream> streams
SharedOutputStream(final Collection<OutputStream> streams) {
if (streams == null) {
throw new NullPointerException()
}
if (streams.any { it == null }) {
throw new NullPointerException()
}
this.streams = Collections.unmodifiableCollection(streams)
}
void close() {
streams.each { it.close() }
}
void flush() {
streams.each { it.flush() }
}
void write(final byte[] b) {
streams.each { it.write(b) }
}
void write(final byte[] b, final int off, final int len) {
streams.each { it.write(b, off, len) }
}
void write(final int b) {
streams.each { it.write(b) }
}
}
abstract class ConcurrentTaskUnitOfWork implements Runnable {
private final String projectDir
private final String task
private final String outputFilePath
private final String errorFilePath
private final boolean showOutput
private final boolean showError
private OutputStream outputStreams
private OutputStream printStream
private OutputStream errorStreams
protected ConcurrentTaskUnitOfWork(final String projectDir, final String task, final boolean showOutput, final boolean showError,
final String outputFilePath, final String errorFilePath) {
this.projectDir = projectDir
this.task = task
this.showOutput = showOutput
this.showError = showError
this.outputFilePath = outputFilePath == null || outputFilePath.isEmpty() ? null : outputFilePath
this.errorFilePath = errorFilePath == null || errorFilePath.isEmpty() ? null : errorFilePath
}
private OutputStream defineStream(final boolean show, final OutputStream stream, final String path) {
final Collection<OutputStream> outputs = []
if (show) {
outputs.add(stream)
}
if (path != null) {
outputs.add(new FileOutputStream(path))
}
outputs.isEmpty() ? null : new SharedOutputStream(outputs)
}
protected final void arrangeStreams() {
outputStreams = defineStream(showOutput, System.out, outputFilePath)
printStream = outputStreams != null ? new PrintStream(outputStreams) : null
errorStreams = defineStream(showError, System.err, errorFilePath)
}
protected final void closeStreams() {
outputStreams?.close()
printStream?.close()
errorStreams?.close()
}
protected final ProjectConnection createNewConnection() {
GradleConnector.newConnector()
.forProjectDirectory(new File(projectDir))
.connect()
}
protected final BuildLauncher buildLauncher(final ProjectConnection connection) {
final launcher = connection.newBuild().forTasks(task)
if (outputStreams != null) {
launcher.standardOutput = outputStreams
}
if (errorStreams != null) {
launcher.standardError = errorStreams
}
launcher
}
protected final void output(final String text) {
if (outputStreams != null) {
printStream.println(text)
}
}
}
class ScheduledEndlessTaskUnitOfWork extends ConcurrentTaskUnitOfWork {
private final int scheduledTime
private final boolean showRun
@Inject
ScheduledEndlessTaskUnitOfWork(final String projectDir, final String task, final boolean showOutput, final boolean showError,
final String outputFilePath, final String errorFilePath, final Object[] extraParams) {
super(projectDir, task, showOutput, showError, outputFilePath, errorFilePath)
this.scheduledTime = extraParams[0]
this.showRun = extraParams[1]
}
@Override
void run() {
arrangeStreams()
try {
int runNumber = 0
while (true) {
final connection = createNewConnection()
try {
Thread.sleep(scheduledTime)
buildLauncher(connection).run()
if (showRun) {
output("Run:${++runNumber} ${Instant.now()}")
}
} finally {
connection.close()
}
}
} finally {
closeStreams()
}
}
}
class ScheduledEndlessTask extends ConcurrentTask {
public int scheduledTime = 1000
public boolean showRun = true
@Inject
ScheduledEndlessTask(final WorkerExecutor workerExecutor) {
super(workerExecutor, ScheduledEndlessTaskUnitOfWork)
extraParams = [scheduledTime, showRun]
}
}
class SimultaneousTaskUnitOfWork extends ConcurrentTaskUnitOfWork {
@Inject
SimultaneousTaskUnitOfWork(final String projectDir, final String task, final boolean showOutput, final boolean showError,
final String outputFilePath, final String errorFilePath) {
super(projectDir, task, showOutput, showError, outputFilePath, errorFilePath)
}
@Override
void run() {
final connection = createNewConnection()
try {
arrangeStreams()
buildLauncher(connection).run()
} finally {
connection.close()
closeStreams()
}
}
}
class SimultaneousTask extends ConcurrentTask {
@Inject
SimultaneousTask(final WorkerExecutor workerExecutor) {
super(workerExecutor, SimultaneousTaskUnitOfWork)
}
}
task concurrentClasses(type: ScheduledEndlessTask) {
// ScheduledEndlessTask task settings
projectDir = project.projectDir.absolutePath
task = 'classes'
extraParams = [10000, true]
outputFilePath = "$project.buildDir/concurrentClasses.out"
errorFilePath = "$project.buildDir/concurrentClasses.err"
showOutput = true
// gradle task settings
description = 'Runs a concurrent classes task.'
group = GLOBAL$GROUP_RUN
}
task concurrentBootRun(type: SimultaneousTask) {
// SimultaneousTask task settings
projectDir = project.projectDir.absolutePath
task = 'monitorRun'
showOutput = true
showError = true
// gradle task settings
description = 'Runs a concurrent monitorRun task.'
group = GLOBAL$GROUP_RUN
}
task hotRun {
description = 'Runs the project with support for monitoring and reloading classes and static resources.'
group = GLOBAL$GROUP_RUN
dependsOn = ['concurrentBootRun', 'concurrentClasses']
}