Skip to content

Commit a9221b2

Browse files
authored
Merge pull request #50 from topcoder-platform/dev-maven
Dev maven
2 parents 6b4a7cd + cabc54c commit a9221b2

File tree

4 files changed

+54
-34
lines changed

4 files changed

+54
-34
lines changed

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ Add ./target/auto-pilot-1.0-SNAPSHOT-spring-boot.jar /opt/autopilot/
1010
Add ./xml_phase_template.xsd /opt/autopilot/
1111
Add ./target/templates /opt/autopilot/templates/
1212

13-
CMD ["java", "-jar", "auto-pilot-1.0-SNAPSHOT-spring-boot.jar", "-poll", "5"]
13+
CMD ["java", "-jar", "auto-pilot-1.0-SNAPSHOT-spring-boot.jar", "-poll", "1"]

ECSDockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ Add ./target/auto-pilot-1.0-SNAPSHOT-spring-boot.jar /opt/autopilot/
1010
Add ./xml_phase_template.xsd /opt/autopilot/
1111
Add ./target/templates /opt/autopilot/templates/
1212

13-
CMD ["java", "-jar", "auto-pilot-1.0-SNAPSHOT-spring-boot.jar", "-poll", "5"]
13+
CMD ["java", "-jar", "auto-pilot-1.0-SNAPSHOT-spring-boot.jar", "-poll", "1"]

src/main/java/com/topcoder/management/phase/autopilot/AutoPilot.java

+44-28
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
import java.util.HashSet;
1313
import java.util.Map;
1414
import java.util.Set;
15+
import java.util.concurrent.CountDownLatch;
16+
import java.util.concurrent.ExecutorService;
17+
import java.util.concurrent.Executors;
1518

1619
/**
1720
* <p>
@@ -50,6 +53,8 @@ public class AutoPilot {
5053
*/
5154
private static final AutoPilotResult[] ZERO_AUTO_PILOT_RESULT_ARRAY = new AutoPilotResult[0];
5255

56+
private static final ExecutorService THREAD_POOL = Executors.newFixedThreadPool(20);
57+
5358
/**
5459
* <p>
5560
* Represents the AutoPilotSource instance that is used to retrieve a list of project ids to
@@ -158,37 +163,48 @@ public AutoPilotResult[] advanceProjects(long[] projectId, String operator) thro
158163

159164
// Map key is Long (project id). Map value is AutoPilotResult instance.
160165
Map resMap = new HashMap();
161-
for (int i = 0; i < projectId.length; i++) {
162-
AutoPilotResult result = null;
163-
Long longProjectId = new Long(projectId[i]);
164-
165-
// Check if the project is processing by another thread
166-
synchronized (processingProjectIds) {
167-
if (processingProjectIds.contains(longProjectId)) {
168-
log.info(new LogMessage(null, operator, "Stopped in synchronized for projectId=" + longProjectId).toString());
169-
continue;
170-
} else {
171-
processingProjectIds.add(longProjectId);
172-
}
173-
}
174-
175-
try {
176-
result = advanceProject(projectId[i], operator);
177-
// store/aggregate into Map
178-
if (resMap.containsKey(longProjectId)) {
179-
// Aggregate the result only if at least one of counters > 0.
180-
if (result.getPhaseEndedCount() > 0 || result.getPhaseStartedCount() > 0) {
181-
((AutoPilotResult) resMap.get(longProjectId)).aggregate(result);
166+
CountDownLatch latch = new CountDownLatch(projectId.length);
167+
for (long id : projectId) {
168+
THREAD_POOL.execute(() -> {
169+
AutoPilotResult result = null;
170+
Long longProjectId = new Long(id);
171+
172+
// Check if the project is processing by another thread
173+
synchronized (processingProjectIds) {
174+
if (processingProjectIds.contains(longProjectId)) {
175+
log.info(
176+
new LogMessage(null, operator, "Stopped in synchronized for projectId=" + longProjectId)
177+
.toString());
178+
return;
179+
} else {
180+
processingProjectIds.add(longProjectId);
182181
}
183-
} else {
184-
resMap.put(longProjectId, result);
185182
}
186-
} finally {
187-
// Make sure this project can be processed by next thread
188-
synchronized (processingProjectIds) {
189-
processingProjectIds.remove(longProjectId);
183+
184+
try {
185+
result = advanceProject(longProjectId, operator);
186+
// store/aggregate into Map
187+
if (resMap.containsKey(longProjectId)) {
188+
// Aggregate the result only if at least one of counters > 0.
189+
if (result.getPhaseEndedCount() > 0 || result.getPhaseStartedCount() > 0) {
190+
((AutoPilotResult) resMap.get(longProjectId)).aggregate(result);
191+
}
192+
} else {
193+
resMap.put(longProjectId, result);
194+
}
195+
} finally {
196+
// Make sure this project can be processed by next thread
197+
synchronized (processingProjectIds) {
198+
processingProjectIds.remove(longProjectId);
199+
latch.countDown();
200+
}
190201
}
191-
}
202+
});
203+
}
204+
try {
205+
latch.await();
206+
} catch (InterruptedException E) {
207+
192208
}
193209

194210
return (AutoPilotResult[]) resMap.values().toArray(new AutoPilotResult[resMap.size()]);

src/main/java/com/topcoder/management/phase/autopilot/impl/ActiveAutoPilotSource.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ public class ActiveAutoPilotSource implements AutoPilotSource {
6565
* <p>The log used by this class for logging errors and debug information.</p>
6666
*/
6767
private static final Logger log = LoggerFactory.getLogger(ActiveAutoPilotSource.class);
68+
69+
private static final String QUERY_SEARCH_PROJECTS =
70+
"SELECT DISTINCT project.project_id " +
71+
"FROM project " +
72+
"LEFT JOIN project_info ON project.project_id = project_info.project_id " +
73+
"LEFT JOIN project_info_type_lu ON project_info.project_info_type_id = project_info_type_lu.project_info_type_id " +
74+
"WHERE project.project_status_id=1 and project_info_type_lu.name = 'Autopilot Option' and project_info.value = 'On'";
6875

6976
/**
7077
* <p>
@@ -170,11 +177,8 @@ public void setExtPropAutoPilotSwitchValue(String extPropAutoPilotSwitchValue) {
170177
* @throws AutoPilotSourceException if an error occurs retrieving the project ids
171178
*/
172179
public long[] getProjectIds() throws AutoPilotSourceException {
173-
Filter f = buildFilter();
174180
try {
175-
Project[] proj = projectManager.searchProjects(f);
176-
177-
return processProject(proj);
181+
return projectManager.searchProjectsForIds(QUERY_SEARCH_PROJECTS);
178182
} catch (Exception e) {
179183
log.error("Fail to get projects from projectManager.\n" + LogMessage.getExceptionStackTrace(e));
180184
return ZERO_LONG_ARRAY;

0 commit comments

Comments
 (0)