Skip to content

Commit

Permalink
v2.0.12 - better handling of bad cached sofa feeds
Browse files Browse the repository at this point in the history
  • Loading branch information
erikng committed Sep 18, 2024
1 parent 726804a commit 501a7ea
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.0.12] - 2024-09-18
Requires macOS 12.0 and higher.

### Fixed
- When cached SOFA feed is malformed, attempt to retry downloading and invalidate for future Nudge runs

## [2.0.11] - 2024-08-22
Requires macOS 12.0 and higher.

Expand Down
4 changes: 2 additions & 2 deletions Nudge.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 12.0;
MARKETING_VERSION = 2.0.11;
MARKETING_VERSION = 2.0.12;
PRODUCT_BUNDLE_IDENTIFIER = com.github.macadmins.Nudge;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down Expand Up @@ -729,7 +729,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 12.0;
MARKETING_VERSION = 2.0.11;
MARKETING_VERSION = 2.0.12;
PRODUCT_BUNDLE_IDENTIFIER = com.github.macadmins.Nudge;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down
8 changes: 6 additions & 2 deletions Nudge/3rd Party Assets/sofa.swift
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ extension MacOSDataFeed {
}

class SOFA: NSObject, URLSessionDelegate {
func URLSync(url: URL, maxRetries: Int = 3) -> (data: Data?, response: URLResponse?, error: Error?, responseCode: Int?, eTag: String?) {
func URLSync(url: URL, maxRetries: Int = 3, clearCache: Bool = false) -> (data: Data?, response: URLResponse?, error: Error?, responseCode: Int?, eTag: String?) {
if url.scheme == "file" {
do {
let data = try Data(contentsOf: url)
Expand All @@ -233,10 +233,14 @@ class SOFA: NSObject, URLSessionDelegate {
}

let semaphore = DispatchSemaphore(value: 0)
let lastEtag = Globals.nudgeDefaults.string(forKey: "LastEtag") ?? ""
var lastEtag = Globals.nudgeDefaults.string(forKey: "LastEtag") ?? ""
var request = URLRequest(url: url)
let config = URLSessionConfiguration.default
config.requestCachePolicy = .useProtocolCachePolicy
if clearCache {
URLCache.shared.removeAllCachedResponses()
lastEtag = ""
}
let session = URLSession(configuration: config, delegate: self, delegateQueue: nil)

request.addValue("\(Globals.bundleID)/\(VersionManager.getNudgeVersion())", forHTTPHeaderField: "User-Agent")
Expand Down
4 changes: 2 additions & 2 deletions Nudge/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>2.0.11</string>
<string>2.0.12</string>
<key>CFBundleVersion</key>
<string>2.0.11</string>
<string>2.0.12</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>LSMinimumSystemVersion</key>
Expand Down
7 changes: 5 additions & 2 deletions Nudge/Utilities/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1219,7 +1219,7 @@ struct NetworkFileManager {
}

func redownloadAndReprocessSOFA(url: URL) -> MacOSDataFeed? {
let sofaData = SOFA().URLSync(url: url)
let sofaData = SOFA().URLSync(url: url, clearCache: true)
if let responseCode = sofaData.responseCode, responseCode == 200 {
let fileManager = FileManager.default
let appSupportDirectory = fileManager.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
Expand All @@ -1233,10 +1233,13 @@ struct NetworkFileManager {
try data.write(to: sofaPath)
}
let assetInfo = try MacOSDataFeed(data: data)
Globals.nudgeDefaults.set(sofaData.eTag, forKey: "LastEtag")
return assetInfo
} else {
LogManager.error("Failed to fetch sofa JSON: No data received.", logger: sofaLog)
return redownloadAndReprocessSOFA(url: url)
LogManager.error("Could not fetch SOFA feed", logger: sofaLog)
nudgePrimaryState.shouldExit = true
exit(1)
}
} catch {
LogManager.error("Failed to decode sofa JSON after redownload: \(error.localizedDescription)", logger: sofaLog)
Expand Down

0 comments on commit 501a7ea

Please sign in to comment.