2024-11-14 10:02:26 +00:00
|
|
|
//
|
|
|
|
// MigrationManager.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 11/13/24.
|
|
|
|
// Copyright (c) 2024 Davide De Rosa. All rights reserved.
|
|
|
|
//
|
|
|
|
// https://github.com/passepartoutvpn
|
|
|
|
//
|
|
|
|
// This file is part of Passepartout.
|
|
|
|
//
|
|
|
|
// Passepartout is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Passepartout is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import PassepartoutKit
|
|
|
|
|
2024-11-21 18:44:20 +00:00
|
|
|
public protocol MigrationManagerImporter {
|
|
|
|
func importProfile(_ profile: Profile) async throws
|
|
|
|
}
|
|
|
|
|
2024-11-14 10:02:26 +00:00
|
|
|
@MainActor
|
|
|
|
public final class MigrationManager: ObservableObject {
|
|
|
|
public struct Simulation {
|
2024-11-14 14:11:25 +00:00
|
|
|
public let fakeProfiles: Bool
|
|
|
|
|
2024-11-14 10:02:26 +00:00
|
|
|
public let maxMigrationTime: Double?
|
|
|
|
|
|
|
|
public let randomFailures: Bool
|
|
|
|
|
2024-11-14 14:11:25 +00:00
|
|
|
public init(fakeProfiles: Bool, maxMigrationTime: Double?, randomFailures: Bool) {
|
|
|
|
self.fakeProfiles = fakeProfiles
|
2024-11-14 10:02:26 +00:00
|
|
|
self.maxMigrationTime = maxMigrationTime
|
|
|
|
self.randomFailures = randomFailures
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private let profileStrategy: ProfileMigrationStrategy
|
|
|
|
|
|
|
|
private nonisolated let simulation: Simulation?
|
|
|
|
|
2024-11-16 11:29:03 +00:00
|
|
|
public init(
|
|
|
|
profileStrategy: ProfileMigrationStrategy? = nil,
|
|
|
|
simulation: Simulation? = nil
|
|
|
|
) {
|
2024-11-14 10:02:26 +00:00
|
|
|
self.profileStrategy = profileStrategy ?? DummyProfileStrategy()
|
|
|
|
self.simulation = simulation
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-14 14:11:25 +00:00
|
|
|
// MARK: - Public interface
|
|
|
|
|
2024-11-14 10:02:26 +00:00
|
|
|
extension MigrationManager {
|
|
|
|
public func fetchMigratableProfiles() async throws -> [MigratableProfile] {
|
|
|
|
try await profileStrategy.fetchMigratableProfiles()
|
|
|
|
}
|
|
|
|
|
2024-11-16 11:29:03 +00:00
|
|
|
public func migratedProfile(withId profileId: UUID) async throws -> Profile? {
|
2024-11-14 10:02:26 +00:00
|
|
|
try await profileStrategy.fetchProfile(withId: profileId)
|
|
|
|
}
|
|
|
|
|
2024-11-16 11:29:03 +00:00
|
|
|
public func migratedProfiles(
|
|
|
|
_ migratableProfiles: [MigratableProfile],
|
2024-11-14 10:02:26 +00:00
|
|
|
onUpdate: @escaping @MainActor (UUID, MigrationStatus) -> Void
|
|
|
|
) async throws -> [Profile] {
|
2024-11-16 11:29:03 +00:00
|
|
|
migratableProfiles.forEach {
|
|
|
|
onUpdate($0.id, .pending)
|
2024-11-14 10:02:26 +00:00
|
|
|
}
|
|
|
|
return try await withThrowingTaskGroup(of: Profile?.self, returning: [Profile].self) { group in
|
2024-11-16 11:29:03 +00:00
|
|
|
migratableProfiles.forEach { migratable in
|
2024-11-14 10:02:26 +00:00
|
|
|
group.addTask {
|
|
|
|
do {
|
2024-11-14 14:11:25 +00:00
|
|
|
try await self.simulateBehavior()
|
2024-11-16 11:29:03 +00:00
|
|
|
guard let profile = try await self.simulateMigrateProfile(withId: migratable.id) else {
|
|
|
|
await onUpdate(migratable.id, .failed)
|
2024-11-14 10:02:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-11-16 20:16:25 +00:00
|
|
|
await onUpdate(migratable.id, .done)
|
2024-11-14 10:02:26 +00:00
|
|
|
return profile
|
|
|
|
} catch {
|
2024-11-16 11:29:03 +00:00
|
|
|
await onUpdate(migratable.id, .failed)
|
2024-11-14 10:02:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var profiles: [Profile] = []
|
|
|
|
for try await profile in group {
|
|
|
|
guard let profile else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
profiles.append(profile)
|
|
|
|
}
|
|
|
|
return profiles
|
|
|
|
}
|
|
|
|
}
|
2024-11-14 14:11:25 +00:00
|
|
|
|
|
|
|
public func importProfiles(
|
|
|
|
_ profiles: [Profile],
|
2024-11-21 18:44:20 +00:00
|
|
|
into importer: MigrationManagerImporter,
|
2024-11-14 14:11:25 +00:00
|
|
|
onUpdate: @escaping @MainActor (UUID, MigrationStatus) -> Void
|
|
|
|
) async {
|
|
|
|
profiles.forEach {
|
|
|
|
onUpdate($0.id, .pending)
|
|
|
|
}
|
|
|
|
await withTaskGroup(of: Void.self) { group in
|
|
|
|
profiles.forEach { profile in
|
|
|
|
group.addTask {
|
|
|
|
do {
|
|
|
|
try await self.simulateBehavior()
|
2024-11-21 18:44:20 +00:00
|
|
|
try await self.simulateSaveProfile(profile, to: importer)
|
2024-11-16 20:16:25 +00:00
|
|
|
await onUpdate(profile.id, .done)
|
2024-11-14 14:11:25 +00:00
|
|
|
} catch {
|
|
|
|
await onUpdate(profile.id, .failed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-16 11:29:03 +00:00
|
|
|
|
|
|
|
public func deleteMigratableProfiles(withIds profileIds: Set<UUID>) async throws {
|
|
|
|
try await simulateDeleteProfiles(withIds: profileIds)
|
|
|
|
}
|
2024-11-14 14:11:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Simulation
|
|
|
|
|
|
|
|
private extension MigrationManager {
|
|
|
|
func simulateBehavior() async throws {
|
|
|
|
guard let simulation else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if let maxMigrationTime = simulation.maxMigrationTime {
|
|
|
|
try await Task.sleep(for: .seconds(.random(in: 1.0..<maxMigrationTime)))
|
|
|
|
}
|
|
|
|
if simulation.randomFailures, Bool.random() {
|
|
|
|
throw PassepartoutError(.unhandled)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func simulateMigrateProfile(withId profileId: UUID) async throws -> Profile? {
|
|
|
|
if simulation?.fakeProfiles ?? false {
|
|
|
|
return try? Profile.Builder(id: profileId).tryBuild()
|
|
|
|
}
|
|
|
|
return try await profileStrategy.fetchProfile(withId: profileId)
|
|
|
|
}
|
|
|
|
|
2024-11-21 18:44:20 +00:00
|
|
|
func simulateSaveProfile(_ profile: Profile, to importer: MigrationManagerImporter) async throws {
|
2024-11-14 14:11:25 +00:00
|
|
|
if simulation?.fakeProfiles ?? false {
|
|
|
|
return
|
|
|
|
}
|
2024-11-21 18:44:20 +00:00
|
|
|
try await importer.importProfile(profile)
|
2024-11-14 14:11:25 +00:00
|
|
|
}
|
2024-11-16 11:29:03 +00:00
|
|
|
|
|
|
|
func simulateDeleteProfiles(withIds profileIds: Set<UUID>) async throws {
|
|
|
|
if simulation?.fakeProfiles ?? false {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
try await profileStrategy.deleteProfiles(withIds: profileIds)
|
|
|
|
}
|
2024-11-14 10:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Dummy
|
|
|
|
|
|
|
|
private final class DummyProfileStrategy: ProfileMigrationStrategy {
|
|
|
|
public init() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public func fetchMigratableProfiles() async throws -> [MigratableProfile] {
|
|
|
|
[]
|
|
|
|
}
|
|
|
|
|
|
|
|
func fetchProfile(withId profileId: UUID) async throws -> Profile? {
|
|
|
|
nil
|
|
|
|
}
|
2024-11-16 11:29:03 +00:00
|
|
|
|
|
|
|
func deleteProfiles(withIds profileIds: Set<UUID>) async throws {
|
|
|
|
}
|
2024-11-14 10:02:26 +00:00
|
|
|
}
|