Skip to content

Commit

Permalink
refactor(depa): replace lodash methods (#2454)
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf authored Mar 12, 2024
1 parent 096bd81 commit 5612d5b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 11 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 2 additions & 3 deletions src/classes/flow-producer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { EventEmitter } from 'events';
import { get } from 'lodash';
import { Redis, ChainableCommander } from 'ioredis';
import { v4 } from 'uuid';
import {
Expand Down Expand Up @@ -279,15 +278,15 @@ 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(
queue,
node.name,
node.data,
{
...(jobsOpts ? jobsOpts : {}),
...jobsOpts,
...node.opts,
parent: parent?.parentOpts,
},
Expand Down
8 changes: 4 additions & 4 deletions src/classes/main-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>,
Expand Down Expand Up @@ -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),
Expand Down
5 changes: 2 additions & 3 deletions src/classes/queue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { get } from 'lodash';
import { v4 } from 'uuid';
import {
BaseJobOptions,
Expand Down Expand Up @@ -112,15 +111,15 @@ export class Queue<
Connection,
);

this.jobsOpts = get(opts, 'defaultJobOptions') ?? {};
this.jobsOpts = opts?.defaultJobOptions ?? {};

this.waitUntilReady()
.then(client => {
if (!this.closing) {
client.hset(
this.keys.meta,
'opts.maxLenEvents',
get(opts, 'streams.events.maxLen', 10000),
opts?.streams?.events?.maxLen ?? 10000,
);
}
})
Expand Down
24 changes: 24 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,28 @@ export const errorToJSON = (value: any): Record<string, any> => {
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';

0 comments on commit 5612d5b

Please sign in to comment.