Skip to content

Commit

Permalink
Fix rebuild
Browse files Browse the repository at this point in the history
  • Loading branch information
livid committed Feb 7, 2024
1 parent 813a7fe commit 66c454c
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 22 deletions.
8 changes: 6 additions & 2 deletions Planet/Entities/MyArticleModel+Save.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ extension MyArticleModel {
// MARK: - Save to Public/:planet_id/:article_id/index.html

/// Save the article into UUID/index.html along with its attachments.
func savePublic() throws {
func savePublic(usingTasks: Bool = false) throws {
let started: Date = Date()
var marks: OrderedDictionary<String, Date> = ["Started": started]

Expand Down Expand Up @@ -79,7 +79,7 @@ extension MyArticleModel {

// MARK: - Render Markdown

try processArticleHTML()
try processArticleHTML(usingTasks: usingTasks)
marks.recordEvent("ArticleHTML", for: self.title)

// MARK: - Hero Grid
Expand Down Expand Up @@ -317,6 +317,10 @@ extension MyArticleModel {
debugPrint("HeroImage: candidate size: \(image.size)")
if image.size.width >= 600 && image.size.height >= 400 {
debugPrint("HeroImage: \(item)")
DispatchQueue.main.async {
self.heroImage = firstImage
try? self.save()
}
return item
}
}
Expand Down
20 changes: 16 additions & 4 deletions Planet/Entities/MyArticleModel+SavePublic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,18 +224,30 @@ extension MyArticleModel {
}

/// Render article HTML
func processArticleHTML() throws {
func processArticleHTML(usingTasks: Bool = false) throws {
guard let template = planet.template else {
throw PlanetError.MissingTemplateError
}

Task(priority: .userInitiated) {
if (usingTasks) {
Task(priority: .userInitiated) {
let articleHTML = try template.render(article: self)
try articleHTML.data(using: .utf8)?.write(to: publicIndexPath)
debugPrint("HTML for \(self.title) saved to \(publicIndexPath.path)")
}

Task(priority: .userInitiated) {
if template.hasSimpleHTML {
let simpleHTML = try template.render(article: self, forSimpleHTML: true)
try simpleHTML.data(using: .utf8)?.write(to: publicSimplePath)
debugPrint("Simple HTML for \(self.title) saved to \(publicSimplePath.path)")
}
}
} else {
let articleHTML = try template.render(article: self)
try articleHTML.data(using: .utf8)?.write(to: publicIndexPath)
debugPrint("HTML for \(self.title) saved to \(publicIndexPath.path)")
}

Task(priority: .userInitiated) {
if template.hasSimpleHTML {
let simpleHTML = try template.render(article: self, forSimpleHTML: true)
try simpleHTML.data(using: .utf8)?.write(to: publicSimplePath)
Expand Down
51 changes: 36 additions & 15 deletions Planet/Entities/MyPlanetModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,15 @@ class MyPlanetModel: Equatable, Hashable, Identifiable, ObservableObject, Codabl
let hasPodcastCoverArt = FileManager.default.fileExists(
atPath: publicPodcastCoverArtPath.path
)

// MARK: - Render RSS and podcast RSS
renderRSS(podcastOnly: false)

if publicPlanet.hasAudioContent() {
renderRSS(podcastOnly: true)
}
reduceRebuildTasks()

// MARK: - Render index.html and pages
let itemsPerPage = template.idealItemsPerPage ?? 10
let generateIndexPagination = template.generateIndexPagination ?? false
Expand Down Expand Up @@ -1458,6 +1467,7 @@ class MyPlanetModel: Equatable, Hashable, Identifiable, ObservableObject, Codabl
let indexHTML = try template.renderIndex(context: pageContext)
try indexHTML.data(using: .utf8)?.write(to: publicIndexPath)
}
reduceRebuildTasks()

// MARK: - Render tags
if let generateTagPages = template.generateTagPages, generateTagPages {
Expand Down Expand Up @@ -1520,6 +1530,7 @@ class MyPlanetModel: Equatable, Hashable, Identifiable, ObservableObject, Codabl
else {
debugPrint("Skip generating tags for planet \(name)")
}
reduceRebuildTasks()

// MARK: - Render archive.html
if let generateArchive = template.generateArchive, generateArchive {
Expand Down Expand Up @@ -1556,13 +1567,7 @@ class MyPlanetModel: Equatable, Hashable, Identifiable, ObservableObject, Codabl
else {
debugPrint("Skip generating archive for planet \(name)")
}

// MARK: - Render RSS and podcast RSS
renderRSS(podcastOnly: false)

if publicPlanet.hasAudioContent() {
renderRSS(podcastOnly: true)
}
reduceRebuildTasks()

// MARK: - Save planet.json
let info = try JSONEncoder.shared.encode(publicPlanet)
Expand Down Expand Up @@ -1890,7 +1895,7 @@ class MyPlanetModel: Equatable, Hashable, Identifiable, ObservableObject, Codabl

// heaviest task is generating thumbnails

// try self.articles.forEach { try $0.savePublic() }
// try self.articles.forEach { try $0.savePublic(usingTasks: true) }

do {
// split the articles into groups
Expand All @@ -1904,7 +1909,7 @@ class MyPlanetModel: Equatable, Hashable, Identifiable, ObservableObject, Codabl
DispatchQueue.concurrentPerform(iterations: articleGroup.count) { index in
group.enter()
do {
try articleGroup[index].savePublic()
try articleGroup[index].savePublic(usingTasks: true)
group.leave()
}
catch {
Expand All @@ -1919,7 +1924,7 @@ class MyPlanetModel: Equatable, Hashable, Identifiable, ObservableObject, Codabl
try await withThrowingTaskGroup(of: Void.self) { group in
for article in articleGroup {
group.addTask(priority: .high) {
try article.savePublic()
try article.savePublic(usingTasks: true)
}
}
try await group.waitForAll()
Expand Down Expand Up @@ -1956,6 +1961,25 @@ class MyPlanetModel: Equatable, Hashable, Identifiable, ObservableObject, Codabl
await sendNotificationForRebuild()
}

func quickRebuildTaskCount() -> Int {
let copyTemplateAssetsTask = 1
let pagesTask = 1
let tagsTask = 1
let archiveTask = 1
let rssTask = 1
return copyTemplateAssetsTask + pagesTask + tagsTask + archiveTask + rssTask
}

func reduceRebuildTasks() {
Task { @MainActor in
let before = PlanetStore.shared.rebuildTasks
PlanetStore.shared.rebuildTasks -= 1
let after = PlanetStore.shared.rebuildTasks
debugPrint("Rebuild tasks reduced from \(before) to \(after) at \(Date())")
NotificationCenter.default.post(name: .myArticleBuilt, object:nil)
}
}

func quickRebuild() async throws {
let started = Date()
await MainActor.run {
Expand All @@ -1969,14 +1993,11 @@ class MyPlanetModel: Equatable, Hashable, Identifiable, ObservableObject, Codabl
}
Task { @MainActor in
PlanetStore.shared.isRebuilding = true
PlanetStore.shared.rebuildTasks = 2
PlanetStore.shared.rebuildTasks = quickRebuildTaskCount()
PlanetStatusManager.shared.updateStatus()
}
try self.copyTemplateAssets()
Task { @MainActor in
PlanetStore.shared.rebuildTasks = 1
NotificationCenter.default.post(name: .myArticleBuilt, object:nil)
}
reduceRebuildTasks()
try await self.savePublic()
await MainActor.run {
self.isRebuilding = false
Expand Down
22 changes: 22 additions & 0 deletions Planet/Views/Components/GIFIndicatorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,25 @@ struct GIFIndicatorView: View {
}
}
}


struct VideoIndicatorView: View {
var body: some View {
VStack {
Spacer()
HStack {
Text("MP4")
.font(.system(size: 10, weight: .semibold, design: .monospaced))
.foregroundColor(.white.opacity(0.85))
.padding(.horizontal, 4)
.padding(.vertical, 2)
.background(Color.secondary.opacity(0.75))
.cornerRadius(4)
.multilineTextAlignment(.center)
Spacer()
}
.padding(.leading, 4)
.padding(.bottom, 4)
}
}
}
5 changes: 5 additions & 0 deletions Planet/Views/My/MyArticleGridView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ struct MyArticleGridView: View {
.shadow(color: Color.black.opacity(0.1), radius: 2, x: 0, y: 1)
if article.hasGIF {
GIFIndicatorView()
}
else if article.hasVideo {
VideoIndicatorView()
} else {

}
if article.attachmentURLs.count > 1 {
GroupIndicatorView()
Expand Down
2 changes: 1 addition & 1 deletion Planet/versioning.xcconfig
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CURRENT_PROJECT_VERSION = 1858
CURRENT_PROJECT_VERSION = 1859

0 comments on commit 66c454c

Please sign in to comment.