Skip to content

Commit 9443354

Browse files
committed
Ping status now represents the status of the entire ping sub-process. This means it either is completed or errors occurred. The new values in CodeSystem dsf-ping-status.xml reflect that. This required changing all instances where the status gets set. Generally, the status should now only get set at the end of the process/sub-process instead of setting it multiple times during process execution.
Additionally, introduced AggregateErrorMailService which can aggregate error messages into a list and send them all in one e-mail instead of sending one e-mail per error message like before
1 parent 3ac362c commit 9443354

File tree

19 files changed

+195
-158
lines changed

19 files changed

+195
-158
lines changed

src/main/java/dev/dsf/bpe/ConstantsPing.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,12 @@ private ConstantsPing()
5555
public static final String CODESYSTEM_DSF_PING_VALUE_DOWNLOADED_DURATION_MILLIS = "downloaded-duration-millis";
5656
public static final String CODESYSTEM_DSF_PING_VALUE_DOWNLOADED_BYTES = "downloaded-bytes";
5757
public static final String CODESYSTEM_DSF_PING_VALUE_DOWNLOAD_RESOURCE_REFERENCE = "download-resource-reference";
58+
public static final String CODESYSTEM_DSF_PING_VALUE_ERROR_MESSAGE = "error-message";
5859

5960
public static final String CODESYSTEM_DSF_PING_STATUS = "http://dsf.dev/fhir/CodeSystem/ping-status-v2";
60-
public static final String CODESYSTEM_DSF_PING_STATUS_VALUE_NOT_ALLOWED = "not-allowed";
61-
public static final String CODESYSTEM_DSF_PING_STATUS_VALUE_NOT_REACHABLE = "not-reachable";
62-
public static final String CODESYSTEM_DSF_PING_STATUS_VALUE_PONG_MISSING = "pong-missing";
63-
public static final String CODESYSTEM_DSF_PING_STATUS_VALUE_PONG_RECEIVED = "pong-received";
64-
public static final String CODESYSTEM_DSF_PING_STATUS_VALUE_PONG_SENT = "pong-sent";
65-
public static final String CODESYSTEM_DSF_PING_STATUS_VALUE_ERROR_MESSAGE = "error-message";
66-
public static final String CODESYSTEM_DSF_PING_STATUS_VALUE_RESOURCE_DOWNLOADED = "resource-downloaded";
61+
public static final String CODESYSTEM_DSF_PING_STATUS_VALUE_COMPLETED = "completed";
62+
public static final String CODESYSTEM_DSF_PING_STATUS_VALUE_PENDING = "pending";
63+
public static final String CODESYSTEM_DSF_PING_STATUS_VALUE_ERROR = "error";
6764

6865
public static final String CODESYSTEM_DSF_PING_UNITS = "http://dsf.dev/fhir/CodeSystem/ping-units-v2";
6966
public static final String CODESYSTEM_DSF_PING_UNITS_VALUE_BITS_PER_SECOND = "bits-per-second";
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package dev.dsf.bpe.mail;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.hl7.fhir.r4.model.IdType;
7+
8+
import dev.dsf.bpe.v1.ProcessPluginApi;
9+
import dev.dsf.bpe.v1.variables.Target;
10+
11+
public class AggregateErrorMailService extends ErrorMailService
12+
{
13+
private static final String MAIL_MESSAGE_INTRO = "Error(s) while executing ping-pong process:";
14+
private static final String PING_PROCESS_HAS_ERRORS = "Ping process has errors";
15+
16+
private List<String> errorMessages;
17+
18+
public AggregateErrorMailService(ProcessPluginApi api, boolean sendPingProcessFailedMail,
19+
boolean sendPongProcessFailedMail)
20+
{
21+
super(api, sendPingProcessFailedMail, sendPongProcessFailedMail);
22+
errorMessages = new ArrayList<>();
23+
}
24+
25+
public void addMessagePing(Target target, String message)
26+
{
27+
pingProcessErrorLogger.info("Ping process error: {}", message);
28+
29+
if (sendPingProcessFailedMail)
30+
{
31+
errorMessages.add(createMessage(target, message, null));
32+
}
33+
}
34+
35+
public void addMessagePong(Target target, String message)
36+
{
37+
pongProcessErrorLogger.info("Pong process error: {}", message);
38+
39+
if (sendPongProcessFailedMail)
40+
{
41+
errorMessages.add(createMessage(target, message, null));
42+
}
43+
}
44+
45+
public void send(IdType taskId)
46+
{
47+
if (!errorMessages.isEmpty())
48+
{
49+
api.getMailService().send(PING_PROCESS_HAS_ERRORS, buildMailMessage(taskId));
50+
errorMessages = new ArrayList<>();
51+
}
52+
}
53+
54+
protected String buildMailMessage(IdType taskId)
55+
{
56+
StringBuilder mailMessage = new StringBuilder();
57+
58+
mailMessage.append(MAIL_MESSAGE_INTRO);
59+
mailMessage.append("\n\n");
60+
61+
errorMessages.forEach(errorMessage ->
62+
{
63+
mailMessage.append(errorMessage);
64+
mailMessage.append("\n\n");
65+
});
66+
67+
mailMessage.append("\nProcess started by: ");
68+
mailMessage.append(taskId.toVersionless()
69+
.withServerBase(api.getEndpointProvider().getLocalEndpointAddress(), "Task").getValue());
70+
71+
return mailMessage.toString();
72+
}
73+
74+
protected String createMessage(Target target, String message, String messageDetails)
75+
{
76+
StringBuilder b = new StringBuilder();
77+
78+
b.append(api.getOrganizationProvider().getLocalOrganizationIdentifierValue().orElse("?"));
79+
b.append('/');
80+
b.append(api.getEndpointProvider().getLocalEndpointIdentifierValue().orElse("?"));
81+
82+
b.append(" -> ");
83+
84+
b.append(target.getOrganizationIdentifierValue());
85+
b.append('/');
86+
b.append(target.getEndpointIdentifierValue());
87+
88+
b.append(": ");
89+
b.append(message);
90+
91+
if (messageDetails != null)
92+
{
93+
b.append("\n\t");
94+
b.append(messageDetails);
95+
}
96+
97+
return b.toString();
98+
}
99+
}

