Skip to content

Commit 1509cca

Browse files
committed
Add job identity to indexing log messages
1 parent e1f952e commit 1509cca

File tree

4 files changed

+95
-34
lines changed

4 files changed

+95
-34
lines changed

packages/host/app/components/card-prerender.gts

+10-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Component from '@glimmer/component';
55

66
import { didCancel, enqueueTask } from 'ember-concurrency';
77

8-
import { type IndexWriter } from '@cardstack/runtime-common';
8+
import { type IndexWriter, type JobInfo } from '@cardstack/runtime-common';
99
import { readFileAsText as _readFileAsText } from '@cardstack/runtime-common/stream';
1010
import {
1111
getReader,
@@ -100,11 +100,12 @@ export default class CardPrerender extends Component {
100100
});
101101

102102
private doFromScratch = enqueueTask(async (realmURL: URL) => {
103-
let { reader, indexWriter } = this.getRunnerParams(realmURL);
103+
let { reader, indexWriter, jobInfo } = this.getRunnerParams(realmURL);
104104
let currentRun = new CurrentRun({
105105
realmURL,
106106
reader,
107107
indexWriter,
108+
jobInfo,
108109
renderCard: this.renderService.renderCard,
109110
render: this.renderService.render,
110111
});
@@ -122,11 +123,12 @@ export default class CardPrerender extends Component {
122123
operation: 'delete' | 'update',
123124
ignoreData: Record<string, string>,
124125
) => {
125-
let { reader, indexWriter } = this.getRunnerParams(realmURL);
126+
let { reader, indexWriter, jobInfo } = this.getRunnerParams(realmURL);
126127
let currentRun = new CurrentRun({
127128
realmURL,
128129
reader,
129130
indexWriter,
131+
jobInfo,
130132
ignoreData: { ...ignoreData },
131133
renderCard: this.renderService.renderCard,
132134
render: this.renderService.render,
@@ -144,15 +146,18 @@ export default class CardPrerender extends Component {
144146
private getRunnerParams(realmURL: URL): {
145147
reader: Reader;
146148
indexWriter: IndexWriter;
149+
jobInfo?: JobInfo;
147150
} {
148151
if (this.fastboot.isFastBoot) {
149152
let optsId = (globalThis as any).runnerOptsId;
150153
if (optsId == null) {
151154
throw new Error(`Runner Options Identifier was not set`);
152155
}
156+
let { reader, indexWriter, jobInfo } = getRunnerOpts(optsId);
153157
return {
154-
reader: getRunnerOpts(optsId).reader,
155-
indexWriter: getRunnerOpts(optsId).indexWriter,
158+
reader,
159+
indexWriter,
160+
jobInfo,
156161
};
157162
} else {
158163
return {

packages/host/app/lib/current-run.ts

+58-24
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
type Relationship,
3434
type TextFileRef,
3535
type LastModifiedTimes,
36+
type JobInfo,
3637
} from '@cardstack/runtime-common';
3738
import { Deferred } from '@cardstack/runtime-common/deferred';
3839
import {
@@ -88,6 +89,7 @@ export class CurrentRun {
8889
#render: Render;
8990
#realmURL: URL;
9091
#realmInfo?: RealmInfo;
92+
#jobInfo?: JobInfo;
9193
readonly stats: Stats = {
9294
instancesIndexed: 0,
9395
modulesIndexed: 0,
@@ -105,13 +107,15 @@ export class CurrentRun {
105107
ignoreData = {},
106108
renderCard,
107109
render,
110+
jobInfo,
108111
}: {
109112
realmURL: URL;
110113
reader: Reader;
111114
indexWriter: IndexWriter;
112115
ignoreData?: Record<string, string>;
113116
renderCard: RenderCard;
114117
render: Render;
118+
jobInfo?: JobInfo;
115119
}) {
116120
this.#indexWriter = indexWriter;
117121
this.#realmPaths = new RealmPaths(realmURL);
@@ -120,44 +124,51 @@ export class CurrentRun {
120124
this.#ignoreData = ignoreData;
121125
this.#renderCard = renderCard;
122126
this.#render = render;
127+
this.#jobInfo = jobInfo;
123128
}
124129

125130
static async fromScratch(current: CurrentRun): Promise<IndexResults> {
126131
let start = Date.now();
127-
log.debug(`starting from scratch indexing`);
132+
log.debug(
133+
`${jobIdentity(current.#jobInfo)} starting from scratch indexing`,
134+
);
128135
perfLog.debug(
129-
`starting from scratch indexing for realm ${current.realmURL.href}`,
136+
`${jobIdentity(current.#jobInfo)} starting from scratch indexing for realm ${current.realmURL.href}`,
130137
);
131138
current.#batch = await current.#indexWriter.createBatch(current.realmURL);
132139
let invalidations: URL[] = [];
133140
let mtimesStart = Date.now();
134141
let mtimes = await current.batch.getModifiedTimes();
135142
perfLog.debug(
136-
`completed getting index mtimes in ${Date.now() - mtimesStart} ms`,
143+
`${jobIdentity(current.#jobInfo)} completed getting index mtimes in ${Date.now() - mtimesStart} ms`,
137144
);
138145
let invalidateStart = Date.now();
139146
invalidations = (
140147
await current.discoverInvalidations(current.realmURL, mtimes)
141148
).map((href) => new URL(href));
142149
perfLog.debug(
143-
`completed invalidations in ${Date.now() - invalidateStart} ms`,
150+
`${jobIdentity(current.#jobInfo)} completed invalidations in ${Date.now() - invalidateStart} ms`,
144151
);
145152

146153
await current.whileIndexing(async () => {
147154
let visitStart = Date.now();
148155
for (let invalidation of invalidations) {
149156
await current.tryToVisit(invalidation);
150157
}
151-
perfLog.debug(`completed index visit in ${Date.now() - visitStart} ms`);
158+
perfLog.debug(
159+
`${jobIdentity(current.#jobInfo)} completed index visit in ${Date.now() - visitStart} ms`,
160+
);
152161
let finalizeStart = Date.now();
153162
let { totalIndexEntries } = await current.batch.done();
154163
perfLog.debug(
155-
`completed index finalization in ${Date.now() - finalizeStart} ms`,
164+
`${jobIdentity(current.#jobInfo)} completed index finalization in ${Date.now() - finalizeStart} ms`,
156165
);
157166
current.stats.totalIndexEntries = totalIndexEntries;
158-
log.debug(`completed from scratch indexing in ${Date.now() - start}ms`);
167+
log.debug(
168+
`${jobIdentity(current.#jobInfo)} completed from scratch indexing in ${Date.now() - start}ms`,
169+
);
159170
perfLog.debug(
160-
`completed from scratch indexing for realm ${
171+
`${jobIdentity(current.#jobInfo)} completed from scratch indexing for realm ${
161172
current.realmURL.href
162173
} in ${Date.now() - start} ms`,
163174
);
@@ -180,7 +191,9 @@ export class CurrentRun {
180191
},
181192
): Promise<IndexResults> {
182193
let start = Date.now();
183-
log.debug(`starting from incremental indexing for ${url.href}`);
194+
log.debug(
195+
`${jobIdentity(current.#jobInfo)} starting from incremental indexing for ${url.href}`,
196+
);
184197

185198
current.#batch = await current.#indexWriter.createBatch(current.realmURL);
186199
let invalidations = (await current.batch.invalidate(url)).map(
@@ -200,7 +213,7 @@ export class CurrentRun {
200213
current.stats.totalIndexEntries = totalIndexEntries;
201214

202215
log.debug(
203-
`completed incremental indexing for ${url.href} in ${
216+
`${jobIdentity(current.#jobInfo)} completed incremental indexing for ${url.href} in ${
204217
Date.now() - start
205218
}ms`,
206219
);
@@ -217,7 +230,9 @@ export class CurrentRun {
217230
await this.visitFile(url);
218231
} catch (err: any) {
219232
if (isCardError(err) && err.status === 404) {
220-
log.info(`tried to visit file ${url.href}, but it no longer exists`);
233+
log.info(
234+
`${jobIdentity(this.#jobInfo)} tried to visit file ${url.href}, but it no longer exists`,
235+
);
221236
} else {
222237
throw err;
223238
}
@@ -258,12 +273,16 @@ export class CurrentRun {
258273
url: URL,
259274
indexMtimes: LastModifiedTimes,
260275
): Promise<string[]> {
261-
log.debug(`discovering invalidations in dir ${url.href}`);
262-
perfLog.debug(`discovering invalidations in dir ${url.href}`);
276+
log.debug(
277+
`${jobIdentity(this.#jobInfo)} discovering invalidations in dir ${url.href}`,
278+
);
279+
perfLog.debug(
280+
`${jobIdentity(this.#jobInfo)} discovering invalidations in dir ${url.href}`,
281+
);
263282
let mtimesStart = Date.now();
264283
let filesystemMtimes = await this.#reader.mtimes();
265284
perfLog.debug(
266-
`time to get file system mtimes ${Date.now() - mtimesStart} ms`,
285+
`${jobIdentity(this.#jobInfo)} time to get file system mtimes ${Date.now() - mtimesStart} ms`,
267286
);
268287

269288
let ignoreFile = new URL('.gitignore', url).href;
@@ -278,7 +297,9 @@ export class CurrentRun {
278297
this.#ignoreData[url.href] = ignorePatterns.content;
279298
}
280299
} else {
281-
perfLog.debug(`skip getting the ignore file--there is nothing to ignore`);
300+
perfLog.debug(
301+
`${jobIdentity(this.#jobInfo)} skip getting the ignore file--there is nothing to ignore`,
302+
);
282303
}
283304

284305
let invalidationList: string[] = [];
@@ -314,7 +335,7 @@ export class CurrentRun {
314335
await this.batch.invalidate(new URL(invalidationURL));
315336
}
316337
perfLog.debug(
317-
`time to invalidate ${url} ${Date.now() - invalidationStart} ms`,
338+
`${jobIdentity(this.#jobInfo)} time to invalidate ${url} ${Date.now() - invalidationStart} ms`,
318339
);
319340
return this.batch.invalidations;
320341
}
@@ -327,7 +348,7 @@ export class CurrentRun {
327348
return;
328349
}
329350
let start = Date.now();
330-
log.debug(`begin visiting file ${url.href}`);
351+
log.debug(`${jobIdentity(this.#jobInfo)} begin visiting file ${url.href}`);
331352
let localPath: string;
332353
try {
333354
localPath = this.#realmPaths.local(url);
@@ -336,7 +357,7 @@ export class CurrentRun {
336357
// graph cross a realm just skip over the file. it will be out
337358
// of date, but such is life...
338359
log.debug(
339-
`Visit of ${url.href} cannot be performed as it is in a different realm than the realm whose contents are being invalidated (${this.realmURL.href})`,
360+
`${jobIdentity(this.#jobInfo)} Visit of ${url.href} cannot be performed as it is in a different realm than the realm whose contents are being invalidated (${this.realmURL.href})`,
340361
);
341362
return;
342363
}
@@ -365,13 +386,15 @@ export class CurrentRun {
365386
let { data } = JSON.parse(content);
366387
resource = data;
367388
} catch (e) {
368-
log.warn(`unable to parse ${url.href} as card JSON`);
389+
log.warn(
390+
`${jobIdentity(this.#jobInfo)} unable to parse ${url.href} as card JSON`,
391+
);
369392
}
370393

371394
if (resource && isCardResource(resource)) {
372395
if (lastModified == null) {
373396
log.warn(
374-
`No lastModified date available for ${url.href}, using current time`,
397+
`${jobIdentity(this.#jobInfo)} No lastModified date available for ${url.href}, using current time`,
375398
);
376399
lastModified = unixTime(Date.now());
377400
}
@@ -386,7 +409,9 @@ export class CurrentRun {
386409
}
387410
}
388411
}
389-
log.debug(`completed visiting file ${url.href} in ${Date.now() - start}ms`);
412+
log.debug(
413+
`${jobIdentity(this.#jobInfo)} completed visiting file ${url.href} in ${Date.now() - start}ms`,
414+
);
390415
}
391416

392417
private async indexModule(url: URL, ref: TextFileRef): Promise<void> {
@@ -395,7 +420,7 @@ export class CurrentRun {
395420
} catch (err: any) {
396421
this.stats.moduleErrors++;
397422
log.warn(
398-
`encountered error loading module "${url.href}": ${err.message}`,
423+
`${jobIdentity(this.#jobInfo)} encountered error loading module "${url.href}": ${err.message}`,
399424
);
400425
let deps = await (
401426
await this.loaderService.loader.getConsumedModules(url.href)
@@ -413,7 +438,9 @@ export class CurrentRun {
413438
}
414439

415440
if (ref.isShimmed) {
416-
log.debug(`skipping indexing of shimmed module ${url.href}`);
441+
log.debug(
442+
`${jobIdentity(this.#jobInfo)} skipping indexing of shimmed module ${url.href}`,
443+
);
417444
return;
418445
}
419446
let consumes = (
@@ -650,7 +677,7 @@ export class CurrentRun {
650677
throw err;
651678
}
652679
log.warn(
653-
`encountered error indexing card instance ${path}: ${error.error.message}`,
680+
`${jobIdentity(this.#jobInfo)} encountered error indexing card instance ${path}: ${error.error.message}`,
654681
);
655682
await this.updateEntry(instanceURL, error);
656683
}
@@ -811,3 +838,10 @@ function getDisplayName(card: typeof CardDef) {
811838
return card.displayName;
812839
}
813840
}
841+
842+
function jobIdentity(jobInfo?: JobInfo): string {
843+
if (!jobInfo) {
844+
return `[no job identity]`;
845+
}
846+
return `[job: ${jobInfo.jobId}] [jobReservation: ${jobInfo.reservationId}]`;
847+
}

packages/postgres/pg-queue.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
type QueueRunner,
55
type PgPrimitive,
66
type Expression,
7+
type JobInfo,
78
param,
89
separatedByCommas,
910
addExplicitParens,
@@ -269,11 +270,20 @@ export class PgQueueRunner implements QueueRunner {
269270
}
270271
}
271272

272-
private async runJob(jobType: string, args: PgPrimitive) {
273+
private async runJob(jobType: string, args: PgPrimitive, jobInfo: JobInfo) {
273274
let handler = this.#handlers.get(jobType);
274275
if (!handler) {
275276
throw new Error(`unknown job handler ${jobType}`);
276277
}
278+
279+
if (
280+
args &&
281+
typeof args === 'object' &&
282+
!Array.isArray(args) &&
283+
!('jobInfo' in args)
284+
) {
285+
args.jobInfo = jobInfo;
286+
}
277287
return await handler(args);
278288
}
279289

@@ -348,7 +358,11 @@ export class PgQueueRunner implements QueueRunner {
348358
try {
349359
log.debug(`%s: running %s`, this.#workerId, jobToRun.id);
350360
result = await Promise.race([
351-
this.runJob(jobToRun.job_type, jobToRun.args),
361+
this.runJob(jobToRun.job_type, jobToRun.args, {
362+
jobId: jobToRun.id,
363+
workerId: this.#workerId,
364+
reservationId: jobReservationId,
365+
}),
352366
// we race the job so that it doesn't hold this worker hostage if
353367
// the job's promise never resolves
354368
new Promise<'timeout'>((r) =>

0 commit comments

Comments
 (0)