Skip to content

Commit

Permalink
Removing the ahead-of-time node scanning from GraphQL processing, as …
Browse files Browse the repository at this point in the history
…it was causing crashes
  • Loading branch information
ptsochantaris committed Jul 26, 2023
1 parent 3c6fc4b commit 2468e30
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 162 deletions.
102 changes: 0 additions & 102 deletions GraphQLProcessor.swift

This file was deleted.

109 changes: 55 additions & 54 deletions Shared/GraphQL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ enum GraphQL {
}
}

private static let nodeBlockMax = 2000

enum Profile: Int {
case light = -20
case cautious = -10
Expand Down Expand Up @@ -488,37 +486,25 @@ enum GraphQL {
return
}

let processor = Processor()
let itemsByServer = Dictionary(grouping: items) { $0.apiServer }
var count = 0
for (server, items) in itemsByServer {
let ids = items.compactMap(\.nodeId)
var nodes = [String: Lista<Node>]()
let serverName = server.label ?? "<no label>"
let nodeBlock = { (node: Node) in
let nodeBlock: Query.PerNodeBlock = { node in
let type = node.elementType
if let existingList = nodes[type] {
existingList.append(node)
} else {
nodes[type] = Lista<Node>(value: node)
}

count += 1
if count > nodeBlockMax {
count = 0
processor.add(chunk: .init(nodes: nodes, server: server, parentType: parentType, moreComing: true))
nodes.removeAll(keepingCapacity: true)
}
}

do {
let queries = Query.batching("\(serverName): \(name)", groupName: "nodes", idList: ids, maxCost: maxCost, perNode: nodeBlock, fields: fields)
try await server.run(queries: queries)
processor.add(chunk: .init(nodes: nodes, server: server, parentType: parentType, moreComing: false))
await processor.waitForCompletion()
await scanNodes(nodes, from: server, parentType: parentType)
} catch {
processor.add(chunk: .init(nodes: [:], server: server, parentType: nil, moreComing: false))
await processor.waitForCompletion()
server.lastSyncSucceeded = false
throw error
}
Expand Down Expand Up @@ -638,29 +624,19 @@ enum GraphQL {
}

static func fetchAllAuthoredItems(from server: ApiServer, label: String, @ElementsBuilder fields: () -> [any Element]) async -> [String: Lista<Node>]? {
var count = 0
var nodes = [String: Lista<Node>]()
let group = Group("viewer", fields: fields)
let processor = Processor()
let authoredItemsQuery = Query(name: "Authored \(label)", rootElement: group) { node in
let type = node.elementType
if let existingList = nodes[type] {
existingList.append(node)
} else {
nodes[type] = Lista<Node>(value: node)
}

count += 1
if count > nodeBlockMax {
count = 0
processor.add(chunk: .init(nodes: nodes, server: server, parentType: nil, moreComing: true))
nodes.removeAll(keepingCapacity: true)
}
}
do {
try await server.run(queries: Lista(value: authoredItemsQuery))
processor.add(chunk: .init(nodes: nodes, server: server, parentType: nil, moreComing: false))
await processor.waitForCompletion()
await scanNodes(nodes, from: server, parentType: nil)
return nodes

} catch {
Expand Down Expand Up @@ -691,9 +667,7 @@ enum GraphQL {
}
do {
try await server.run(queries: Lista(value: query))
let processor = Processor()
processor.add(chunk: .init(nodes: ["PullRequest": nodes], server: server, parentType: nil, moreComing: false))
await processor.waitForCompletion()
await scanNodes(["PullRequest": nodes], from: server, parentType: nil)
} catch {
server.lastSyncSucceeded = false
}
Expand Down Expand Up @@ -751,10 +725,7 @@ enum GraphQL {
}
}

let processor = Processor()

for (server, reposInThisServer) in reposByServer {
var count = 0
var nodes = [String: Lista<Node>]()

let perNodeBlock: Query.PerNodeBlock = { node in
Expand All @@ -773,13 +744,6 @@ enum GraphQL {
d < prRepoIdToLatestExistingUpdate[repo.id]! {
throw TQL.Error.alreadyParsed
}

count += 1
if count > nodeBlockMax {
count = 0
processor.add(chunk: .init(nodes: nodes, server: server, parentType: nil, moreComing: true))
nodes.removeAll(keepingCapacity: true)
}
}

let queriesForServer = Lista<Query>()
Expand Down Expand Up @@ -809,8 +773,7 @@ enum GraphQL {

do {
try await server.run(queries: queriesForServer)
processor.add(chunk: .init(nodes: nodes, server: server, parentType: nil, moreComing: false))
await processor.waitForCompletion()
await scanNodes(nodes, from: server, parentType: nil)
} catch {
server.lastSyncSucceeded = false
}
Expand All @@ -831,10 +794,7 @@ enum GraphQL {
}
}

let processor = Processor()

for (server, reposInThisServer) in reposByServer {
var count = 0
var nodes = [String: Lista<Node>]()

let perNodeBlock: Query.PerNodeBlock = { node in
Expand All @@ -853,13 +813,6 @@ enum GraphQL {
d < issueRepoIdToLatestExistingUpdate[repo.id]! {
throw TQL.Error.alreadyParsed
}

count += 1
if count > nodeBlockMax {
count = 0
processor.add(chunk: .init(nodes: nodes, server: server, parentType: nil, moreComing: true))
nodes.removeAll(keepingCapacity: true)
}
}

let queriesForServer = Lista<Query>()
Expand Down Expand Up @@ -889,11 +842,59 @@ enum GraphQL {

do {
try await server.run(queries: queriesForServer)
processor.add(chunk: .init(nodes: nodes, server: server, parentType: nil, moreComing: false))
await processor.waitForCompletion()
await scanNodes(nodes, from: server, parentType: nil)
} catch {
server.lastSyncSucceeded = false
}
}
}

private static func scanNodes(_ nodes: [String: Lista<Node>], from server: ApiServer, parentType: (some DataItem).Type?) async {
guard nodes.count > 0, let moc = server.managedObjectContext else { return }
await DataManager.runInChild(of: moc) { child in
guard let server = try? child.existingObject(with: server.objectID) as? ApiServer else {
return
}

let parentCache = FetchCache()
// Order must be fixed, since labels may refer to PRs or Issues, ensure they are created first

if let nodeList = nodes["Repository"] {
Repo.sync(from: nodeList, on: server, moc: child, parentCache: parentCache)
}
if let nodeList = nodes["Issue"] {
Issue.sync(from: nodeList, on: server, moc: child, parentCache: parentCache)
}
if let nodeList = nodes["PullRequest"] {
PullRequest.sync(from: nodeList, on: server, moc: child, parentCache: parentCache)
}
if let nodeList = nodes["Label"] {
PRLabel.sync(from: nodeList, on: server, moc: child, parentCache: parentCache)
}
if let nodeList = nodes["CommentReaction"] {
Reaction.sync(from: nodeList, for: PRComment.self, on: server, moc: child, parentCache: parentCache)
}
if let nodeList = nodes["IssueComment"] {
PRComment.sync(from: nodeList, on: server, moc: child, parentCache: parentCache)
}
if let nodeList = nodes["PullRequestReviewComment"] {
PRComment.sync(from: nodeList, on: server, moc: child, parentCache: parentCache)
}
if let nodeList = nodes["Reaction"], let parentType = parentType {
Reaction.sync(from: nodeList, for: parentType, on: server, moc: child, parentCache: parentCache)
}
if let nodeList = nodes["ReviewRequest"] {
Review.syncRequests(from: nodeList, moc: child, parentCache: parentCache)
}
if let nodeList = nodes["PullRequestReview"] {
Review.sync(from: nodeList, on: server, moc: child, parentCache: parentCache)
}
if let nodeList = nodes["StatusContext"] {
PRStatus.sync(from: nodeList, on: server, moc: child, parentCache: parentCache)
}
if let nodeList = nodes["CheckRun"] {
PRStatus.sync(from: nodeList, on: server, moc: child, parentCache: parentCache)
}
}
}
}
6 changes: 0 additions & 6 deletions Trailer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@
16A0FA7423AECFAE0023B565 /* BackgroundTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16A0FA7323AECFAE0023B565 /* BackgroundTask.swift */; };
16A1E44129F2FBB6007684D0 /* AsyncAlgorithms in Frameworks */ = {isa = PBXBuildFile; productRef = 16A1E44029F2FBB6007684D0 /* AsyncAlgorithms */; };
16A1E44329F2FBCA007684D0 /* AsyncAlgorithms in Frameworks */ = {isa = PBXBuildFile; productRef = 16A1E44229F2FBCA007684D0 /* AsyncAlgorithms */; };
16A1E44529F31B9D007684D0 /* GraphQLProcessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16A1E44429F31B9D007684D0 /* GraphQLProcessor.swift */; };
16A1E44629F31B9D007684D0 /* GraphQLProcessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16A1E44429F31B9D007684D0 /* GraphQLProcessor.swift */; };
16A41E9E22A71C7A00D1D352 /* TextFieldCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16A41E9D22A71C7A00D1D352 /* TextFieldCell.swift */; };
16A69EDA1AC83020008231F9 /* ListableItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16A69ED91AC83020008231F9 /* ListableItem.swift */; };
16A69EDB1AC83020008231F9 /* ListableItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16A69ED91AC83020008231F9 /* ListableItem.swift */; };
Expand Down Expand Up @@ -381,7 +379,6 @@
169FB2351A3DBD7D00E00771 /* SectionHeader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SectionHeader.swift; sourceTree = "<group>"; };
16A0B0361A48AC7900B3452D /* MacAppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = MacAppDelegate.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
16A0FA7323AECFAE0023B565 /* BackgroundTask.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BackgroundTask.swift; sourceTree = "<group>"; };
16A1E44429F31B9D007684D0 /* GraphQLProcessor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GraphQLProcessor.swift; sourceTree = "<group>"; };
16A41E9D22A71C7A00D1D352 /* TextFieldCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextFieldCell.swift; sourceTree = "<group>"; };
16A69ED91AC83020008231F9 /* ListableItem.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; name = ListableItem.swift; path = ../Shared/ListableItem.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
16AE7CED1C92304300CB304B /* Trailer 24.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = "Trailer 24.xcdatamodel"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -526,7 +523,6 @@
children = (
1657230023AAA23500F597EF /* V4API.swift */,
160CA02B23A65152003FA0D8 /* GraphQL.swift */,
16A1E44429F31B9D007684D0 /* GraphQLProcessor.swift */,
);
name = V4API;
sourceTree = "<group>";
Expand Down Expand Up @@ -1278,7 +1274,6 @@
164840FC1AD196D6009E6383 /* SettingsManager.swift in Sources */,
1657230923AAA3B000F597EF /* RestAccess.swift in Sources */,
1633C1931ABCEC4A0086C692 /* Issue.swift in Sources */,
16A1E44629F31B9D007684D0 /* GraphQLProcessor.swift in Sources */,
16F413701BA5850A006B0195 /* RepoCell.swift in Sources */,
168839C71DEF8C3B002D9448 /* ImportExport.swift in Sources */,
1606F1BD1A471CDC0088368E /* RespositoriesViewController.swift in Sources */,
Expand Down Expand Up @@ -1358,7 +1353,6 @@
169B92961CC52C05002774A8 /* SnoozePreset.swift in Sources */,
160FB1401AE3F78E00EB5C8E /* PreferencesWindow.swift in Sources */,
16A0B0371A48AC7900B3452D /* MacAppDelegate.swift in Sources */,
16A1E44529F31B9D007684D0 /* GraphQLProcessor.swift in Sources */,
16393A9B283E99E000DDD546 /* StatusItemView.swift in Sources */,
161B851C1AB8E12C00AD4C5D /* Team.swift in Sources */,
16B3B31D1A34E8E2000BE6CC /* PRLabel.swift in Sources */,
Expand Down

0 comments on commit 2468e30

Please sign in to comment.