Update profiles at start if background refresh unavailable

This commit is contained in:
世界
2023-11-09 15:48:27 +08:00
parent 8bf24dba8a
commit 1b12837eec
3 changed files with 42 additions and 23 deletions
@@ -47,7 +47,7 @@ public enum ProfileUpdateTask {
}
}
static func updateProfiles(_ profiles: [Profile]) async throws -> Bool {
static func updateProfiles(_ profiles: [Profile]) async -> Bool {
var success = true
for profile in profiles {
if profile.lastUpdated! > Date(timeIntervalSinceNow: -profile.autoUpdateIntervalOrDefault) {
@@ -55,6 +55,7 @@ public enum ProfileUpdateTask {
}
do {
try await profile.updateRemoteProfile()
NSLog("Updated profile \(profile.name)")
} catch {
NSLog("Update profile \(profile.name) failed: \(error.localizedDescription)")
success = false
@@ -1,6 +1,9 @@
import BackgroundTasks
import Foundation
import Library
#if canImport(UIKit)
import UIKit
#endif
#if os(iOS) || os(tvOS)
public class UIProfileUpdateTask: BGAppRefreshTask {
@@ -13,7 +16,7 @@ import Library
let success = BGTaskScheduler.shared.register(forTaskWithIdentifier: taskSchedulerPermittedIdentifier, using: nil) { task in
NSLog("profile update task started")
Task {
await UIProfileUpdateTask.getAndupdateProfiles(task)
await UIProfileUpdateTask.getAndUpdateProfiles(task)
}
}
if !success {
@@ -33,9 +36,15 @@ import Library
private static let register = Register()
public static func configure() async throws {
try await register.configure()
if await UIApplication.shared.backgroundRefreshStatus != .available {
Task {
await updateOnce()
}
}
}
private nonisolated static func getAndupdateProfiles(_ task: BGTask) async {
private nonisolated static func updateOnce() async {
NSLog("update profiles at start since background refresh unavailable")
let profiles: [Profile]
do {
profiles = try await ProfileManager.listAutoUpdateEnabled()
@@ -45,19 +54,24 @@ import Library
if profiles.isEmpty {
return
}
await ProfileUpdateTask.updateProfiles(profiles)
}
private nonisolated static func getAndUpdateProfiles(_ task: BGTask) async {
let profiles: [Profile]
do {
let success = try await ProfileUpdateTask.updateProfiles(profiles)
try? scheduleUpdate(ProfileUpdateTask.calculateEarliestBeginDate(profiles))
task.setTaskCompleted(success: success)
NSLog("profile update task succeed")
profiles = try await ProfileManager.listAutoUpdateEnabled()
} catch {
try? scheduleUpdate(nil)
task.setTaskCompleted(success: false)
NSLog("profile update task failed: \(error.localizedDescription)")
return
}
if profiles.isEmpty {
return
}
let success = await ProfileUpdateTask.updateProfiles(profiles)
try? scheduleUpdate(ProfileUpdateTask.calculateEarliestBeginDate(profiles))
task.setTaskCompleted(success: success)
task.expirationHandler = {
try? scheduleUpdate(nil)
NSLog("profile update task expired")
}
}
+16 -12
View File
@@ -1966,7 +1966,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.6.0;
MARKETING_VERSION = 1.7.0;
PRODUCT_BUNDLE_IDENTIFIER = io.nekohasekai.sfa;
PRODUCT_NAME = "sing-box";
SDKROOT = appletvos;
@@ -2000,7 +2000,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.6.0;
MARKETING_VERSION = 1.7.0;
PRODUCT_BUNDLE_IDENTIFIER = io.nekohasekai.sfa;
PRODUCT_NAME = "sing-box";
SDKROOT = appletvos;
@@ -2017,6 +2017,7 @@
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_ENTITLEMENTS = TVExtension/TVExtension.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = Z56Z6NYZN2;
@@ -2033,6 +2034,7 @@
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = io.nekohasekai.sfa.extension;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SDKROOT = iphoneos;
SKIP_INSTALL = YES;
SUPPORTED_PLATFORMS = "appletvos appletvsimulator";
@@ -2049,6 +2051,7 @@
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_ENTITLEMENTS = TVExtension/TVExtension.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = Z56Z6NYZN2;
@@ -2065,6 +2068,7 @@
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = io.nekohasekai.sfa.extension;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SDKROOT = iphoneos;
SKIP_INSTALL = YES;
SUPPORTED_PLATFORMS = "appletvos appletvsimulator";
@@ -2234,7 +2238,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.6.0;
MARKETING_VERSION = 1.7.0;
OTHER_CODE_SIGN_FLAGS = "--deep";
PRODUCT_BUNDLE_IDENTIFIER = io.nekohasekai.sfa;
PRODUCT_NAME = "sing-box";
@@ -2274,7 +2278,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.6.0;
MARKETING_VERSION = 1.7.0;
OTHER_CODE_SIGN_FLAGS = "--deep";
PRODUCT_BUNDLE_IDENTIFIER = io.nekohasekai.sfa;
PRODUCT_NAME = "sing-box";
@@ -2297,7 +2301,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 88;
CURRENT_PROJECT_VERSION = 109;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_TEAM = Z56Z6NYZN2;
ENABLE_HARDENED_RUNTIME = YES;
@@ -2313,7 +2317,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 13.0;
MARKETING_VERSION = 1.6.0;
MARKETING_VERSION = 1.7.0;
OTHER_CODE_SIGN_FLAGS = "";
PRODUCT_BUNDLE_IDENTIFIER = io.nekohasekai.sfa;
PRODUCT_NAME = "sing-box";
@@ -2335,7 +2339,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 88;
CURRENT_PROJECT_VERSION = 109;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_TEAM = Z56Z6NYZN2;
ENABLE_HARDENED_RUNTIME = YES;
@@ -2351,7 +2355,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 13.0;
MARKETING_VERSION = 1.6.0;
MARKETING_VERSION = 1.7.0;
OTHER_CODE_SIGN_FLAGS = "";
PRODUCT_BUNDLE_IDENTIFIER = io.nekohasekai.sfa;
PRODUCT_NAME = "sing-box";
@@ -2493,7 +2497,7 @@
"@executable_path/../../../../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 13.0;
MARKETING_VERSION = 1.6.0-alpha.3;
MARKETING_VERSION = 1.7.0-alpha.10;
PRODUCT_BUNDLE_IDENTIFIER = io.nekohasekai.sfa.system;
PRODUCT_NAME = "$(inherited)";
PROVISIONING_PROFILE_SPECIFIER = "";
@@ -2529,7 +2533,7 @@
"@executable_path/../../../../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 13.0;
MARKETING_VERSION = 1.6.0-alpha.3;
MARKETING_VERSION = 1.7.0-alpha.10;
PRODUCT_BUNDLE_IDENTIFIER = io.nekohasekai.sfa.system;
PRODUCT_NAME = "$(inherited)";
PROVISIONING_PROFILE_SPECIFIER = "";
@@ -2569,7 +2573,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 13.0;
MARKETING_VERSION = 1.6.0-alpha.3;
MARKETING_VERSION = 1.7.0-alpha.10;
PRODUCT_BUNDLE_IDENTIFIER = io.nekohasekai.sfa.independent;
PRODUCT_NAME = SFM;
PROVISIONING_PROFILE_SPECIFIER = "";
@@ -2608,7 +2612,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 13.0;
MARKETING_VERSION = 1.6.0-alpha.3;
MARKETING_VERSION = 1.7.0-alpha.10;
PRODUCT_BUNDLE_IDENTIFIER = io.nekohasekai.sfa.independent;
PRODUCT_NAME = SFM;
PROVISIONING_PROFILE_SPECIFIER = "";