src/main/java/dev/dsf/bpe/mail/ErrorMailService.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@
1212

1313
public class ErrorMailService implements InitializingBean
1414
{
15-
private static final Logger pingProcessErrorLogger = LoggerFactory.getLogger("ping-process-error-logger");
16-
private static final Logger pongProcessErrorLogger = LoggerFactory.getLogger("pong-process-error-logger");
15+
protected static final Logger pingProcessErrorLogger = LoggerFactory.getLogger("ping-process-error-logger");
16+
protected static final Logger pongProcessErrorLogger = LoggerFactory.getLogger("pong-process-error-logger");
1717

18-
private static final String SUBJECT_PING_PROCESS_FAILED = "Ping Process Failed";
19-
private static final String SUBJECT_PONG_PROCESS_FAILED = "Pong Process Failed";
18+
protected static final String SUBJECT_PING_PROCESS_FAILED = "Ping Process Failed";
19+
protected static final String SUBJECT_PONG_PROCESS_FAILED = "Pong Process Failed";
2020

21-
private final ProcessPluginApi api;
21+
protected final ProcessPluginApi api;
2222

23-
private final boolean sendPingProcessFailedMail;
24-
private final boolean sendPongProcessFailedMail;
23+
protected final boolean sendPingProcessFailedMail;
24+
protected final boolean sendPongProcessFailedMail;
2525

2626
public ErrorMailService(ProcessPluginApi api, boolean sendPingProcessFailedMail, boolean sendPongProcessFailedMail)
2727
{

src/main/java/dev/dsf/bpe/message/SendPing.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ protected void handleSendTaskError(DelegateExecution execution, Variables variab
5757
{
5858
Target target = variables.getTarget();
5959

60-
String statusCode = exception instanceof WebApplicationException w && w.getResponse() != null
61-
&& w.getResponse().getStatus() == Response.Status.FORBIDDEN.getStatusCode()
62-
? ConstantsPing.CODESYSTEM_DSF_PING_STATUS_VALUE_NOT_ALLOWED
63-
: ConstantsPing.CODESYSTEM_DSF_PING_STATUS_VALUE_NOT_REACHABLE;
60+
String responseCode = exception instanceof WebApplicationException w && w.getResponse() != null
61+
? Response.Status.fromStatusCode(w.getResponse().getStatus()).toString()
62+
: "unknown";
63+
String statusCode = ConstantsPing.CODESYSTEM_DSF_PING_STATUS_VALUE_ERROR;
6464

6565
execution.setVariableLocal(ConstantsPing.getBpmnExecutionVariableStatusCode(), statusCode);
6666
String specialErrorMessage = createErrorMessage(exception);
6767
execution.setVariableLocal(ConstantsPing.getBpmnExecutionVariableErrorMessage(), specialErrorMessage);
68-
logger.info("Request to {} resulted in status {}", target.getEndpointUrl(), statusCode);
68+
logger.info("Request to {} resulted in status {}", target.getEndpointUrl(), responseCode);
6969
}
7070

7171
@Override

src/main/java/dev/dsf/bpe/message/SendPong.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ protected void doExecute(DelegateExecution execution, Variables variables) throw
8585
Target target = variables.getTarget();
8686
Task mainTask = variables.getStartTask();
8787
PingStatusGenerator.updatePongStatusOutput(mainTask, target);
88-
PingStatusGenerator.updatePongStatusOutput(mainTask, ConstantsPing.CODESYSTEM_DSF_PING_STATUS_VALUE_PONG_SENT);
8988
variables.updateTask(mainTask);
9089
super.doExecute(execution, variables);
9190
}
@@ -99,9 +98,8 @@ protected void handleSendTaskError(DelegateExecution execution, Variables variab
9998
Task startTask = variables.getStartTask();
10099

101100
String statusCode = exception instanceof WebApplicationException w && w.getResponse() != null
102-
&& w.getResponse().getStatus() == Response.Status.FORBIDDEN.getStatusCode()
103-
? ConstantsPing.CODESYSTEM_DSF_PING_STATUS_VALUE_NOT_ALLOWED
104-
: ConstantsPing.CODESYSTEM_DSF_PING_STATUS_VALUE_NOT_REACHABLE;
101+
? Response.Status.fromStatusCode(w.getResponse().getStatus()).toString()
102+
: "unknown";
105103
execution.setVariable(ConstantsPing.getBpmnExecutionVariableStatusCode(), statusCode);
106104

107105
String specialErrorMessage = createErrorMessage(exception);

src/main/java/dev/dsf/bpe/service/ping/LogAndSaveNoResponse.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ protected void doExecute(DelegateExecution delegateExecution, Variables variable
2929
String correlationKey = target.getCorrelationKey();
3030
delegateExecution.removeVariable("statusCode");
3131
variables.setString(ConstantsPing.getBpmnExecutionVariableStatusCode(correlationKey),
32-
ConstantsPing.CODESYSTEM_DSF_PING_STATUS_VALUE_PONG_MISSING);
32+
ConstantsPing.CODESYSTEM_DSF_PING_STATUS_VALUE_ERROR);
3333

3434
logger.debug("Saved '{}' to process execution for correlation key '{}'",
35-
ConstantsPing.CODESYSTEM_DSF_PING_STATUS_VALUE_PONG_MISSING, correlationKey);
35+
ConstantsPing.CODESYSTEM_DSF_PING_STATUS_VALUE_ERROR, correlationKey);
3636
}
3737
}

