Repository navigation
Expand file tree
/
Copy pathPluginExternalUpdateOnInvoke.java
More file actions
58 lines (51 loc) · 2.52 KB
/
Copy pathPluginExternalUpdateOnInvoke.java
File metadata and controls
58 lines (51 loc) · 2.52 KB
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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package plugin;
import java.time.Duration;
import software.amazon.lambda.durable.DurableConfig;
import software.amazon.lambda.durable.DurableContext;
import software.amazon.lambda.durable.DurableHandler;
import software.amazon.lambda.durable.plugin.DurableExecutionPlugin;
import software.amazon.lambda.durable.plugin.InvocationInfo;
import software.amazon.lambda.durable.plugin.OperationEndInfo;
/**
* 10-9: Plugin observes a wait completed externally between invocations as an updated operation.
*
* <p>A single 2-second wait. The Java SDK surfaces an operation that completed while the execution was suspended via
* {@code onOperationEnd} with {@code isReplay() == true} (per the hook contract: a later invocation that first observes
* a completion which happened during suspension). The plugin captures {@code isFirstInvocation} from the
* invocation-start hook and emits the "updated-on-invoke" record only for wait-type operations observed with
* {@code isReplay() == true}; on the first invocation the wait has only started (never ends), so nothing is emitted.
*/
public class PluginExternalUpdateOnInvoke extends DurableHandler<Object, String> {
@Override
protected DurableConfig createConfiguration() {
return DurableConfig.builder()
.withPlugins(info -> new UpdatedOnInvokePlugin())
.build();
}
@Override
public String handleRequest(Object input, DurableContext context) {
context.wait(null, Duration.ofSeconds(2));
return "Wait completed";
}
private static final class UpdatedOnInvokePlugin implements DurableExecutionPlugin {
private volatile String executionArn;
private volatile boolean firstInvocation;
@Override
public void onInvocationStart(InvocationInfo info) {
this.executionArn = info.durableExecutionArn();
this.firstInvocation = info.isFirstInvocation();
}
@Override
public void onOperationEnd(OperationEndInfo info) {
if (!PluginSupport.isWait(info.type()) || !info.isReplay()) {
return;
}
System.out.println(String.format(
"{\"plugin\": \"CONFPLUGIN\", \"hook\": \"updated-on-invoke\", \"op\": \"%s\", "
+ "\"status\": \"%s\", \"first\": %b%s}",
info.id(), info.status(), firstInvocation, PluginSupport.arnField(executionArn)));
}
}
}