Skip to content

Commit dbf0365

Browse files
author
nervenes
committed
make logger property an optional as well
1 parent f188481 commit dbf0365

File tree

2 files changed

+38
-58
lines changed

2 files changed

+38
-58
lines changed

Sources/ServiceLifecycle/ServiceGroup.swift

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public actor ServiceGroup: Sendable, Service {
3030
}
3131

3232
/// The logger.
33-
private let logger: Logger
33+
private let logger: Logger?
3434
/// The logging configuration.
3535
private let loggingConfiguration: ServiceGroupConfiguration.LoggingConfiguration
3636
/// The maximum amount of time that graceful shutdown is allowed to take.
@@ -55,7 +55,7 @@ public actor ServiceGroup: Sendable, Service {
5555
Set(configuration.gracefulShutdownSignals).isDisjoint(with: configuration.cancellationSignals),
5656
"Overlapping graceful shutdown and cancellation signals"
5757
)
58-
precondition(configuration.logger.label != deprecatedLoggerLabel, "Please migrate to the new initializers")
58+
precondition(configuration.logger?.label != deprecatedLoggerLabel, "Please migrate to the new initializers")
5959
self.state = .initial(services: configuration.services)
6060
self.gracefulShutdownSignals = configuration.gracefulShutdownSignals
6161
self.cancellationSignals = configuration.cancellationSignals
@@ -78,15 +78,11 @@ public actor ServiceGroup: Sendable, Service {
7878
cancellationSignals: [UnixSignal] = [],
7979
logger: Logger? = nil
8080
) {
81-
if logger == nil {
82-
LoggingSystem.bootstrap { SwiftLogNoOpLogHandler($0) }
83-
}
84-
8581
let configuration = ServiceGroupConfiguration(
8682
services: services.map { ServiceGroupConfiguration.ServiceConfiguration(service: $0) },
8783
gracefulShutdownSignals: gracefulShutdownSignals,
8884
cancellationSignals: cancellationSignals,
89-
logger: logger ?? .init(label: "")
85+
logger: logger
9086
)
9187

9288
self.init(configuration: configuration)
@@ -208,7 +204,7 @@ public actor ServiceGroup: Sendable, Service {
208204
services: inout [ServiceGroupConfiguration.ServiceConfiguration],
209205
gracefulShutdownStream: AsyncStream<Void>
210206
) async throws {
211-
self.logger.debug(
207+
self.logger?.debug(
212208
"Starting service lifecycle",
213209
metadata: [
214210
self.loggingConfiguration.keys.gracefulShutdownSignalsKey: "\(self.gracefulShutdownSignals)",
@@ -274,7 +270,7 @@ public actor ServiceGroup: Sendable, Service {
274270
gracefulShutdownManagers.reserveCapacity(services.count)
275271

276272
for (index, serviceConfiguration) in services.enumerated() {
277-
self.logger.debug(
273+
self.logger?.debug(
278274
"Starting service",
279275
metadata: [
280276
self.loggingConfiguration.keys.serviceKey: "\(serviceConfiguration.service)"
@@ -328,7 +324,7 @@ public actor ServiceGroup: Sendable, Service {
328324

329325
switch service.successTerminationBehavior.behavior {
330326
case .cancelGroup:
331-
self.logger.debug(
327+
self.logger?.debug(
332328
"Service finished unexpectedly. Cancelling group.",
333329
metadata: [
334330
self.loggingConfiguration.keys.serviceKey: "\(service.service)"
@@ -341,7 +337,7 @@ public actor ServiceGroup: Sendable, Service {
341337
return .failure(ServiceGroupError.serviceFinishedUnexpectedly())
342338

343339
case .gracefullyShutdownGroup:
344-
self.logger.debug(
340+
self.logger?.debug(
345341
"Service finished. Gracefully shutting down group.",
346342
metadata: [
347343
self.loggingConfiguration.keys.serviceKey: "\(service.service)"
@@ -361,7 +357,7 @@ public actor ServiceGroup: Sendable, Service {
361357
}
362358

363359
case .ignore:
364-
self.logger.debug(
360+
self.logger?.debug(
365361
"Service finished.",
366362
metadata: [
367363
self.loggingConfiguration.keys.serviceKey: "\(service.service)"
@@ -370,7 +366,7 @@ public actor ServiceGroup: Sendable, Service {
370366
services[index] = nil
371367

372368
if services.allSatisfy({ $0 == nil }) {
373-
self.logger.debug(
369+
self.logger?.debug(
374370
"All services finished."
375371
)
376372
self.cancelGroupAndSpawnTimeoutIfNeeded(
@@ -384,7 +380,7 @@ public actor ServiceGroup: Sendable, Service {
384380
case .serviceThrew(let service, let index, let serviceError):
385381
switch service.failureTerminationBehavior.behavior {
386382
case .cancelGroup:
387-
self.logger.debug(
383+
self.logger?.debug(
388384
"Service threw error. Cancelling group.",
389385
metadata: [
390386
self.loggingConfiguration.keys.serviceKey: "\(service.service)",
@@ -398,7 +394,7 @@ public actor ServiceGroup: Sendable, Service {
398394
return .failure(serviceError)
399395

400396
case .gracefullyShutdownGroup:
401-
self.logger.debug(
397+
self.logger?.debug(
402398
"Service threw error. Shutting down group.",
403399
metadata: [
404400
self.loggingConfiguration.keys.serviceKey: "\(service.service)",
@@ -420,7 +416,7 @@ public actor ServiceGroup: Sendable, Service {
420416
}
421417

422418
case .ignore:
423-
self.logger.debug(
419+
self.logger?.debug(
424420
"Service threw error.",
425421
metadata: [
426422
self.loggingConfiguration.keys.serviceKey: "\(service.service)",
@@ -430,7 +426,7 @@ public actor ServiceGroup: Sendable, Service {
430426
services[index] = nil
431427

432428
if services.allSatisfy({ $0 == nil }) {
433-
self.logger.debug(
429+
self.logger?.debug(
434430
"All services finished."
435431
)
436432

@@ -445,7 +441,7 @@ public actor ServiceGroup: Sendable, Service {
445441
case .signalCaught(let unixSignal):
446442
if self.gracefulShutdownSignals.contains(unixSignal) {
447443
// Let's initiate graceful shutdown.
448-
self.logger.debug(
444+
self.logger?.debug(
449445
"Signal caught. Shutting down the group.",
450446
metadata: [
451447
self.loggingConfiguration.keys.signalKey: "\(unixSignal)"
@@ -464,7 +460,7 @@ public actor ServiceGroup: Sendable, Service {
464460
}
465461
} else {
466462
// Let's cancel the group.
467-
self.logger.debug(
463+
self.logger?.debug(
468464
"Signal caught. Cancelling the group.",
469465
metadata: [
470466
self.loggingConfiguration.keys.signalKey: "\(unixSignal)"
@@ -479,7 +475,7 @@ public actor ServiceGroup: Sendable, Service {
479475

480476
case .gracefulShutdownCaught:
481477
// We got a manual or inherited graceful shutdown. Let's initiate graceful shutdown.
482-
self.logger.debug("Graceful shutdown caught. Cascading shutdown to services")
478+
self.logger?.debug("Graceful shutdown caught. Cascading shutdown to services")
483479

484480
do {
485481
try await self.shutdownGracefully(
@@ -496,7 +492,7 @@ public actor ServiceGroup: Sendable, Service {
496492
case .cancellationCaught:
497493
// We caught cancellation in our child task so we have to spawn
498494
// our cancellation timeout task if needed
499-
self.logger.debug("Caught cancellation.")
495+
self.logger?.debug("Caught cancellation.")
500496
self.cancelGroupAndSpawnTimeoutIfNeeded(
501497
group: &group,
502498
cancellationTimeoutTask: &cancellationTimeoutTask
@@ -521,7 +517,7 @@ public actor ServiceGroup: Sendable, Service {
521517
return .success(())
522518
}
523519

524-
self.logger.debug(
520+
self.logger?.debug(
525521
"Service lifecycle ended"
526522
)
527523
cancellationTimeoutTask?.cancel()
@@ -562,12 +558,12 @@ public actor ServiceGroup: Sendable, Service {
562558
.enumerated().reversed()
563559
{
564560
guard let service = services[gracefulShutdownIndex] else {
565-
self.logger.debug(
561+
self.logger?.debug(
566562
"Service already finished. Skipping shutdown"
567563
)
568564
continue gracefulShutdownLoop
569565
}
570-
self.logger.debug(
566+
self.logger?.debug(
571567
"Triggering graceful shutdown for service",
572568
metadata: [
573569
self.loggingConfiguration.keys.serviceKey: "\(service.service)"
@@ -587,7 +583,7 @@ public actor ServiceGroup: Sendable, Service {
587583

588584
guard index == gracefulShutdownIndex else {
589585
// Another service exited unexpectedly
590-
self.logger.debug(
586+
self.logger?.debug(
591587
"Service finished unexpectedly during graceful shutdown. Cancelling all other services now",
592588
metadata: [
593589
self.loggingConfiguration.keys.serviceKey: "\(service.service)"
@@ -602,7 +598,7 @@ public actor ServiceGroup: Sendable, Service {
602598
}
603599
// The service that we signalled graceful shutdown did exit/
604600
// We can continue to the next one.
605-
self.logger.debug(
601+
self.logger?.debug(
606602
"Service finished",
607603
metadata: [
608604
self.loggingConfiguration.keys.serviceKey: "\(service.service)"
@@ -614,7 +610,7 @@ public actor ServiceGroup: Sendable, Service {
614610
services[index] = nil
615611
switch service.failureTerminationBehavior.behavior {
616612
case .cancelGroup:
617-
self.logger.debug(
613+
self.logger?.debug(
618614
"Service threw error during graceful shutdown. Cancelling group.",
619615
metadata: [
620616
self.loggingConfiguration.keys.serviceKey: "\(service.service)",
@@ -632,7 +628,7 @@ public actor ServiceGroup: Sendable, Service {
632628
guard index == gracefulShutdownIndex else {
633629
// Another service threw while we were waiting for a shutdown
634630
// We have to continue the iterating the task group's result
635-
self.logger.debug(
631+
self.logger?.debug(
636632
"Another service than the service that we were shutting down threw. Continuing with the next one.",
637633
metadata: [
638634
self.loggingConfiguration.keys.serviceKey: "\(service.service)",
@@ -643,7 +639,7 @@ public actor ServiceGroup: Sendable, Service {
643639
}
644640
// The service that we were shutting down right now threw. Since it's failure
645641
// behaviour is to shutdown the group we can continue
646-
self.logger.debug(
642+
self.logger?.debug(
647643
"The service that we were shutting down threw. Continuing with the next one.",
648644
metadata: [
649645
self.loggingConfiguration.keys.serviceKey: "\(service.service)",
@@ -656,7 +652,7 @@ public actor ServiceGroup: Sendable, Service {
656652
guard index == gracefulShutdownIndex else {
657653
// Another service threw while we were waiting for a shutdown
658654
// We have to continue the iterating the task group's result
659-
self.logger.debug(
655+
self.logger?.debug(
660656
"Another service than the service that we were shutting down threw. Continuing with the next one.",
661657
metadata: [
662658
self.loggingConfiguration.keys.serviceKey: "\(service.service)",
@@ -667,7 +663,7 @@ public actor ServiceGroup: Sendable, Service {
667663
}
668664
// The service that we were shutting down right now threw. Since it's failure
669665
// behaviour is to shutdown the group we can continue
670-
self.logger.debug(
666+
self.logger?.debug(
671667
"The service that we were shutting down threw. Continuing with the next one.",
672668
metadata: [
673669
self.loggingConfiguration.keys.serviceKey: "\(service.service)",
@@ -680,7 +676,7 @@ public actor ServiceGroup: Sendable, Service {
680676
case .signalCaught(let signal):
681677
if self.cancellationSignals.contains(signal) {
682678
// We got signalled cancellation after graceful shutdown
683-
self.logger.debug(
679+
self.logger?.debug(
684680
"Signal caught. Cancelling the group.",
685681
metadata: [
686682
self.loggingConfiguration.keys.signalKey: "\(signal)"
@@ -696,7 +692,7 @@ public actor ServiceGroup: Sendable, Service {
696692
case .gracefulShutdownTimedOut:
697693
// Gracefully shutting down took longer than the user configured
698694
// so we have to escalate it now.
699-
self.logger.debug(
695+
self.logger?.debug(
700696
"Graceful shutdown took longer than allowed by the configuration. Cancelling the group now.",
701697
metadata: [
702698
self.loggingConfiguration.keys.serviceKey: "\(service.service)"
@@ -710,7 +706,7 @@ public actor ServiceGroup: Sendable, Service {
710706
case .cancellationCaught:
711707
// We caught cancellation in our child task so we have to spawn
712708
// our cancellation timeout task if needed
713-
self.logger.debug("Caught cancellation.")
709+
self.logger?.debug("Caught cancellation.")
714710
self.cancelGroupAndSpawnTimeoutIfNeeded(
715711
group: &group,
716712
cancellationTimeoutTask: &cancellationTimeoutTask
@@ -745,7 +741,7 @@ public actor ServiceGroup: Sendable, Service {
745741
) {
746742
guard cancellationTimeoutTask == nil else {
747743
// We already have a cancellation timeout task running.
748-
self.logger.debug(
744+
self.logger?.debug(
749745
"Task cancellation timeout task already running."
750746
)
751747
return
@@ -760,7 +756,7 @@ public actor ServiceGroup: Sendable, Service {
760756
// from being cancelled.
761757
cancellationTimeoutTask = Task {
762758
do {
763-
self.logger.debug(
759+
self.logger?.debug(
764760
"Task cancellation timeout task started."
765761
)
766762
try await Task.sleep(
@@ -769,7 +765,7 @@ public actor ServiceGroup: Sendable, Service {
769765
attosecondsComponent: maximumCancellationDuration.attosecondsComponent
770766
)
771767
)
772-
self.logger.debug(
768+
self.logger?.debug(
773769
"Cancellation took longer than allowed by the configuration."
774770
)
775771
fatalError("Cancellation took longer than allowed by the configuration.")

Sources/ServiceLifecycle/ServiceGroupConfiguration.swift

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public struct ServiceGroupConfiguration: Sendable {
107107
public var cancellationSignals = [UnixSignal]()
108108

109109
/// The group's logger.
110-
public var logger: Logger
110+
public var logger: Logger?
111111

112112
/// The group's logging configuration.
113113
public var logging = LoggingConfiguration()
@@ -174,12 +174,8 @@ public struct ServiceGroupConfiguration: Sendable {
174174
services: [ServiceConfiguration],
175175
logger: Logger? = nil
176176
) {
177-
if logger == nil {
178-
LoggingSystem.bootstrap { SwiftLogNoOpLogHandler($0) }
179-
}
180-
181177
self.services = services
182-
self.logger = logger ?? .init(label: "")
178+
self.logger = logger
183179
}
184180

185181
/// Initializes a new ``ServiceGroupConfiguration``.
@@ -195,12 +191,8 @@ public struct ServiceGroupConfiguration: Sendable {
195191
cancellationSignals: [UnixSignal] = [],
196192
logger: Logger? = nil
197193
) {
198-
if logger == nil {
199-
LoggingSystem.bootstrap { SwiftLogNoOpLogHandler($0) }
200-
}
201-
202194
self.services = services
203-
self.logger = logger ?? .init(label: "")
195+
self.logger = logger
204196
self.gracefulShutdownSignals = gracefulShutdownSignals
205197
self.cancellationSignals = cancellationSignals
206198
}
@@ -214,12 +206,8 @@ public struct ServiceGroupConfiguration: Sendable {
214206
services: [Service],
215207
logger: Logger? = nil
216208
) {
217-
if logger == nil {
218-
LoggingSystem.bootstrap { SwiftLogNoOpLogHandler($0) }
219-
}
220-
221209
self.services = Array(services.map { ServiceConfiguration(service: $0) })
222-
self.logger = logger ?? .init(label: "")
210+
self.logger = logger
223211
}
224212

225213
/// Initializes a new ``ServiceGroupConfiguration``.
@@ -235,12 +223,8 @@ public struct ServiceGroupConfiguration: Sendable {
235223
cancellationSignals: [UnixSignal] = [],
236224
logger: Logger? = nil
237225
) {
238-
if logger == nil {
239-
LoggingSystem.bootstrap { SwiftLogNoOpLogHandler($0) }
240-
}
241-
242226
self.services = Array(services.map { ServiceConfiguration(service: $0) })
243-
self.logger = logger ?? .init(label: "")
227+
self.logger = logger
244228
self.gracefulShutdownSignals = gracefulShutdownSignals
245229
self.cancellationSignals = cancellationSignals
246230
}

0 commit comments

Comments
 (0)