Skip to content

Commit

Permalink
fix: add log debug for background task
Browse files Browse the repository at this point in the history
  • Loading branch information
duyhungtnn committed Aug 24, 2023
1 parent de71864 commit 72fd7c4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ final class EvaluationBackgroundTask {

do {
try BGTaskScheduler.shared.submit(request)
component?.config.logger?.debug(message: "The background task is scheduled.")
component?.config.logger?.debug(message: "[EvaluationBackgroundTask] The background task is scheduled.")
} catch {
component?.config.logger?.error(error)
}
}

private func handleAppRefresh(_ task: BGTask) {
component?.config.logger?.debug(message: "[EvaluationBackgroundTask] handleAppRefresh")
// Schedule a new refresh task.
scheduleAppRefresh()

Expand All @@ -39,13 +40,18 @@ final class EvaluationBackgroundTask {
timeoutMillis: nil,
completion: { error in
task.setTaskCompleted(success: error == nil)
if let error {
self?.component?.config.logger?.error(error)
} else {
self?.component?.config.logger?.warn(message: "[EventBackgroundTask] success")
}
}
)
}
}
// Provide the background task with an expiration handler that cancels the operation.
task.expirationHandler = { [weak self] in
self?.component?.config.logger?.warn(message: "The background task is expired.")
self?.component?.config.logger?.debug(message: "[EvaluationBackgroundTask] The background task is expired.")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ final class EventBackgroundTask {

do {
try BGTaskScheduler.shared.submit(request)
component?.config.logger?.debug(message: "[EventBackgroundTask] The background task is scheduled.")
} catch {
component?.config.logger?.error(error)
}
}

private func handleAppRefresh(_ task: BGTask) {
component?.config.logger?.debug(message: "[EventBackgroundTask] handleAppRefresh")
// Schedule a new refresh task.
scheduleAppRefresh()

Expand All @@ -35,12 +37,14 @@ final class EventBackgroundTask {
task.setTaskCompleted(success: error == nil)
if let error {
self?.component?.config.logger?.error(error)
} else {
self?.component?.config.logger?.warn(message: "[EventBackgroundTask] success")
}
}
}
// Provide the background task with an expiration handler that cancels the operation.
task.expirationHandler = { [weak self] in
self?.component?.config.logger?.warn(message: "The background task is expired.")
self?.component?.config.logger?.debug(message: "[EventBackgroundTask] The background task is expired.")
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions Bucketeer/Sources/Public/BKTBackgroundTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@ public final class BKTBackgroundTask {

public static func enable() {
// Register all background task here
BGTaskScheduler.shared.register(forTaskWithIdentifier: BackgroundTaskIndentifier.fetchEvaluations, using: nil) { task in
forwardTaskToHandler(BackgroundTaskIndentifier.fetchEvaluations, task)
}
BGTaskScheduler.shared.register(forTaskWithIdentifier: BackgroundTaskIndentifier.flushEvents, using: nil) { task in
forwardTaskToHandler(BackgroundTaskIndentifier.flushEvents, task)
}
BGTaskScheduler.shared.register(forTaskWithIdentifier: BackgroundTaskIndentifier.fetchEvaluations, using: nil) { task in
forwardTaskToHandler(BackgroundTaskIndentifier.fetchEvaluations, task)
}
}

private static func forwardTaskToHandler(_ identifier: String, _ task: BGTask) {
if let handler = handlerRegistry[identifier] {
handler.handle(task)
} else {
// no handler
task.setTaskCompleted(success: true)
}
}

Expand Down

0 comments on commit 72fd7c4

Please sign in to comment.