Skip to content

Commit 7340362

Browse files
authored
Merge pull request #2421 from marklogic/bugfix/DHFPROD-2421
DHFPROD-2421: move creation/release of client into the service method…
2 parents 7ff2d05 + e5cba71 commit 7340362

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

web/src/main/java/com/marklogic/hub/web/model/FlowStepModel.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -188,18 +188,20 @@ public void setLatestJob(LatestJob latestJob) {
188188
}
189189

190190
public void setJobs(FlowJobs flowJobs, boolean fromRunFlow) {
191-
this.jobIds = flowJobs.jobIds;
192-
if (latestJob != null && latestJob.id != null && !this.jobIds.contains(latestJob.id)) {
193-
this.jobIds.add(latestJob.id);
194-
flowJobs.jobIds = this.jobIds;
195-
flowJobs.latestJob = latestJob;
196-
return;
197-
}
198-
if (fromRunFlow) {
199-
//reset the latestJob info for until the running flow starts with a new jobId
200-
flowJobs.latestJob = null;
191+
if(flowJobs != null) {
192+
this.jobIds = flowJobs.jobIds;
193+
if (latestJob != null && latestJob.id != null && !this.jobIds.contains(latestJob.id)) {
194+
this.jobIds.add(latestJob.id);
195+
flowJobs.jobIds = this.jobIds;
196+
flowJobs.latestJob = latestJob;
197+
return;
198+
}
199+
if (fromRunFlow) {
200+
//reset the latestJob info for until the running flow starts with a new jobId
201+
flowJobs.latestJob = null;
202+
}
203+
this.latestJob = flowJobs.latestJob;
201204
}
202-
this.latestJob = flowJobs.latestJob;
203205
}
204206

205207
//for testing purpose

web/src/main/java/com/marklogic/hub/web/service/FlowJobService.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,25 @@ public class FlowJobService extends ResourceManager {
3838

3939
private DatabaseClient client;
4040

41-
//use a cache with ttl 30 minutes or longer to reduce frequency for fetching data from DB
41+
//use a cache with ttl 5 minutes to reduce frequency for fetching data from DB
4242
public final Cache<String, FlowJobs> cachedJobsByFlowName = CacheBuilder.newBuilder().expireAfterWrite(
43-
30, TimeUnit.MINUTES).build();
43+
5, TimeUnit.MINUTES).build();
4444

4545
public FlowJobService() {
4646
super();
47+
if(hubConfig != null) {
48+
this.setupClient();
49+
}
4750
}
4851

49-
public void setupClient() {
52+
private void setupClient() {
5053
this.client = hubConfig.newJobDbClient();
5154
}
5255

5356
public FlowJobs getJobs(String flowName) {
57+
if(this.client == null) {
58+
this.setupClient();
59+
}
5460
try {
5561
return cachedJobsByFlowName.get(flowName, () -> {
5662
client.init(ML_JOBS_NAME, this);
@@ -144,11 +150,18 @@ public FlowJobs getJobs(String flowName) {
144150
if (cachedJobsByFlowName.getIfPresent(flowName) == null) {
145151
cachedJobsByFlowName.invalidate(flowName);
146152
}
153+
this.release();
147154
}
148155
return cachedJobsByFlowName.getIfPresent(flowName);
149156
}
150-
151-
public void release() {
152-
this.client.release();
157+
//TODO: fix this whole client mess and properly report exceptions
158+
private void release() {
159+
if(this.client != null) {
160+
try {
161+
this.client.release();
162+
} catch (Exception e) {
163+
this.client = null;
164+
}
165+
}
153166
}
154167
}

web/src/main/java/com/marklogic/hub/web/service/FlowManagerService.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,11 @@ public FlowStepModel getFlow(String flowName, boolean fromRunFlow) {
139139
}
140140

141141
private FlowStepModel getFlowStepModel(Flow flow, boolean fromRunFlow) {
142-
flowJobService.setupClient();
143142
FlowStepModel fsm = FlowStepModel.transformFromFlow(flow);
144143
if (flowRunner.getRunningFlow() != null && flow.getName().equalsIgnoreCase(flowRunner.getRunningFlow().getName())) {
145144
fsm.setLatestJob(FlowRunnerChecker.getInstance(flowRunner).getLatestJob());
146145
}
147146
FlowJobs flowJobs = flowJobService.getJobs(flow.getName());
148-
flowJobService.release();
149147
fsm.setJobs(flowJobs, fromRunFlow);
150148
return fsm;
151149
}

0 commit comments

Comments
 (0)