Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REPLAY-1963 Add background task coordinator #1412

Merged
merged 15 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased

- [IMPROVEMENT] Add UIBackgroundTask for uploading jobs. See [#1412][]

# 2.1.2 / 29-08-2023

- [BUGFIX] Do not embed DatadogInternal while building Trace and RUM xcframeworks. See [#1444][].
Expand Down Expand Up @@ -513,6 +515,7 @@ Release `2.0` introduces breaking changes. Follow the [Migration Guide](MIGRATIO
[#1419]: https://github.com/DataDog/dd-sdk-ios/pull/1419
[#1428]: https://github.com/DataDog/dd-sdk-ios/pull/1428
[#1444]: https://github.com/DataDog/dd-sdk-ios/pull/1444
[#1412]: https://github.com/DataDog/dd-sdk-ios/pull/1412
[@00fa9a]: https://github.com/00FA9A
[@britton-earnin]: https://github.com/Britton-Earnin
[@hengyu]: https://github.com/Hengyu
Expand Down
12 changes: 12 additions & 0 deletions Datadog/Datadog.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,8 @@
9E68FB55244707FD0013A8AA /* ObjcExceptionHandler.m in Sources */ = {isa = PBXBuildFile; fileRef = 9E68FB53244707FD0013A8AA /* ObjcExceptionHandler.m */; };
9E68FB56244707FD0013A8AA /* ObjcExceptionHandler.h in Headers */ = {isa = PBXBuildFile; fileRef = 9E68FB54244707FD0013A8AA /* ObjcExceptionHandler.h */; settings = {ATTRIBUTES = (Public, ); }; };
9EE5AD8226205B82001E699E /* DDNSURLSessionDelegateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EE5AD8126205B82001E699E /* DDNSURLSessionDelegateTests.swift */; };
A70A82652A935F210072F5DC /* BackgroundTaskCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = A70A82642A935F210072F5DC /* BackgroundTaskCoordinator.swift */; };
A70A82662A935F210072F5DC /* BackgroundTaskCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = A70A82642A935F210072F5DC /* BackgroundTaskCoordinator.swift */; };
A728ADAB2934EA2100397996 /* W3CHTTPHeadersWriter+objc.swift in Sources */ = {isa = PBXBuildFile; fileRef = A728ADAA2934EA2100397996 /* W3CHTTPHeadersWriter+objc.swift */; };
A728ADAC2934EA2100397996 /* W3CHTTPHeadersWriter+objc.swift in Sources */ = {isa = PBXBuildFile; fileRef = A728ADAA2934EA2100397996 /* W3CHTTPHeadersWriter+objc.swift */; };
A728ADB02934EB0900397996 /* DDW3CHTTPHeadersWriter+apiTests.m in Sources */ = {isa = PBXBuildFile; fileRef = A728ADAD2934EB0300397996 /* DDW3CHTTPHeadersWriter+apiTests.m */; };
Expand All @@ -458,6 +460,8 @@
A79B0F65292BD074008742B3 /* DDB3HTTPHeadersWriter+apiTests.m in Sources */ = {isa = PBXBuildFile; fileRef = A79B0F63292BD074008742B3 /* DDB3HTTPHeadersWriter+apiTests.m */; };
A79B0F66292BD7CA008742B3 /* B3HTTPHeadersWriter+objc.swift in Sources */ = {isa = PBXBuildFile; fileRef = A79B0F5E292BA435008742B3 /* B3HTTPHeadersWriter+objc.swift */; };
A79B0F67292BD7CC008742B3 /* B3HTTPHeadersWriter+objc.swift in Sources */ = {isa = PBXBuildFile; fileRef = A79B0F5E292BA435008742B3 /* B3HTTPHeadersWriter+objc.swift */; };
A7C816AB2A98CEBA00BF097B /* UIKitBackgroundTaskCoordinatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7C816AA2A98CEBA00BF097B /* UIKitBackgroundTaskCoordinatorTests.swift */; };
A7C816AC2A98CEBA00BF097B /* UIKitBackgroundTaskCoordinatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7C816AA2A98CEBA00BF097B /* UIKitBackgroundTaskCoordinatorTests.swift */; };
D20605A3287464F40047275C /* ContextValuePublisher.swift in Sources */ = {isa = PBXBuildFile; fileRef = D20605A2287464F40047275C /* ContextValuePublisher.swift */; };
D20605A4287464F40047275C /* ContextValuePublisher.swift in Sources */ = {isa = PBXBuildFile; fileRef = D20605A2287464F40047275C /* ContextValuePublisher.swift */; };
D20605A6287476230047275C /* ServerOffsetPublisher.swift in Sources */ = {isa = PBXBuildFile; fileRef = D20605A5287476230047275C /* ServerOffsetPublisher.swift */; };
Expand Down Expand Up @@ -2321,6 +2325,7 @@
9EC8B5D92668197B000F7529 /* VitalCPUReader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VitalCPUReader.swift; sourceTree = "<group>"; };
9EC8B5ED2668E4DB000F7529 /* VitalCPUReaderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VitalCPUReaderTests.swift; sourceTree = "<group>"; };
9EE5AD8126205B82001E699E /* DDNSURLSessionDelegateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DDNSURLSessionDelegateTests.swift; sourceTree = "<group>"; };
A70A82642A935F210072F5DC /* BackgroundTaskCoordinator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BackgroundTaskCoordinator.swift; sourceTree = "<group>"; };
A728AD9C2934CE4400397996 /* W3CHTTPHeaders.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = W3CHTTPHeaders.swift; sourceTree = "<group>"; };
A728AD9E2934CE5000397996 /* W3CHTTPHeadersWriter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = W3CHTTPHeadersWriter.swift; sourceTree = "<group>"; };
A728ADA02934CE5D00397996 /* W3CHTTPHeadersReader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = W3CHTTPHeadersReader.swift; sourceTree = "<group>"; };
Expand All @@ -2332,6 +2337,7 @@
A79B0F5E292BA435008742B3 /* B3HTTPHeadersWriter+objc.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "B3HTTPHeadersWriter+objc.swift"; sourceTree = "<group>"; };
A79B0F60292BB071008742B3 /* B3HTTPHeadersReaderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = B3HTTPHeadersReaderTests.swift; sourceTree = "<group>"; };
A79B0F63292BD074008742B3 /* DDB3HTTPHeadersWriter+apiTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "DDB3HTTPHeadersWriter+apiTests.m"; sourceTree = "<group>"; };
A7C816AA2A98CEBA00BF097B /* UIKitBackgroundTaskCoordinatorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIKitBackgroundTaskCoordinatorTests.swift; sourceTree = "<group>"; };
A7F773D32924EA2D00AC1A62 /* B3HTTPHeaders.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = B3HTTPHeaders.swift; sourceTree = "<group>"; };
A7F773DB29253F8B00AC1A62 /* B3HTTPHeadersWriter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = B3HTTPHeadersWriter.swift; sourceTree = "<group>"; };
A7F773DC29253F8B00AC1A62 /* B3HTTPHeadersReader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = B3HTTPHeadersReader.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3875,6 +3881,7 @@
61133C322423990D00786299 /* DataUploaderTests.swift */,
61133C342423990D00786299 /* URLSessionClientTests.swift */,
61133C332423990D00786299 /* RequestBuilderTests.swift */,
A7C816AA2A98CEBA00BF097B /* UIKitBackgroundTaskCoordinatorTests.swift */,
);
path = Upload;
sourceTree = "<group>";
Expand Down Expand Up @@ -5379,6 +5386,7 @@
D26C49B428893E5300802B2D /* Upload */ = {
isa = PBXGroup;
children = (
A70A82642A935F210072F5DC /* BackgroundTaskCoordinator.swift */,
D26C49BE288982DA00802B2D /* FeatureUpload.swift */,
61133BB32423979B00786299 /* DataUploadDelay.swift */,
61ED39D326C2A36B002C0F26 /* DataUploadStatus.swift */,
Expand Down Expand Up @@ -7219,6 +7227,7 @@
61DA8CA928609C5B0074A606 /* Directories.swift in Sources */,
D2EFA868286DA85700F1FAA6 /* DatadogContextProvider.swift in Sources */,
D26C49BF288982DA00802B2D /* FeatureUpload.swift in Sources */,
A70A82652A935F210072F5DC /* BackgroundTaskCoordinator.swift in Sources */,
61D3E0D2277B23F1008BE766 /* KronosInternetAddress.swift in Sources */,
D2553826288F0B1A00727FAD /* BatteryStatusPublisher.swift in Sources */,
61D3E0D5277B23F1008BE766 /* KronosNTPPacket.swift in Sources */,
Expand Down Expand Up @@ -7286,6 +7295,7 @@
61B8BA91281812F60068AFF4 /* KronosInternetAddressTests.swift in Sources */,
614798962A459AA80095CB02 /* DDTraceTests.swift in Sources */,
D25085102976E30000E931C3 /* DatadogRemoteFeatureMock.swift in Sources */,
A7C816AB2A98CEBA00BF097B /* UIKitBackgroundTaskCoordinatorTests.swift in Sources */,
61A1A44929643254007909E7 /* DatadogCoreProxy.swift in Sources */,
D2A1EE3B287EECC000D28DFB /* CarrierInfoPublisherTests.swift in Sources */,
D22743D829DEB8B4001A7EF9 /* VitalInfoTests.swift in Sources */,
Expand Down Expand Up @@ -8294,6 +8304,7 @@
D20605AA2874C1CD0047275C /* NetworkConnectionInfoPublisher.swift in Sources */,
D29CDD3328211A2200F7DAA5 /* DataBlock.swift in Sources */,
D2612F48290197C700509B7D /* LaunchTimePublisher.swift in Sources */,
A70A82662A935F210072F5DC /* BackgroundTaskCoordinator.swift in Sources */,
D20605CA2875A83D0047275C /* ContextValueReader.swift in Sources */,
D2A1EE24287740B500D28DFB /* ApplicationStatePublisher.swift in Sources */,
D2CB6E2927C50EAE00A62B57 /* KronosInternetAddress.swift in Sources */,
Expand Down Expand Up @@ -8388,6 +8399,7 @@
D24C9C7229A7D57A002057CF /* DirectoriesMock.swift in Sources */,
61DA8CB3286215DE0074A606 /* CryptographyTests.swift in Sources */,
D2CB6F0427C520D400A62B57 /* DDTracerTests.swift in Sources */,
A7C816AC2A98CEBA00BF097B /* UIKitBackgroundTaskCoordinatorTests.swift in Sources */,
D24C9C6129A7CB0C002057CF /* DatadogLogsFeatureTests.swift in Sources */,
D29A9FCF29DDC4BC005C54A4 /* RUMFeatureMocks.swift in Sources */,
D22743DE29DEB8B5001A7EF9 /* VitalInfoSamplerTests.swift in Sources */,
Expand Down
12 changes: 7 additions & 5 deletions DatadogCore/Sources/Core/DatadogCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ internal final class DatadogCore {
/// The core context provider.
internal let contextProvider: DatadogContextProvider

internal let backgroundTasksEnabled: Bool

/// Creates a core instance.
///
/// - Parameters:
Expand All @@ -84,14 +86,16 @@ internal final class DatadogCore {
httpClient: HTTPClient,
encryption: DataEncryption?,
contextProvider: DatadogContextProvider,
applicationVersion: String
applicationVersion: String,
backgroundTasksEnabled: Bool
) {
self.directory = directory
self.dateProvider = dateProvider
self.performance = performance
self.httpClient = httpClient
self.encryption = encryption
self.contextProvider = contextProvider
self.backgroundTasksEnabled = backgroundTasksEnabled
self.applicationVersionPublisher = ApplicationVersionPublisher(version: applicationVersion)
self.consentPublisher = TrackingConsentPublisher(consent: initialConsent)

Expand Down Expand Up @@ -241,6 +245,7 @@ extension DatadogCore: DatadogCoreProtocol {
requestBuilder: feature.requestBuilder,
httpClient: httpClient,
performance: performancePreset,
backgroundTasksEnabled: backgroundTasksEnabled,
telemetry: telemetry
)

Expand Down Expand Up @@ -313,10 +318,7 @@ internal struct DatadogCoreFeatureScope: FeatureScope {
// On user thread: request SDK context.
contextProvider.read { context in
// On context thread: request writer for current tracking consent.
let writer = storage.writer(
for: bypassConsent ? .granted : context.trackingConsent,
forceNewBatch: forceNewBatch
)
let writer = storage.writer(for: context, bypassConsent: bypassConsent, forceNewBatch: forceNewBatch)

// Still on context thread: send `Writer` to EWC caller. The writer implements `AsyncWriter`, so
// the implementation of `writer.write(value:)` will run asynchronously without blocking the context thread.
Expand Down
14 changes: 10 additions & 4 deletions DatadogCore/Sources/Core/Storage/FeatureStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,20 @@ internal struct FeatureStorage {
/// Telemetry interface.
let telemetry: Telemetry

func writer(for trackingConsent: TrackingConsent, forceNewBatch: Bool) -> Writer {
switch trackingConsent {
func writer(
for context: DatadogContext,
bypassConsent: Bool = false,
forceNewBatch: Bool = false
) -> Writer {
switch bypassConsent ? .granted : context.trackingConsent {
case .granted:
return AsyncWriter(
execute: FileWriter(
orchestrator: authorizedFilesOrchestrator,
forceNewFile: forceNewBatch,
encryption: encryption,
telemetry: telemetry
telemetry: telemetry,
context: context
),
on: queue
)
Expand All @@ -43,7 +48,8 @@ internal struct FeatureStorage {
orchestrator: unauthorizedFilesOrchestrator,
forceNewFile: forceNewBatch,
encryption: encryption,
telemetry: telemetry
telemetry: telemetry,
context: context
),
on: queue
)
Expand Down
Loading