chore(server): print jobId (#12593)

Need to query the payload through job id for debugging

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->

## Summary by CodeRabbit

- **Tests**
  - Updated job metrics test to include an explicit job ID during execution.

- **Refactor**
  - Enhanced job execution to support an optional job ID, improving job tracking and logging.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
fengmk2
2025-05-28 08:48:38 +00:00
parent 793823a9f9
commit 9abbfa3ab4
2 changed files with 5 additions and 4 deletions

View File

@@ -168,7 +168,7 @@ test('should be able to record job metrics', async t => {
timerStub.reset(); timerStub.reset();
await t.throwsAsync( await t.throwsAsync(
executor.run('nightly.__test__throw', { name: 'test executor' }), executor.run('nightly.__test__throw', { name: 'test executor' }, 'test-id'),
{ {
message: 'Throw in job handler', message: 'Throw in job handler',
} }

View File

@@ -58,7 +58,8 @@ export class JobExecutor implements OnModuleDestroy {
async run<T extends JobName>( async run<T extends JobName>(
name: T, name: T,
payload: Jobs[T] payload: Jobs[T],
jobId?: string
): Promise<JOB_SIGNAL | undefined> { ): Promise<JOB_SIGNAL | undefined> {
const ns = namespace(name); const ns = namespace(name);
const handler = this.scanner.getHandler(name); const handler = this.scanner.getHandler(name);
@@ -70,7 +71,7 @@ export class JobExecutor implements OnModuleDestroy {
const fn = wrapCallMetric( const fn = wrapCallMetric(
async () => { async () => {
const signature = `[${name}] (${handler.name})`; const signature = `[${name}] (${handler.name}, id=${jobId})`;
try { try {
this.logger.debug(`Job started: ${signature}`); this.logger.debug(`Job started: ${signature}`);
const ret = await handler.fn(payload); const ret = await handler.fn(payload);
@@ -129,7 +130,7 @@ export class JobExecutor implements OnModuleDestroy {
return await cls.run(async () => { return await cls.run(async () => {
cls.set(CLS_ID, requestId); cls.set(CLS_ID, requestId);
return await this.run(job.name as JobName, payload); return await this.run(job.name as JobName, payload, job.id);
}); });
}, },
merge({}, this.config.job.queue, this.config.job.worker, queueOptions, { merge({}, this.config.job.queue, this.config.job.worker, queueOptions, {