Skip to content

Commit

Permalink
fix: move notification of requested action before action execution
Browse files Browse the repository at this point in the history
  • Loading branch information
cake-lier committed Jan 17, 2024
1 parent 4165fd8 commit fb85c85
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 71 deletions.
109 changes: 54 additions & 55 deletions src/test/java/org/hyperagents/yggdrasil/BodyNotificationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.hc.core5.http.HttpStatus;
Expand Down Expand Up @@ -302,60 +301,60 @@ public void testRun(final VertxTestContext ctx) throws URISyntaxException, IOExc
);
Assertions.assertNull(r.bodyAsString(), RESPONSE_BODY_EMPTY_MESSAGE);
})
.compose(r -> this.callbackMessages
.get(2)
.future()
.compose(r1 -> this.callbackMessages
.get(3)
.future()
.onSuccess(r2 -> {
Assertions.assertEquals(
this.getUrl(
WORKSPACES_PATH
+ MAIN_WORKSPACE_NAME
+ BODIES_PATH
+ TEST_AGENT_NAME
),
r1.getKey(),
URIS_EQUAL_MESSAGE
);
Assertions.assertEquals(
this.getUrl(
WORKSPACES_PATH
+ MAIN_WORKSPACE_NAME
+ BODIES_PATH
+ TEST_AGENT_NAME
),
r2.getKey(),
URIS_EQUAL_MESSAGE
);
Assertions.assertEquals(
Set.of(
JsonObject
.of(
"artifactName",
COUNTER_ARTIFACT_NAME,
"actionName",
"inc",
"eventType",
"actionRequested"
)
.encode(),
JsonObject
.of(
"artifactName",
COUNTER_ARTIFACT_NAME,
"actionName",
"inc",
"eventType",
"actionSucceeded"
)
.encode()
),
Set.of(r1.getValue(), r2.getValue()),
REPRESENTATIONS_EQUAL_MESSAGE
);
})))
.compose(r -> this.callbackMessages.get(2).future())
.onSuccess(m -> {
Assertions.assertEquals(
this.getUrl(
WORKSPACES_PATH
+ MAIN_WORKSPACE_NAME
+ BODIES_PATH
+ TEST_AGENT_NAME
),
m.getKey(),
URIS_EQUAL_MESSAGE
);
Assertions.assertEquals(
JsonObject
.of(
"artifactName",
COUNTER_ARTIFACT_NAME,
"actionName",
"inc",
"eventType",
"actionRequested"
)
.encode(),
m.getValue(),
REPRESENTATIONS_EQUAL_MESSAGE
);
})
.compose(r -> this.callbackMessages.get(3).future())
.onSuccess(m -> {
Assertions.assertEquals(
this.getUrl(
WORKSPACES_PATH
+ MAIN_WORKSPACE_NAME
+ BODIES_PATH
+ TEST_AGENT_NAME
),
m.getKey(),
URIS_EQUAL_MESSAGE
);
Assertions.assertEquals(
JsonObject
.of(
"artifactName",
COUNTER_ARTIFACT_NAME,
"actionName",
"inc",
"eventType",
"actionSucceeded"
)
.encode(),
m.getValue(),
REPRESENTATIONS_EQUAL_MESSAGE
);
})
.onComplete(ctx.succeedingThenComplete());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,14 +294,20 @@ private Future<Optional<String>> doAction(
final var promise = Promise.<Void>promise();
final var workspace = this.workspaceRegistry.getWorkspace(workspaceName).orElseThrow();
final var agentName = this.getAgentNameFromAgentUri(agentUri);
this.dispatcherMessagebox.sendMessage(
new HttpNotificationDispatcherMessage.ActionRequested(
this.httpConfig.getAgentBodyUri(workspaceName, agentName),
this.getActionNotificationContent(artifactName, action).encode()
)
);
workspace.execOp(0L,
this.getAgentId(this.getAgentCredential(agentUri), workspace.getId()),
e -> {
if (e instanceof ActionSucceededEvent) {
this.dispatcherMessagebox.sendMessage(
new HttpNotificationDispatcherMessage.ActionSucceeded(
this.httpConfig.getAgentBodyUri(workspaceName, agentName),
this.getActionNotificationContent(artifactName, action)
this.getActionNotificationContent(artifactName, action).encode()
)
);
promise.complete();
Expand All @@ -310,6 +316,8 @@ private Future<Optional<String>> doAction(
new HttpNotificationDispatcherMessage.ActionFailed(
this.httpConfig.getAgentBodyUri(workspaceName, agentName),
this.getActionNotificationContent(artifactName, action)
.put("cause", f.getFailureMsg())
.encode()
)
);
promise.fail(f.getFailureMsg());
Expand All @@ -319,12 +327,6 @@ private Future<Optional<String>> doAction(
operation,
-1,
null);
this.dispatcherMessagebox.sendMessage(
new HttpNotificationDispatcherMessage.ActionRequested(
this.httpConfig.getAgentBodyUri(workspaceName, agentName),
this.getActionNotificationContent(artifactName, action)
)
);
return promise.future()
.map(ignored ->
Optional.ofNullable(feedbackParameter.get())
Expand All @@ -338,15 +340,13 @@ private Future<Optional<String>> doAction(
);
}

private String getActionNotificationContent(final String artifactName, final String action) {
return JsonObject
.of(
"artifactName",
artifactName,
"actionName",
action
)
.encode();
private JsonObject getActionNotificationContent(final String artifactName, final String action) {
return JsonObject.of(
"artifactName",
artifactName,
"actionName",
action
);
}

private String getAgentNameFromAgentUri(final String agentUri) {
Expand Down

0 comments on commit fb85c85

Please sign in to comment.