Skip to content

Commit

Permalink
feat: pino logs
Browse files Browse the repository at this point in the history
  • Loading branch information
sanoel committed Oct 2, 2024
1 parent f15c5e9 commit 796d13f
Show file tree
Hide file tree
Showing 6 changed files with 413 additions and 19 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oada/jobs",
"version": "4.6.1",
"version": "4.7.0",
"description": "A library for oada job based microservices",
"source": "src/index.ts",
"main": "dist/index.js",
Expand Down Expand Up @@ -62,6 +62,7 @@
"@oada/lib-prom": "^3.8.0",
"@oada/list-lib": "^4.3.0",
"@oada/oadaify": "^2.1.0",
"@oada/pino-debug": "^4.0.1",
"@oada/types": "^3.5.3",
"@sindresorhus/is": "^6.3.1",
"ava": "^6.1.3",
Expand Down
12 changes: 10 additions & 2 deletions src/Runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import type { OADAClient } from '@oada/client';
import type { Json, JsonCompatible } from './index.js';
import { debug, error, info, trace } from './utils.js';
import { Job } from './Job.js';
import { Logger } from './Logger.js';
import type { Logger } from '@oada/pino-debug';
import type { Service } from './Service.js';
import { tree } from './tree.js';

Expand All @@ -48,6 +48,7 @@ export class Runner {
readonly #jobKey: string;
readonly #job: Job;
readonly #oada: OADAClient;
readonly #log: Logger;

/**
* Create a Runner
Expand All @@ -61,6 +62,13 @@ export class Runner {
this.#jobKey = jobKey;
this.#job = job;
this.#oada = oada;
this.#log = service.log.child({
jobId: job.oadaId,
type: job.type,
// @ts-expect-error Json type is annoying
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
...(job?.config?.traceId ? { traceId: job?.config?.traceId } : {}),
});
}

/**
Expand Down Expand Up @@ -112,7 +120,7 @@ export class Runner {
const r = await pTimeout(
worker.work(this.#job, {
jobId: this.#job.oadaId,
log: new Logger(this),
log: this.#log,
oada: this.#oada,
}),
{
Expand Down
27 changes: 16 additions & 11 deletions src/Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
* limitations under the License.
*/

import { type Logger, pino } from '@oada/pino-debug';

import type { Config } from '@oada/client';
import { Gauge } from '@oada/lib-prom';
import { OADAClient } from '@oada/client';
import { assert as assertQueue } from '@oada/types/oada/service/queue.js';

import { Report, type ReportConstructor } from './Report.js';
import { debug, error, warn } from './utils.js';
import { type Job } from './Job.js';
import type { Json } from './index.js';
import type { Logger } from './Logger.js';
import { Queue } from './Queue.js';

export type Domain = string;
Expand Down Expand Up @@ -62,6 +62,7 @@ export interface ConstructorArguments {
oada?: OADAClient | Config;
concurrency?: number;
opts?: ServiceOptions;
log?: Logger;
}

export const defaultServiceQueueName = 'default-service-queue';
Expand All @@ -83,6 +84,7 @@ export class Service {
public token: string;
public opts: ServiceOptions | undefined;
public metrics;
public log: Logger;

readonly #oada: OADAClient;
// Readonly #clients = new Map<Domain, OADAClient>();
Expand All @@ -100,17 +102,21 @@ export class Service {
*/
constructor(object: ConstructorArguments) {
this.name = object.name;
this.log = object.log ?? pino({ base: { service: this.name } });

if (
// @ts-expect-error instanceof OADAClient does not work
object.oada?.getDomain !== undefined &&
// @ts-expect-error instanceof OADAClient does not work
object.oada?.getToken !== undefined
) {
debug('Using oada connection passed to constructor');
this.log.debug('Using oada connection passed to constructor');
// @ts-expect-error instanceof OADAClient does not work
this.#oada = object.oada;
} else {
debug('Opening OADA connection from domain/token that were passed');
this.log.debug(
'Opening OADA connection from domain/token that were passed'
);
try {
// @ts-expect-error instanceof OADAClient does not work
this.#oada = new OADAClient(object.oada!);
Expand All @@ -124,7 +130,6 @@ export class Service {
this.domain = this.#oada.getDomain();
this.token = this.#oada.getToken()[0]!;
this.concurrency = object.concurrency ?? this.#oada.getConcurrency();

// TODO: Get total pending jobs in collect callback?
this.metrics = new Gauge({
name: 'oada_jobs_total',
Expand Down Expand Up @@ -213,7 +218,7 @@ export class Service {
const worker = this.#workers.get(type);

if (!worker) {
error('No worker registered for %s', type);
this.log.error('No worker registered for %s', type);
throw new Error(`No worker registered for ${type}`);
}

Expand Down Expand Up @@ -256,18 +261,18 @@ export class Service {
await queue.start(this.opts?.skipQueueOnStartup);
this.#queue = queue;
} catch (error_) {
warn('Invalid queue');
debug('Invalid queue: %O', error_);
error(error_);
this.log.warn('Invalid queue');
this.log.debug('Invalid queue: %O', error_);
this.log.error(error_);
}
}

async #stopQueue(): Promise<void> {
try {
await this.#queue?.stop();
} catch (error_) {
warn('Unable to stop queues');
debug('Unable to stop queue %0', error_);
this.log.warn('Unable to stop queues');
this.log.debug('Unable to stop queue %0', error_);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Logger.ts → src/Updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import type { Runner } from './Runner.js';
/**
* Manages logging updates to a running job.
*/
export class Logger {
export class Updater {
/**
* Create a Logger.
* Create a Updater.
* @param _runner The runner of the job
*/
constructor(private readonly _runner: Runner) {}
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ declare global {
export {
Service,
type WorkerFunction,
type WorkerContext,
type JobId,
type FinishReporter,
} from './Service.js';
export { JobError } from './Runner.js';
export { type JobUpdate, Job } from './Job.js';
export { Logger } from './Logger.js';
//export { Logger } from './Logger.js';
export { parseAttachment, reportOnItem, type ReportConfig } from './Report.js';
export { postJob, postUpdate } from './utils.js';

Expand Down
Loading

0 comments on commit 796d13f

Please sign in to comment.