Skip to content

Commit 323cbe4

Browse files
committed
Allow max catalog size to be passed in
1 parent 9c2e389 commit 323cbe4

File tree

1 file changed

+64
-17
lines changed

1 file changed

+64
-17
lines changed

Modules/Sources/Yosemite/Tools/POS/POSCatalogSyncCoordinator.swift

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,30 @@ public protocol POSCatalogSyncCoordinatorProtocol {
1616
/// - Returns: True if a sync should be performed
1717
func shouldPerformFullSync(for siteID: Int64, maxAge: TimeInterval) async -> Bool
1818

19+
/// Determines if a full sync should be performed based on the age of the last sync
20+
/// - Parameters:
21+
/// - siteID: The site ID to check
22+
/// - maxAge: Maximum age before a sync is considered stale
23+
/// - maxCatalogSize: Maximum allowed catalog size for syncing (default: 1000)
24+
/// - Returns: True if a sync should be performed
25+
func shouldPerformFullSync(for siteID: Int64, maxAge: TimeInterval, maxCatalogSize: Int) async -> Bool
26+
1927
/// Performs an incremental sync if applicable based on sync conditions
2028
/// - Parameters:
2129
/// - siteID: The site ID to sync catalog for
2230
/// - forceSync: Whether to bypass age checks and always sync
2331
/// - Throws: POSCatalogSyncError.syncAlreadyInProgress if a sync is already running for this site
2432
//periphery:ignore - remove ignore comment when incremental sync is integrated with POS
2533
func performIncrementalSyncIfApplicable(for siteID: Int64, forceSync: Bool) async throws
34+
35+
/// Performs an incremental sync if applicable based on sync conditions
36+
/// - Parameters:
37+
/// - siteID: The site ID to sync catalog for
38+
/// - forceSync: Whether to bypass age checks and always sync
39+
/// - maxCatalogSize: Maximum allowed catalog size for syncing (default: 1000)
40+
/// - Throws: POSCatalogSyncError.syncAlreadyInProgress if a sync is already running for this site
41+
//periphery:ignore - remove ignore comment when incremental sync is integrated with POS
42+
func performIncrementalSyncIfApplicable(for siteID: Int64, forceSync: Bool, maxCatalogSize: Int) async throws
2643
}
2744