src/main/java/dev/dsf/bpe/service/ping/LogAndSaveSendError.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,11 @@ protected void doExecute(DelegateExecution execution, Variables variables) throw
2323
PingPongLogger logger = new PingPongLogger(LogAndSaveSendError.class, variables.getStartTask());
2424

2525
String correlationKey = variables.getTarget().getCorrelationKey();
26-
String statusCode = (String) execution.getVariableLocal(ConstantsPing.getBpmnExecutionVariableStatusCode());
2726
String errorMessage = (String) execution.getVariableLocal(ConstantsPing.getBpmnExecutionVariableErrorMessage());
2827

29-
variables.setString(ConstantsPing.getBpmnExecutionVariableStatusCode(correlationKey), statusCode);
3028
ErrorMessageListUtils.add(errorMessage, execution, correlationKey);
3129
variables.setInteger(ConstantsPing.getBpmnExecutionVariableUploadedBytes(correlationKey), 0);
3230
variables.setLong(ConstantsPing.getBpmnExecutionVariableDownloadedDurationMillis(correlationKey), 0L);
33-
logger.debug("Saved error when trying to send ping message. Status: {}, error message: {}", statusCode,
34-
errorMessage);
31+
logger.debug("Saved error when trying to send ping message. Error message: {}", errorMessage);
3532
}
3633
}

