@@ -34,9 +34,9 @@ public enum Lambda {
3434
3535 // allow to gracefully shitdown the runtime client loop
3636 // 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 {
4040 $0 = true
4141 }
4242 }
@@ -46,11 +46,13 @@ public enum Lambda {
4646 logger: Logger
4747 ) async throws where Handler: StreamingLambdaHandler {
4848 var handler = handler
49-
49+ var gracefulShutdown : Bool = Lambda . gracefulShutdown . withLock { $0 }
5050 do {
51- while !Task. isCancelled {
51+ while !Task. isCancelled && !gracefulShutdown {
52+ logger. trace ( " Waiting for next invocation " )
5253 let ( invocation, writer) = try await runtimeClient. nextInvocation ( )
5354
55+ logger. trace ( " Received invocation : \( invocation. metadata. requestID) " )
5456 do {
5557 try await handler. handle (
5658 invocation. event,
@@ -69,11 +71,15 @@ public enum Lambda {
6971 try await writer. reportError ( error)
7072 continue
7173 }
74+ logger. trace ( " Completed invocation : \( invocation. metadata. requestID) " )
75+ gracefulShutdown = Lambda . gracefulShutdown. withLock { $0 }
7276 }
77+
7378 } catch is CancellationError {
7479 // don't allow cancellation error to propagate further
80+ logger. trace ( " Lambda runLoop() task has been cancelled " )
7581 }
76- logger. trace ( " Lambda runLoop() \( cancelled ? " cancelled " : " completed " ) " )
82+ logger. trace ( " Lambda runLoop() terminated \( gracefulShutdown ? " with gracefull shutdown " : " " ) " )
7783 }
7884
7985 /// The default EventLoop the Lambda is scheduled on.
0 commit comments