Skip to content

Commit 9fcad4a

Browse files
committed
Refactor manifest storage
1 parent c04d4fa commit 9fcad4a

File tree

2 files changed

+86
-61
lines changed

2 files changed

+86
-61
lines changed

src/server.ts

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ interface ManifestCache {
1616
[requestSha: string]: string;
1717
}
1818

19-
const downloadManifest = async (branchOrRef: string) => {
19+
const getManifest = async (siteId: string, branchOrRef: string) => {
2020
const keys = [
21-
datastore.key(['Fileset2Manifest', branchOrRef]),
22-
datastore.key(['Fileset2Manifest', `branch:${branchOrRef}`]),
21+
datastore.key(['Fileset2Manifest', `${siteId}:branch:${branchOrRef}`]),
22+
datastore.key(['Fileset2Manifest', `${siteId}:ref:${branchOrRef}`]),
2323
];
2424
const resp = await datastore.get(keys);
2525
if (!resp || !resp[0]) {
@@ -30,28 +30,23 @@ const downloadManifest = async (branchOrRef: string) => {
3030
if (!result) {
3131
return;
3232
}
33-
if (!result.ttls) {
34-
return result;
35-
}
3633

37-
if (result.ttls) {
38-
// TODO: Allow this to be overwritten.
39-
const now = new Date();
40-
let latestManifest = null;
41-
for (const ttlString in result.ttls) {
42-
const ttlDate = new Date(ttlString);
43-
const isLaterThanManifestDate = now >= ttlDate;
44-
const isLaterThanAllManifests =
45-
!latestManifest || ttlDate >= latestManifest.ttl;
46-
if (isLaterThanManifestDate && isLaterThanAllManifests) {
47-
latestManifest = result.ttls[ttlString];
48-
latestManifest.ttl = ttlDate;
49-
}
50-
}
51-
if (latestManifest) {
52-
return latestManifest;
34+
// TODO: Allow this to be overwritten.
35+
const now = new Date();
36+
let latestManifest = null;
37+
for (const ttlString in result.schedule) {
38+
const ttlDate = new Date(ttlString);
39+
const isLaterThanManifestDate = now >= ttlDate;
40+
const isLaterThanAllManifests =
41+
!latestManifest || ttlDate >= latestManifest.ttl;
42+
if (isLaterThanManifestDate && isLaterThanAllManifests) {
43+
latestManifest = result.schedule[ttlString];
44+
latestManifest.ttl = ttlDate;
5345
}
5446
}
47+
if (latestManifest) {
48+
return latestManifest;
49+
}
5550
return result;
5651
};
5752

@@ -73,7 +68,7 @@ const parseHostname = (hostname: string) => {
7368
};
7469

7570
export function createApp(siteId: string, branchOrRef: string) {
76-
// const startupManifest = await downloadManifest(branchOrRef);
71+
// const startupManifest = await getManifest(branchOrRef);
7772
console.log(`Starting server for site: ${siteId} @ ${branchOrRef}`);
7873

7974
const app = express();
@@ -88,7 +83,7 @@ export function createApp(siteId: string, branchOrRef: string) {
8883
blobPath += 'index.html';
8984
}
9085

91-
const manifest = await downloadManifest(requestBranchOrRef);
86+
const manifest = await getManifest(requestSiteId, requestBranchOrRef);
9287
if (!manifest) {
9388
res
9489
.status(404)
@@ -99,7 +94,6 @@ export function createApp(siteId: string, branchOrRef: string) {
9994
}
10095

10196
const manifestPaths = manifest.paths;
102-
10397
if (!manifestPaths) {
10498
res
10599
.status(404)
@@ -129,7 +123,7 @@ export function createApp(siteId: string, branchOrRef: string) {
129123
preserveHeaderKeyCase: true,
130124
});
131125
server.on('error', (error, req, res) => {
132-
console.log(`An error occurred while serving ${req.url}`, error);
126+
console.log(`An error occurred while serving ${req.url} (${error})`);
133127
});
134128
server.on('proxyRes', (proxyRes, req, res) => {
135129
delete proxyRes.headers['x-cloud-trace-context'];

src/upload.ts

Lines changed: 66 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -130,54 +130,85 @@ export async function uploadManifest(
130130
}
131131
}
132132

133-
async function createEntity(key: entity.Key, manifest: Manifest, ttl?: Date) {
134-
const manifestPaths = manifest.toJSON();
135-
let props: any = {
136-
created: new Date(),
137-
site: manifest.site,
133+
interface ScheduleItem {
134+
ttl: Date;
135+
ref: string;
136+
paths: Record<string, string>;
137+
}
138+
139+
function createScheduleItem(
140+
manifest: Manifest,
141+
manifestPaths: Record<string, string>,
142+
ttl: Date
143+
) {
144+
return {
145+
ttl: ttl,
138146
ref: manifest.ref,
139-
branch: manifest.branch,
140147
paths: manifestPaths,
141-
};
142-
if (ttl) {
143-
const key = datastore.key([
144-
'Fileset2Manifest',
145-
`branch:${manifest.branch}`,
146-
]);
147-
const resp = await datastore.get(key);
148-
if (resp && resp[0]) {
149-
console.log(
150-
`Adding TTL for branch ${manifest.branch} -> ${manifest.shortSha} @ ${ttl}`
151-
);
152-
const entity = resp[0];
153-
if (entity.ttls) {
154-
entity.ttls[ttl.toString()] = props;
155-
} else {
156-
entity.ttls = {ttl: props};
157-
}
158-
props = entity;
159-
}
160-
}
161-
return {
162-
key: key,
163-
excludeFromIndexes: ['paths', 'ttls'],
164-
data: props,
165-
};
148+
} as ScheduleItem;
166149
}
167150

168151
async function finalize(manifest: Manifest, ttl?: Date) {
152+
const manifestPaths = manifest.toJSON();
153+
const now = new Date();
154+
169155
// Create shortSha mapping.
170-
const key = datastore.key(['Fileset2Manifest', manifest.shortSha]);
171-
const ent = await createEntity(key, manifest);
156+
const key = datastore.key([
157+
'Fileset2Manifest',
158+
`${manifest.site}:ref:${manifest.shortSha}`,
159+
]);
160+
const scheduleItem = createScheduleItem(manifest, manifestPaths, now);
161+
const schedule: Record<string, ScheduleItem> = {};
162+
schedule[now.toString()] = scheduleItem;
163+
const ent = {
164+
key: key,
165+
excludeFromIndexes: ['schedule'],
166+
data: {
167+
site: manifest.site,
168+
ref: manifest.ref,
169+
branch: manifest.branch,
170+
schedule: schedule,
171+
},
172+
};
172173
await datastore.save(ent);
173174

174175
// Create branch mapping.
175176
if (manifest.branch) {
176177
const branchKey = datastore.key([
177178
'Fileset2Manifest',
178-
`branch:${manifest.branch}`,
179+
`${manifest.site}:branch:${manifest.branch}`,
179180
]);
180-
const branchEnt = await createEntity(branchKey, manifest, ttl);
181+
const branchScheduleItem = createScheduleItem(
182+
manifest,
183+
manifestPaths,
184+
ttl || now
185+
);
186+
const branchSchedule: Record<string, ScheduleItem> = {};
187+
const branchScheduleKey = (ttl || now).toString();
188+
branchSchedule[branchScheduleKey] = branchScheduleItem;
189+
const resp = await datastore.get(branchKey);
190+
let existingData = resp && resp[0];
191+
if (!existingData) {
192+
existingData = {
193+
site: manifest.site,
194+
ref: manifest.ref,
195+
branch: manifest.branch,
196+
schedule: branchSchedule,
197+
};
198+
} else {
199+
// TODO: Clean up past scheduled items here.
200+
existingData.schedule[branchScheduleKey] = branchScheduleItem;
201+
}
202+
console.log(
203+
`TTLs for branch: ${manifest.branch} -> ${Object.keys(
204+
existingData.schedule
205+
)}`
206+
);
207+
const branchEnt = {
208+
key: branchKey,
209+
excludeFromIndexes: ['schedule'],
210+
data: existingData,
211+
};
181212
await datastore.save(branchEnt);
182213
}
183214

0 commit comments

Comments
 (0)