src/main/java/dev/dsf/bpe/service/ping/SavePong.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ protected void doExecute(DelegateExecution delegateExecution, Variables variable
3535
logger.debug("Pong received from {}. Saving pong information...", target.getEndpointUrl());
3636
String correlationKey = target.getCorrelationKey();
3737
delegateExecution.removeVariable("statusCode");
38-
variables.setString(ConstantsPing.getBpmnExecutionVariableStatusCode(correlationKey),
39-
ConstantsPing.CODESYSTEM_DSF_PING_STATUS_VALUE_PONG_RECEIVED);
4038

4139
Task pong = variables.getLatestTask();
4240

@@ -56,7 +54,7 @@ protected void doExecute(DelegateExecution delegateExecution, Variables variable
5654

5755
List<String> errorList = api.getTaskHelper()
5856
.getInputParameterValues(pong, ConstantsPing.CODESYSTEM_DSF_PING,
59-
ConstantsPing.CODESYSTEM_DSF_PING_STATUS_VALUE_ERROR_MESSAGE, StringType.class)
57+
ConstantsPing.CODESYSTEM_DSF_PING_VALUE_ERROR_MESSAGE, StringType.class)
6058
.map(PrimitiveType::getValue).map(string -> "Pong error: " + string).toList();
6159

6260
ErrorMessageListUtils.addAll(errorList, delegateExecution, correlationKey);

src/main/java/dev/dsf/bpe/service/ping/StoreResults.java

Lines changed: 35 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import org.springframework.beans.factory.InitializingBean;
1212

1313
import dev.dsf.bpe.ConstantsPing;
14-
import dev.dsf.bpe.mail.ErrorMailService;
14+
import dev.dsf.bpe.mail.AggregateErrorMailService;
1515
import dev.dsf.bpe.util.ErrorMessageListUtils;
1616
import dev.dsf.bpe.util.logging.PingPongLogger;
1717
import dev.dsf.bpe.util.task.NetworkSpeedCalculator;
@@ -25,10 +25,10 @@
2525

2626
public class StoreResults extends AbstractServiceDelegate implements InitializingBean
2727
{
28-
private final ErrorMailService errorMailService;
28+
private final AggregateErrorMailService errorMailService;
2929
private final String networkSpeedUnit;
3030

31-
public StoreResults(ProcessPluginApi api, ErrorMailService errorMailService, String networkSpeedUnit)
31+
public StoreResults(ProcessPluginApi api, AggregateErrorMailService errorMailService, String networkSpeedUnit)
3232
{
3333
super(api);
3434
this.networkSpeedUnit = networkSpeedUnit;
@@ -59,66 +59,48 @@ protected void doExecute(DelegateExecution execution, Variables variables) throw
5959
{
6060
String correlationKey = target.getCorrelationKey();
6161

62-
String statusCode = variables.getString(ConstantsPing.getBpmnExecutionVariableStatusCode(correlationKey));
63-
if (ConstantsPing.CODESYSTEM_DSF_PING_STATUS_VALUE_NOT_REACHABLE.equals(statusCode)
64-
|| ConstantsPing.CODESYSTEM_DSF_PING_STATUS_VALUE_NOT_ALLOWED.equals(statusCode))
62+
List<String> errors = ErrorMessageListUtils.getErrorMessageList(execution, correlationKey);
63+
String statusCode = errors.isEmpty() ? ConstantsPing.CODESYSTEM_DSF_PING_STATUS_VALUE_COMPLETED
64+
: ConstantsPing.CODESYSTEM_DSF_PING_STATUS_VALUE_ERROR;
65+
int downloadResourceSizeBytes = variables
66+
.getInteger(ConstantsPing.BPMN_EXECUTION_VARIABLE_DOWNLOAD_RESOURCE_SIZE_BYTES);
67+
List<String> errorMessageList = ErrorMessageListUtils.getErrorMessageList(execution, correlationKey);
68+
if (downloadResourceSizeBytes >= 0) // if fat-ping
6569
{
66-
List<String> errorMessages = ErrorMessageListUtils.getErrorMessageList(execution, correlationKey);
67-
String errorMessage = errorMessages.get(0);
68-
task.addOutput(PingStatusGenerator.createPingStatusOutput(target, statusCode, errorMessages));
69-
70-
if (ConstantsPing.CODESYSTEM_DSF_PING_STATUS_VALUE_NOT_REACHABLE.equals(statusCode))
71-
errorMailService.endpointNotReachableForPing(task.getIdElement(), target, errorMessage);
72-
else if (ConstantsPing.CODESYSTEM_DSF_PING_STATUS_VALUE_NOT_ALLOWED.equals(statusCode))
73-
errorMailService.endpointReachablePingForbidden(task.getIdElement(), target, errorMessage);
70+
Integer downloadedBytes = variables
71+
.getInteger(ConstantsPing.getBpmnExecutionVariableDownloadedBytes(correlationKey));
72+
Long downloadedDurationMillis = variables
73+
.getLong(ConstantsPing.getBpmnExecutionVariableDownloadedDurationMillis(correlationKey));
74+
75+
BigDecimal downloadSpeed = downloadedBytes != null && downloadedDurationMillis != null
76+
? NetworkSpeedCalculator.calculate(downloadedBytes, downloadedDurationMillis, networkSpeedUnit)
77+
: null;
78+
79+
Integer uploadedBytes = variables
80+
.getInteger(ConstantsPing.getBpmnExecutionVariableUploadedBytes(correlationKey));
81+
Long uploadedDurationMillis = variables
82+
.getLong(ConstantsPing.getBpmnExecutionVariableUploadedDurationMillis(correlationKey));
83+
84+
BigDecimal uploadSpeed = uploadedBytes != null && uploadedDurationMillis != null
85+
? NetworkSpeedCalculator.calculate(uploadedBytes, uploadedDurationMillis, networkSpeedUnit)
86+
: null;
87+
88+
task.addOutput(PingStatusGenerator.createPingStatusOutput(target, statusCode, errorMessageList,
89+
downloadSpeed, uploadSpeed, networkSpeedUnit));
7490
}
75-
else if (ConstantsPing.CODESYSTEM_DSF_PING_STATUS_VALUE_PONG_MISSING.equals(statusCode))
91+
else // if slim-ping
7692
{
77-
List<String> errorMessages = ErrorMessageListUtils.getErrorMessageList(execution, correlationKey);
78-
task.addOutput(PingStatusGenerator.createPingStatusOutput(target, statusCode, errorMessages));
79-
80-
errorMailService.pongMessageNotReceived(task.getIdElement(), target);
81-
}
82-
else
83-
{
84-
int downloadResourceSizeBytes = variables
85-
.getInteger(ConstantsPing.BPMN_EXECUTION_VARIABLE_DOWNLOAD_RESOURCE_SIZE_BYTES);
86-
List<String> errorMessageList = ErrorMessageListUtils.getErrorMessageList(execution, correlationKey);
87-
if (downloadResourceSizeBytes >= 0) // if fat-ping
88-
{
89-
Integer downloadedBytes = variables
90-
.getInteger(ConstantsPing.getBpmnExecutionVariableDownloadedBytes(correlationKey));
91-
Long downloadedDurationMillis = variables
92-
.getLong(ConstantsPing.getBpmnExecutionVariableDownloadedDurationMillis(correlationKey));
93-
94-
BigDecimal downloadSpeed = downloadedBytes != null && downloadedDurationMillis != null
95-
? NetworkSpeedCalculator.calculate(downloadedBytes, downloadedDurationMillis,
96-
networkSpeedUnit)
97-
: null;
98-
99-
Integer uploadedBytes = variables
100-
.getInteger(ConstantsPing.getBpmnExecutionVariableUploadedBytes(correlationKey));
101-
Long uploadedDurationMillis = variables
102-
.getLong(ConstantsPing.getBpmnExecutionVariableUploadedDurationMillis(correlationKey));
103-
104-
BigDecimal uploadSpeed = uploadedBytes != null && uploadedDurationMillis != null
105-
? NetworkSpeedCalculator.calculate(uploadedBytes, uploadedDurationMillis, networkSpeedUnit)
106-
: null;
107-
108-
task.addOutput(PingStatusGenerator.createPingStatusOutput(target, statusCode, errorMessageList,
109-
downloadSpeed, uploadSpeed, networkSpeedUnit));
110-
}
111-
else // if slim-ping
112-
{
113-
task.addOutput(PingStatusGenerator.createPingStatusOutput(target, statusCode));
114-
}
93+
task.addOutput(PingStatusGenerator.createPingStatusOutput(target, statusCode, errorMessageList));
11594
}
95+
errors.forEach(error -> errorMailService.addMessagePing(target, error));
11696
});
11797

11898
// TODO only send one combined status mail
11999

120100
variables.updateTask(task);
121101

102+
errorMailService.send(task.getIdElement());
103+
122104
logger.debug("Successfully stored results for task {}", variables.getStartTask().getIdElement().getValue());
123105
}
124106
}

src/main/java/dev/dsf/bpe/service/pong/StoreDownloadSpeed.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ protected void doExecute(DelegateExecution execution, Variables variables) throw
3838
BigDecimal downloadSpeed = NetworkSpeedCalculator.calculate(downloadedBytes, downloadedDurationMillis,
3939
networkSpeedUnit);
4040

41-
PingStatusGenerator.updatePongStatusOutput(startTask,
42-
ConstantsPing.CODESYSTEM_DSF_PING_STATUS_VALUE_RESOURCE_DOWNLOADED);
41+
PingStatusGenerator.updatePongStatusOutput(startTask, ConstantsPing.CODESYSTEM_DSF_PING_STATUS_VALUE_PENDING);
4342
PingStatusGenerator.updatePongStatusOutputDownloadSpeed(startTask, downloadSpeed, networkSpeedUnit);
4443

4544
variables.updateTask(startTask);

0 commit comments

Comments
 (0)