2845
public enum POSCatalogSyncError: Error, Equatable {
@@ -75,18 +92,20 @@ public actor POSCatalogSyncCoordinator: POSCatalogSyncCoordinatorProtocol {
7592
}
7693

7794
public func shouldPerformFullSync(for siteID: Int64, maxAge: TimeInterval) async -> Bool {
78-
guard let catalogSize = try? await catalogSizeChecker.checkCatalogSize(for: siteID) else {
79-
DDLogError("📋 POSCatalogSyncCoordinator: Could not get catalog size for site \(siteID)")
80-
return false
81-
}
95+
return await shouldPerformFullSync(for: siteID, maxAge: maxAge, maxCatalogSize: 1000)
96+
}
8297

83-
guard catalogSize.totalCount <= 1000 else {
84-
DDLogInfo("📋 POSCatalogSyncCoordinator: Site \(siteID) has catalog size \(catalogSize.totalCount), greater than 1000, should not sync.")
98+
/// Determines if a full sync should be performed based on the age of the last sync
99+
/// - Parameters:
100+
/// - siteID: The site ID to check
101+
/// - maxAge: Maximum age before a sync is considered stale
102+
/// - maxCatalogSize: Maximum allowed catalog size for syncing
103+
/// - Returns: True if a sync should be performed
104+
public func shouldPerformFullSync(for siteID: Int64, maxAge: TimeInterval, maxCatalogSize: Int) async -> Bool {
105+
guard await isCatalogSizeWithinLimit(for: siteID, maxCatalogSize: maxCatalogSize) else {
85106
return false
86107
}
87108

88-
DDLogInfo("📋 POSCatalogSyncCoordinator: Site \(siteID) has catalog size \(catalogSize.totalCount), with \(catalogSize.productCount) products and \(catalogSize.variationCount) variations")
89-
90109
if !siteExistsInDatabase(siteID: siteID) {
91110
DDLogInfo("📋 POSCatalogSyncCoordinator: Site \(siteID) not found in database, sync needed")
92111
return true
@@ -101,27 +120,33 @@ public actor POSCatalogSyncCoordinator: POSCatalogSyncCoordinatorProtocol {
101120
let shouldSync = age > maxAge
102121

103122
if shouldSync {
104-
DDLogInfo("📋 POSCatalogSyncCoordinator: Last sync for site \(siteID) was \(Int(age))s ago (max: \(Int(maxAge))s), sync needed")
123+
DDLogInfo("📋 POSCatalogSyncCoordinator: Last sync for site \(siteID) was \(Int(age))s ago " +
124+
"(max: \(Int(maxAge))s), sync needed")
105125
} else {
106-
DDLogInfo("📋 POSCatalogSyncCoordinator: Last sync for site \(siteID) was \(Int(age))s ago (max: \(Int(maxAge))s), sync not needed")
126+
DDLogInfo("📋 POSCatalogSyncCoordinator: Last sync for site \(siteID) was \(Int(age))s ago " +
127+
"(max: \(Int(maxAge))s), sync not needed")
107128
}
108129

109130
return shouldSync
110131
}
111132

112133
public func performIncrementalSyncIfApplicable(for siteID: Int64, forceSync: Bool) async throws {
134+
try await performIncrementalSyncIfApplicable(for: siteID, forceSync: forceSync, maxCatalogSize: 1000)
135+
}
136+
137+
/// Performs an incremental sync if applicable based on sync conditions
138+
/// - Parameters:
139+
/// - siteID: The site ID to sync catalog for
140+
/// - forceSync: Whether to bypass age checks and always sync
141+
/// - maxCatalogSize: Maximum allowed catalog size for syncing
142+
/// - Throws: POSCatalogSyncError.syncAlreadyInProgress if a sync is already running for this site
143+
public func performIncrementalSyncIfApplicable(for siteID: Int64, forceSync: Bool, maxCatalogSize: Int) async throws {
113144
if ongoingIncrementalSyncs.contains(siteID) {
114145
DDLogInfo("⚠️ POSCatalogSyncCoordinator: Incremental sync already in progress for site \(siteID)")
115146
throw POSCatalogSyncError.syncAlreadyInProgress(siteID: siteID)
116147
}
117148

118-
guard let catalogSize = try? await catalogSizeChecker.checkCatalogSize(for: siteID) else {
119-
DDLogError("📋 POSCatalogSyncCoordinator: Could not get catalog size for site \(siteID)")
120-
return
121-
}
122-
123-
guard catalogSize.totalCount <= 1000 else {
124-
DDLogInfo("📋 POSCatalogSyncCoordinator: Site \(siteID) has catalog size \(catalogSize.totalCount), greater than 1000, should not perform incremental sync.")
149+
guard await isCatalogSizeWithinLimit(for: siteID, maxCatalogSize: maxCatalogSize) else {
125150
return
126151
}
127152

@@ -155,6 +180,28 @@ public actor POSCatalogSyncCoordinator: POSCatalogSyncCoordinatorProtocol {
155180

156181
// MARK: - Private
157182

183+
/// Checks if the catalog size is within the specified sync limit
184+
/// - Parameters:
185+
/// - siteID: The site ID to check
186+
/// - maxCatalogSize: Maximum allowed catalog size for syncing
187+
/// - Returns: True if catalog size is within limit or if size cannot be determined
188+
private func isCatalogSizeWithinLimit(for siteID: Int64, maxCatalogSize: Int) async -> Bool {
189+
guard let catalogSize = try? await catalogSizeChecker.checkCatalogSize(for: siteID) else {
190+
DDLogError("📋 POSCatalogSyncCoordinator: Could not get catalog size for site \(siteID)")
191+
return false
192+
}
193+
194+
guard catalogSize.totalCount <= maxCatalogSize else {
195+
DDLogInfo("📋 POSCatalogSyncCoordinator: Site \(siteID) has catalog size \(catalogSize.totalCount), " +
196+
"greater than \(maxCatalogSize), should not sync.")
197+
return false
198+
}
199+
200+
DDLogInfo("📋 POSCatalogSyncCoordinator: Site \(siteID) has catalog size \(catalogSize.totalCount), with " +
201+
"\(catalogSize.productCount) products and \(catalogSize.variationCount) variations")
202+
return true
203+
}
204+
158205
private func lastFullSyncDate(for siteID: Int64) async -> Date? {
159206
do {
160207
return try await grdbManager.databaseConnection.read { db in

0 commit comments

Comments
 (0)