Skip to content

Commit f9b23c2

Browse files
committed
fix(server): usability improvements to PSI cron
1 parent 67b6af5 commit f9b23c2

File tree

5 files changed

+33
-11
lines changed

5 files changed

+33
-11
lines changed

packages/cli/src/collect/collect.js

-4
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ function buildCommand(yargs) {
4242
psiApiKey: {
4343
description: '[psi only] The API key to use for PageSpeed Insights runner method.',
4444
},
45-
psiApiEndpoint: {
46-
description:
47-
"[psi only] The endpoint to use for PageSpeed Insights runner method. You do not need to use this unless you've written a custom version.",
48-
},
4945
staticDistDir: {
5046
description: 'The build directory where your HTML files to run Lighthouse on are located.',
5147
},

packages/server/src/cron/psi-collect.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function normalizeCronSchedule(schedule) {
4141
* @return {Promise<void>}
4242
*/
4343
async function psiCollectForProject(storageMethod, psi, site) {
44-
const {urls, projectSlug, numberOfRuns = 3} = site;
44+
const {urls, projectSlug, numberOfRuns = 5} = site;
4545
const project = await storageMethod.findProjectBySlug(projectSlug);
4646
if (!project) throw new Error(`Invalid project slug "${projectSlug}"`);
4747
if (!urls || !urls.length) throw new Error('No URLs set');
@@ -96,6 +96,13 @@ async function psiCollectForProject(storageMethod, psi, site) {
9696
*/
9797
function startPsiCollectCron(storageMethod, options) {
9898
if (!options.psiCollectCron) return;
99+
const uniqueProjectBranches = new Set(
100+
options.psiCollectCron.sites.map(site => `${site.projectSlug}@${site.branch}`)
101+
);
102+
103+
if (uniqueProjectBranches.size < options.psiCollectCron.sites.length) {
104+
throw new Error('Cannot configure more than one cron per project-branch pair');
105+
}
99106

100107
/** @type {(msg: string) => void} */
101108
const log =

packages/server/test/cron/psi-collect.test.js

+19-5
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ describe('cron/psi-collect', () => {
7676
[{projectId: 1, buildId: 2, url: 'http://example.com', lhr: '{"lhr": true}'}],
7777
[{projectId: 1, buildId: 2, url: 'http://example.com', lhr: '{"lhr": true}'}],
7878
[{projectId: 1, buildId: 2, url: 'http://example.com', lhr: '{"lhr": true}'}],
79+
[{projectId: 1, buildId: 2, url: 'http://example.com', lhr: '{"lhr": true}'}],
80+
[{projectId: 1, buildId: 2, url: 'http://example.com', lhr: '{"lhr": true}'}],
7981
]);
8082
expect(storageMethod.sealBuild).toHaveBeenCalled();
8183
});
@@ -114,14 +116,13 @@ describe('cron/psi-collect', () => {
114116
});
115117

116118
it('should respect number of runs', async () => {
117-
const site = {urls: ['http://example.com'], numberOfRuns: 5};
119+
const site = {urls: ['http://example.com'], numberOfRuns: 4};
118120
await psiCollectForProject(storageMethod, psi, site);
119121
expect(storageMethod.createRun.mock.calls).toMatchObject([
120122
[{projectId: 1, buildId: 2, url: 'http://example.com', lhr: '{"lhr": true}'}],
121123
[{projectId: 1, buildId: 2, url: 'http://example.com', lhr: '{"lhr": true}'}],
122124
[{projectId: 1, buildId: 2, url: 'http://example.com', lhr: '{"lhr": true}'}],
123125
[{projectId: 1, buildId: 2, url: 'http://example.com', lhr: '{"lhr": true}'}],
124-
[{projectId: 1, buildId: 2, url: 'http://example.com', lhr: '{"lhr": true}'}],
125126
]);
126127
});
127128

@@ -137,21 +138,34 @@ describe('cron/psi-collect', () => {
137138
});
138139
});
139140

140-
describe('.startPsiCollectCron', () => {
141+
describe('.startPsiCollectCron()', () => {
141142
const logLevel = 'silent';
142143

143144
it('should schedule a cron job per site', () => {
144145
const psiCollectCron = {
145146
sites: [
146-
{schedule: '0 * * * *', urls: ['http://example.com']},
147-
{schedule: '0 * * * *', urls: ['http://other-example.com']},
147+
{schedule: '0 * * * *', urls: ['http://example.com'], projectSlug: 'a'},
148+
{schedule: '0 * * * *', urls: ['http://other-example.com'], projectSlug: 'b'},
148149
],
149150
};
150151

151152
startPsiCollectCron(storageMethod, {logLevel, psiCollectCron});
152153
expect(cronJob).toHaveBeenCalledTimes(2);
153154
});
154155

156+
it('should validate uniqueness', () => {
157+
const psiCollectCron = {
158+
sites: [
159+
{schedule: '0 * * * *', urls: ['http://example.com']},
160+
{schedule: '0 * * * *', urls: ['http://other-example.com']},
161+
],
162+
};
163+
164+
expect(() => startPsiCollectCron(storageMethod, {logLevel, psiCollectCron})).toThrow(
165+
/more than one/
166+
);
167+
});
168+
155169
it('should validate cron job', () => {
156170
const psiCollectCron = {
157171
sites: [{schedule: '* * * * *', urls: ['http://example.com']}],

packages/utils/src/psi-client.js

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ class PsiClient {
3333
url.searchParams.set('locale', locale);
3434
url.searchParams.set('strategy', strategy);
3535
url.searchParams.set('key', this._apiKey);
36+
url.searchParams.append('category', 'performance');
37+
url.searchParams.append('category', 'accessibility');
38+
url.searchParams.append('category', 'best-practices');
39+
url.searchParams.append('category', 'pwa');
40+
url.searchParams.append('category', 'seo');
3641

3742
const response = await this._fetch(url.href);
3843
const body = await response.json();

packages/utils/test/psi-client.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('PSI API Client', () => {
2121
const client = new PsiClient({apiKey, fetch: fetchMock});
2222
expect(await client.run('https://example.com')).toEqual(lighthouseResult);
2323
expect(fetchMock).toHaveBeenCalledWith(
24-
'https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=https%3A%2F%2Fexample.com&locale=en_US&strategy=mobile&key=the-key'
24+
'https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=https%3A%2F%2Fexample.com&locale=en_US&strategy=mobile&key=the-key&category=performance&category=accessibility&category=best-practices&category=pwa&category=seo'
2525
);
2626
});
2727

0 commit comments

Comments
 (0)