@@ -34,9 +34,9 @@ public enum Lambda {
34
34
35
35
// allow to gracefully shitdown the runtime client loop
36
36
// this supports gracefull shutdown of the Lambda runtime when integarted with Swift ServiceLifeCycle
37
- private static let cancelled : Mutex < Bool > = Mutex ( false )
38
- public static func cancel ( ) {
39
- Lambda . cancelled . withLock {
37
+ private static let gracefulShutdown : Mutex < Bool > = Mutex ( false )
38
+ public static func shutdown ( ) {
39
+ Lambda . gracefulShutdown . withLock {
40
40
$0 = true
41
41
}
42
42
}
@@ -46,11 +46,13 @@ public enum Lambda {
46
46
logger: Logger
47
47
) async throws where Handler: StreamingLambdaHandler {
48
48
var handler = handler
49
-
49
+ var gracefulShutdown : Bool = Lambda . gracefulShutdown . withLock { $0 }
50
50
do {
51
- while !Task. isCancelled {
51
+ while !Task. isCancelled && !gracefulShutdown {
52
+ logger. trace ( " Waiting for next invocation " )
52
53
let ( invocation, writer) = try await runtimeClient. nextInvocation ( )
53
54
55
+ logger. trace ( " Received invocation : \( invocation. metadata. requestID) " )
54
56
do {
55
57
try await handler. handle (
56
58
invocation. event,
@@ -69,11 +71,15 @@ public enum Lambda {
69
71
try await writer. reportError ( error)
70
72
continue
71
73
}
74
+ logger. trace ( " Completed invocation : \( invocation. metadata. requestID) " )
75
+ gracefulShutdown = Lambda . gracefulShutdown. withLock { $0 }
72
76
}
77
+
73
78
} catch is CancellationError {
74
79
// don't allow cancellation error to propagate further
80
+ logger. trace ( " Lambda runLoop() task has been cancelled " )
75
81
}
76
- logger. trace ( " Lambda runLoop() \( cancelled ? " cancelled " : " completed " ) " )
82
+ logger. trace ( " Lambda runLoop() terminated \( gracefulShutdown ? " with gracefull shutdown " : " " ) " )
77
83
}
78
84
79
85
/// The default EventLoop the Lambda is scheduled on.
0 commit comments