From 5612d5b0803a11ffe1997dfb2385c07679fc2cb3 Mon Sep 17 00:00:00 2001 From: Rogger Valverde Date: Mon, 11 Mar 2024 20:00:50 -0600 Subject: [PATCH] refactor(depa): replace lodash methods (#2454) --- package.json | 1 - src/classes/flow-producer.ts | 5 ++--- src/classes/main-base.ts | 8 ++++---- src/classes/queue.ts | 5 ++--- src/utils.ts | 24 ++++++++++++++++++++++++ 5 files changed, 32 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 302a6e796a..1926ba3d9a 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,6 @@ "dependencies": { "cron-parser": "^4.6.0", "ioredis": "^5.3.2", - "lodash": "^4.17.21", "msgpackr": "^1.10.1", "node-abort-controller": "^3.1.1", "semver": "^7.5.4", diff --git a/src/classes/flow-producer.ts b/src/classes/flow-producer.ts index c2fe9b2382..5753aaec07 100644 --- a/src/classes/flow-producer.ts +++ b/src/classes/flow-producer.ts @@ -1,5 +1,4 @@ import { EventEmitter } from 'events'; -import { get } from 'lodash'; import { Redis, ChainableCommander } from 'ioredis'; import { v4 } from 'uuid'; import { @@ -279,7 +278,7 @@ export class FlowProducer extends EventEmitter { const queue = this.queueFromNode(node, new QueueKeys(prefix), prefix); const queueOpts = queuesOpts && queuesOpts[node.queueName]; - const jobsOpts = get(queueOpts, 'defaultJobOptions'); + const jobsOpts = queueOpts?.defaultJobOptions ?? {}; const jobId = node.opts?.jobId || v4(); const job = new this.Job( @@ -287,7 +286,7 @@ export class FlowProducer extends EventEmitter { node.name, node.data, { - ...(jobsOpts ? jobsOpts : {}), + ...jobsOpts, ...node.opts, parent: parent?.parentOpts, }, diff --git a/src/classes/main-base.ts b/src/classes/main-base.ts index c1dc42ac80..28d1f6e551 100644 --- a/src/classes/main-base.ts +++ b/src/classes/main-base.ts @@ -2,10 +2,9 @@ * Wrapper for sandboxing. * */ -import { toString } from 'lodash'; import { ChildProcessor } from './child-processor'; import { ParentCommand, ChildCommand } from '../enums'; -import { errorToJSON } from '../utils'; +import { errorToJSON, toString } from '../utils'; export default ( send: (msg: any) => Promise, @@ -33,10 +32,11 @@ export default ( process.on('SIGTERM', () => childProcessor.waitForCurrentJobAndExit()); process.on('SIGINT', () => childProcessor.waitForCurrentJobAndExit()); - process.on('uncaughtException', async (err: Error) => { - if (!err.message) { + process.on('uncaughtException', async (err: any) => { + if (typeof err !== 'object') { err = new Error(toString(err)); } + await send({ cmd: ParentCommand.Failed, value: errorToJSON(err), diff --git a/src/classes/queue.ts b/src/classes/queue.ts index cee7af045c..16ef4c728d 100644 --- a/src/classes/queue.ts +++ b/src/classes/queue.ts @@ -1,4 +1,3 @@ -import { get } from 'lodash'; import { v4 } from 'uuid'; import { BaseJobOptions, @@ -112,7 +111,7 @@ export class Queue< Connection, ); - this.jobsOpts = get(opts, 'defaultJobOptions') ?? {}; + this.jobsOpts = opts?.defaultJobOptions ?? {}; this.waitUntilReady() .then(client => { @@ -120,7 +119,7 @@ export class Queue< client.hset( this.keys.meta, 'opts.maxLenEvents', - get(opts, 'streams.events.maxLen', 10000), + opts?.streams?.events?.maxLen ?? 10000, ); } }) diff --git a/src/utils.ts b/src/utils.ts index eebbd6bdc2..cceecb438c 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -222,4 +222,28 @@ export const errorToJSON = (value: any): Record => { return error; }; +const INFINITY = 1 / 0; + +export const toString = (value: any): string => { + if (value == null) { + return ''; + } + // Exit early for strings to avoid a performance hit in some environments. + if (typeof value === 'string') { + return value; + } + if (Array.isArray(value)) { + // Recursively convert values (susceptible to call stack limits). + return `${value.map(other => (other == null ? other : toString(other)))}`; + } + if ( + typeof value == 'symbol' || + Object.prototype.toString.call(value) == '[object Symbol]' + ) { + return value.toString(); + } + const result = `${value}`; + return result === '0' && 1 / value === -INFINITY ? '-0' : result; +}; + export const QUEUE_EVENT_SUFFIX = ':qe';