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

feat(gatsby): Store site metadata #26162

Merged
merged 2 commits into from
Aug 5, 2020
Merged

feat(gatsby): Store site metadata #26162

merged 2 commits into from
Aug 5, 2020

Conversation

ascorbic
Copy link
Contributor

This creates a metadata file in the config directory, which allows reverse lookup to find the site's path. This is so that Desktop can iterate the directory to discover all sites on the user's drive.

@ascorbic ascorbic requested a review from mxstbr July 31, 2020 10:08
@ascorbic ascorbic requested a review from a team as a code owner July 31, 2020 10:08
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Jul 31, 2020
@ascorbic ascorbic added status: needs core review Currently awaiting review from Core team member and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Jul 31, 2020
@gatsby-cloud
Copy link

gatsby-cloud bot commented Jul 31, 2020

Gatsby Cloud Build Report

using-styled-components

🎉 Your build was successful! See the Deploy preview here.

Build Details

View the build logs here.

🕐 Build time: 19s

Performance

Lighthouse report

Metric Score
Performance 💚 100
Accessibility 💚 90
Best Practices 💚 100
SEO 💚 90

🔗 View full report

@gatsby-cloud
Copy link

gatsby-cloud bot commented Jul 31, 2020

Gatsby Cloud Build Report

client-only-paths

🎉 Your build was successful! See the Deploy preview here.

Build Details

View the build logs here.

🕐 Build time: 18s

Performance

Lighthouse report

Metric Score
Performance 💚 100
Accessibility 🔶 85
Best Practices 💚 100
SEO 🔶 70

🔗 View full report

@gatsby-cloud-staging
Copy link

gatsby-cloud-staging bot commented Jul 31, 2020

Gatsby Cloud Build Report

gatsby-master

🎉 Your build was successful! See the Deploy preview here.

Build Details

View the build logs here.

🕐 Build time: 1m

Performance

Lighthouse report

Metric Score
Performance 💚 97
Accessibility 🔶 87
Best Practices 💚 93
SEO 🔶 73

🔗 View full report

@gatsby-cloud
Copy link

gatsby-cloud bot commented Jul 31, 2020

Gatsby Cloud Build Report

using-reach-skip-nav

🎉 Your build was successful! See the Deploy preview here.

Build Details

View the build logs here.

🕐 Build time: 21s

Performance

Lighthouse report

Metric Score
Performance 💚 100
Accessibility 💚 100
Best Practices 💚 100
SEO 🔶 82

🔗 View full report

@gatsby-cloud-staging
Copy link

gatsby-cloud-staging bot commented Jul 31, 2020

Gatsby Cloud Build Report

gatsby

🎉 Your build was successful! See the Deploy preview here.

Build Details

View the build logs here.

🕐 Build time: 22m

Performance

Lighthouse report

Metric Score
Performance 💚 93
Accessibility 💚 100
Best Practices 💚 100
SEO 🔶 76

🔗 View full report

@gatsby-cloud
Copy link

gatsby-cloud bot commented Jul 31, 2020

Gatsby Cloud Build Report

gatsby

🎉 Your build was successful! See the Deploy preview here.

Build Details

View the build logs here.

🕐 Build time: 19m

