Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bake): lightning deploys populate gdoc metadata #2907

Merged
merged 7 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions adminSiteClient/GdocsDiff.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { omit, OwidGdocInterface } from "@ourworldindata/utils"
// Errors are already shown in the settings drawer, so we don't show those either
export const GDOC_DIFF_OMITTABLE_PROPERTIES = [
"errors",
// imageMetadata *does* count as something that meaningfully contributes to whether a Gdoc is different,
// And it's checked in the checkIsLightningUpdate function,
// but for the preview, it's already captured by changes to the body text, so we don't need to show these objects in the diff also
"imageMetadata",
"linkedCharts",
"linkedDocuments",
Expand Down
2 changes: 1 addition & 1 deletion adminSiteClient/gdocsDeploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export const checkIsLightningUpdate = (
> = {
breadcrumbs: true,
errors: true,
imageMetadata: true,
linkedCharts: true,
linkedDocuments: true,
relatedCharts: true,
Expand All @@ -47,6 +46,7 @@ export const checkIsLightningUpdate = (
updatedAt: true,
createdAt: false,
id: false, // weird case - can't be updated
imageMetadata: false, // could require baking new images
publicationContext: false, // requires an update of the blog roll
published: false, // requires an update of the blog roll
publishedAt: false, // could require an update of the blog roll
Expand Down
4 changes: 4 additions & 0 deletions adminSiteServer/apiRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2502,6 +2502,10 @@ apiRouter.put("/gdocs/:id", async (req, res) => {
.getRepository(Gdoc)
.create(getOwidGdocFromJSON(nextGdocJSON))

// Need to load these to compare them for lightning update candidacy
await prevGdoc.loadImageMetadata()
await nextGdoc.loadImageMetadata()

// Deleting and recreating these is simpler than tracking orphans over the next code block
await GdocXImage.delete({ gdocId: id })
const filenames = nextGdoc.filenames
Expand Down
14 changes: 5 additions & 9 deletions baker/DeployUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {
BAKED_BASE_URL,
BUILDKITE_API_ACCESS_TOKEN,
} from "../settings/serverSettings.js"
import { DeployChange, OwidGdocPublished } from "@ourworldindata/utils"
import { Gdoc } from "../db/model/Gdoc/Gdoc.js"
import { DeployChange } from "@ourworldindata/utils"
import { SiteBaker } from "../baker/SiteBaker.js"

const deployQueueServer = new DeployQueueServer()
Expand Down Expand Up @@ -56,13 +55,10 @@ const triggerBakeAndDeploy = async (
// otherwise, bake locally. This is used for local development or staging servers
const baker = new SiteBaker(BAKED_SITE_DIR, BAKED_BASE_URL)
if (lightningQueue?.length) {
for (const change of lightningQueue) {
const gdoc = (await Gdoc.findOneByOrFail({
published: true,
slug: change.slug,
})) as OwidGdocPublished
await baker.bakeGDocPost(gdoc)
}
if (!lightningQueue.every((change) => change.slug))
throw new Error("Lightning deploy is missing a slug")

await baker.bakeGDocPosts(lightningQueue.map((c) => c.slug!))
} else {
await baker.bakeAll()
}
Expand Down
23 changes: 19 additions & 4 deletions baker/SiteBaker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export class SiteBaker {
}

// Bake an individual post/page
async bakeGDocPost(post: OwidGdocPublished) {
private async bakeGDocPost(post: OwidGdocPublished) {
const html = renderGdoc(post)
const outPath = path.join(this.bakedSiteDir, `${post.slug}.html`)
await fs.mkdirp(path.dirname(outPath))
Expand Down Expand Up @@ -307,12 +307,27 @@ export class SiteBaker {
this.progressBar.tick({ name: "✅ baked posts" })
}

// Bake all GDoc posts
async bakeGDocPosts() {
// Bake all GDoc posts, or a subset of them if slugs are provided
async bakeGDocPosts(slugs?: string[]) {
await db.getConnection()
if (!this.bakeSteps.has("gdocPosts")) return
const publishedGdocs = await Gdoc.getPublishedGdocs()

const gdocsToBake =
slugs !== undefined
? publishedGdocs.filter((gdoc) => slugs.includes(gdoc.slug))
: publishedGdocs

// Ensure we have a published gdoc for each slug given
if (slugs !== undefined && slugs.length !== gdocsToBake.length) {
const slugsNotFound = slugs.filter(
(slug) => !gdocsToBake.find((gdoc) => gdoc.slug === slug)
)
throw new Error(
`Some of the gdoc slugs were not found or are not published: ${slugsNotFound}`
)
}

// Prefetch publishedGdocs, imageMetadata, and linkedCharts instead of each instance fetching
const publishedGdocsDictionary = keyBy(publishedGdocs.map(clone), "id")
const imageMetadataDictionary = await Image.find().then((images) =>
Expand Down Expand Up @@ -349,7 +364,7 @@ export class SiteBaker {
)
)

for (const publishedGdoc of publishedGdocs) {
for (const publishedGdoc of gdocsToBake) {
// Pick the necessary metadata from the dictionaries we prefetched
publishedGdoc.linkedDocuments = pick(
publishedGdocsDictionary,
Expand Down
8 changes: 1 addition & 7 deletions baker/bakeGdocPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import yargs from "yargs"
import { hideBin } from "yargs/helpers"
import { SiteBaker } from "./SiteBaker.js"
import { BAKED_SITE_DIR, BAKED_BASE_URL } from "../settings/serverSettings.js"
import { Gdoc } from "../db/model/Gdoc/Gdoc.js"
import { OwidGdocPublished } from "@ourworldindata/utils"
import * as db from "../db/db.js"

yargs(hideBin(process.argv))
Expand All @@ -22,11 +20,7 @@ yargs(hideBin(process.argv))
const baker = new SiteBaker(BAKED_SITE_DIR, BAKED_BASE_URL)

await db.getConnection()
const gdoc = (await Gdoc.findOneByOrFail({
published: true,
slug: slug,
})) as OwidGdocPublished
await baker.bakeGDocPost(gdoc)
await baker.bakeGDocPosts([slug])
process.exit(0)
}
)
Expand Down
12 changes: 1 addition & 11 deletions baker/bakeGdocPosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import yargs from "yargs"
import { hideBin } from "yargs/helpers"
import { SiteBaker } from "./SiteBaker.js"
import { BAKED_SITE_DIR, BAKED_BASE_URL } from "../settings/serverSettings.js"
import { Gdoc } from "../db/model/Gdoc/Gdoc.js"
import { OwidGdocPublished } from "@ourworldindata/utils"
import * as db from "../db/db.js"

yargs(hideBin(process.argv))
Expand All @@ -27,15 +25,7 @@ yargs(hideBin(process.argv))
const baker = new SiteBaker(BAKED_SITE_DIR, BAKED_BASE_URL)

await db.getConnection()
await Promise.all(
slugs.map(async (slug) => {
const gdoc = (await Gdoc.findOneByOrFail({
published: true,
slug: slug,
})) as OwidGdocPublished
await baker.bakeGDocPost(gdoc)
})
)
await baker.bakeGDocPosts(slugs)
process.exit(0)
}
)
Expand Down
20 changes: 8 additions & 12 deletions ops/buildkite/deploy-content
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,16 @@ deploy_content () {
exit 1
fi

# Lightning deploys ARE CURRENTLY DISABLED because they're screwing up the site
# NOTE: in theory we could run lightning bake in parallel to regular bake and only lock `deploy_to_cloudflare`
# right now lightning bake has to wait if there's a regular bake
# if [ -n "${LIGHTNING_GDOC_SLUGS:-}" ]; then
# # TODO: do we need to trigger `yarn buildLocalBake --steps gdriveImages` to get up to date images for the lightning post?
# bake_gdoc_posts "$LIGHTNING_GDOC_SLUGS"
# else

update_owid_content_repo
sync_wordpress_uploads
bake_site
sync_baked_data_to_r2

# fi
if [ -n "${LIGHTNING_GDOC_SLUGS:-}" ]; then
bake_gdoc_posts "$LIGHTNING_GDOC_SLUGS"
else
update_owid_content_repo
sync_wordpress_uploads
bake_site
sync_baked_data_to_r2
fi

create_dist
deploy_to_cloudflare
Expand Down
Loading