serviceName: string
): Promise<string | null> => {
serviceName: string,
ignoreLockfile: boolean = false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't look like the new parameter is used anywhere? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look at line 81

Copy link
Contributor

@mxstbr mxstbr Jul 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant from the outside 😅 the getService call below doesn't seem to leverage it:

const data = await getService(program.directory, `developproxy`)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's for me in Desktop! I want to get the data without obtaining a lock

@KyleAMathews
Copy link
Contributor

Can we register the site when it gets created as well as part of gatsby new?

@mxstbr
Copy link
Contributor

mxstbr commented Jul 31, 2020

I reckon we could register in gatsby-cli no matter what command is run, right?

@ascorbic
Copy link
Contributor Author

ascorbic commented Aug 3, 2020

I've added support for gatsby new. I don't think it makes sense to add it whatever command is run, because quite a lot of commands don't require to be run in the context of a local gatsby site. It might make sense to add it for build.

@ascorbic ascorbic requested a review from mxstbr August 3, 2020 13:53
@KyleAMathews
Copy link
Contributor

build makes sense. In my poking around in telemetry, there's definitely people who just clone a site and try building it — e.g. a site a colleague asks them to look at.

@ascorbic
Copy link
Contributor Author

ascorbic commented Aug 3, 2020

@KyleAMathews @mxstbr OK, it now registers on new, develop and build.

mxstbr
mxstbr previously approved these changes Aug 4, 2020
Copy link
Contributor

@mxstbr mxstbr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM, one clarification comment inline but that shouldn't be blocking! 👍

await createServiceLock(program.directory, `metadata`, {
name: program.sitePackageJson.name,
sitePath: program.directory,
lastRun: Date.now(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why this doesn't include pid, but the develop one does? I'm assuming this doesn't store the pid since it's not a long-lived process, but just wanted to clarify!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, exactly. I only include the pid in case I need to kill the process from Desktop.

Copy link
Contributor

@wardpeet wardpeet Aug 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't harm us to keep it consistent and always add a pid, right? Do we also want to keep track of which method it is? (build/develop)

Does it make sense to add a typescript type for it?

Moving it to core-utils would solve this as well.

Copy link
Contributor Author

@ascorbic ascorbic Aug 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wardpeet We can't, because the calling process might not always be the actual build process. We use it in Desktop to write the metadata for sites that we have manually added.

Copy link
Contributor

@sidharthachatterjee sidharthachatterjee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a blanket cleanup necessary for cleaning up these locks if someone sends a SIGINT at a certain time that results in stale lock files lying around?

@ascorbic
Copy link
Contributor Author

ascorbic commented Aug 4, 2020

@sidharthachatterjee Apparently the lockfile library handles that itself.

@sidharthachatterjee
Copy link
Contributor

@ascorbic Okay, that’s great then.

@ascorbic
Copy link
Contributor Author

ascorbic commented Aug 4, 2020

@sidharthachatterjee Added the log

mxstbr
mxstbr previously approved these changes Aug 4, 2020
@ascorbic ascorbic added the bot: merge on green Gatsbot will merge these PRs automatically when all tests passes label Aug 4, 2020
Store pid

Allow lockfile to be ignored for services that don't need to be running

Get correct port for running service

Add last run time

init site info in gatsby new

Store metadata on build

Make getService generic so we can avoid casting

Log exception
Copy link
Contributor

@wardpeet wardpeet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added some small cleanup tasks.

I only see you writing to the metadata file. Does gatsby desktop removes things from the metadata file? Mostly concerned about keeping pids inside the metadata when the process stopped.

@@ -9,10 +9,11 @@ import isValid from "is-valid-path"
import sysPath from "path"
import prompts from "prompts"
import url from "url"

import { createServiceLock } from "gatsby-core-utils/dist/service-lock"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { createServiceLock } from "gatsby-core-utils/dist/service-lock"
// TODO make service-lock export from gatsby-core-utils
import { createServiceLock } from "gatsby-core-utils/dist/service-lock"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks. I'll create a ticket for me to fix that

Comment on lines +351 to +365
const sitePath = sysPath.resolve(rootPath)

const sitePackageJson = await fs
.readJSON(sysPath.join(sitePath, `package.json`))
.catch(() => {
reporter.verbose(
`Could not read "${sysPath.join(sitePath, `package.json`)}"`
)
})

await createServiceLock(sitePath, `metadata`, {
name: sitePackageJson?.name || rootPath,
sitePath,
lastRun: Date.now(),
}).then(unlock => unlock?.())
Copy link
Contributor

@wardpeet wardpeet Aug 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we move this to a function so it's more clear what this does and why we need it? Maybe worth to move it into core-utils?

/**
 * the metadata file is used in gatsby desktop to discover which projects are created for gatsby
 */
function saveProjectToMetadataFile() {}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, though it would still need most of the same props. e.g. it won't always want to save the pid or update the lastRun (e.g. if it's being called from Desktop)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those values could be nullable/undefined maybe?

@@ -36,6 +36,7 @@ import {
markWebpackStatusAsPending,
markWebpackStatusAsDone,
} from "../utils/webpack-status"
import { createServiceLock } from "gatsby-core-utils/dist/service-lock"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { createServiceLock } from "gatsby-core-utils/dist/service-lock"
// TODO make service-lock export from gatsby-core-utils
import { createServiceLock } from "gatsby-core-utils/dist/service-lock"

await createServiceLock(program.directory, `metadata`, {
name: program.sitePackageJson.name,
sitePath: program.directory,
lastRun: Date.now(),
Copy link
Contributor

@wardpeet wardpeet Aug 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't harm us to keep it consistent and always add a pid, right? Do we also want to keep track of which method it is? (build/develop)

Does it make sense to add a typescript type for it?

Moving it to core-utils would solve this as well.

console.error(
`Looks like develop for this site is already running. Try visiting http://localhost:8000/ maybe?`
`Looks like develop for this site is already running. Try visiting http://localhost:${port}/ maybe?`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment on lines +12 to +15
import {
createServiceLock,
getService,
} from "gatsby-core-utils/dist/service-lock"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import {
createServiceLock,
getService,
} from "gatsby-core-utils/dist/service-lock"
// TODO make service-lock export from gatsby-core-utils
import {
createServiceLock,
getService,
} from "gatsby-core-utils/dist/service-lock"

Copy link
Contributor

@wardpeet wardpeet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'll approve, would be nice if you could look into moving things to core-utils in another PR

@gatsbybot gatsbybot merged commit 36367c4 into master Aug 5, 2020
@delete-merged-branch delete-merged-branch bot deleted the feat/site-metadata branch August 5, 2020 09:57
@ascorbic
Copy link
Contributor Author

ascorbic commented Aug 5, 2020

I just tried to push a commit that would do just that. Oops!

@ascorbic ascorbic restored the feat/site-metadata branch August 5, 2020 10:28
@ascorbic
Copy link
Contributor Author

ascorbic commented Aug 5, 2020

@wardpeet Here's the followup PR: #26237

blainekasten pushed a commit that referenced this pull request Aug 6, 2020
Store pid

Allow lockfile to be ignored for services that don't need to be running

Get correct port for running service

Add last run time

init site info in gatsby new

Store metadata on build

Make getService generic so we can avoid casting

Log exception

Co-authored-by: gatsbybot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot: merge on green Gatsbot will merge these PRs automatically when all tests passes status: needs core review Currently awaiting review from Core team member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants