index.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import { Cluster } from 'ioredis';
  2. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  3. // @ts-ignore
  4. import { CONNECTION_CLOSED_ERROR_MSG } from 'ioredis/built/utils';
  5. import * as semver from 'semver';
  6. import { SpanKind, TelemetryAttributes } from '../enums';
  7. export const errorObject = { value: null };
  8. export function tryCatch(fn, ctx, args) {
  9. try {
  10. return fn.apply(ctx, args);
  11. }
  12. catch (e) {
  13. errorObject.value = e;
  14. return errorObject;
  15. }
  16. }
  17. /**
  18. * Checks the size of string for ascii/non-ascii characters
  19. * @see https://stackoverflow.com/a/23318053/1347170
  20. * @param str -
  21. */
  22. export function lengthInUtf8Bytes(str) {
  23. return Buffer.byteLength(str, 'utf8');
  24. }
  25. export function isEmpty(obj) {
  26. for (const key in obj) {
  27. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  28. return false;
  29. }
  30. }
  31. return true;
  32. }
  33. export function array2obj(arr) {
  34. const obj = {};
  35. for (let i = 0; i < arr.length; i += 2) {
  36. obj[arr[i]] = arr[i + 1];
  37. }
  38. return obj;
  39. }
  40. export function objectToFlatArray(obj) {
  41. const arr = [];
  42. for (const key in obj) {
  43. if (Object.prototype.hasOwnProperty.call(obj, key) &&
  44. obj[key] !== undefined) {
  45. arr[arr.length] = key;
  46. arr[arr.length] = obj[key];
  47. }
  48. }
  49. return arr;
  50. }
  51. export function delay(ms, abortController) {
  52. return new Promise(resolve => {
  53. // eslint-disable-next-line prefer-const
  54. let timeout;
  55. const callback = () => {
  56. abortController === null || abortController === void 0 ? void 0 : abortController.signal.removeEventListener('abort', callback);
  57. clearTimeout(timeout);
  58. resolve();
  59. };
  60. timeout = setTimeout(callback, ms);
  61. abortController === null || abortController === void 0 ? void 0 : abortController.signal.addEventListener('abort', callback);
  62. });
  63. }
  64. export function increaseMaxListeners(emitter, count) {
  65. const maxListeners = emitter.getMaxListeners();
  66. emitter.setMaxListeners(maxListeners + count);
  67. }
  68. export function invertObject(obj) {
  69. return Object.entries(obj).reduce((result, [key, value]) => {
  70. result[value] = key;
  71. return result;
  72. }, {});
  73. }
  74. export const optsDecodeMap = {
  75. de: 'deduplication',
  76. fpof: 'failParentOnFailure',
  77. cpof: 'continueParentOnFailure',
  78. idof: 'ignoreDependencyOnFailure',
  79. kl: 'keepLogs',
  80. rdof: 'removeDependencyOnFailure',
  81. };
  82. export const optsEncodeMap = Object.assign(Object.assign({}, invertObject(optsDecodeMap)), {
  83. /*/ Legacy for backwards compatibility */ debounce: 'de' });
  84. export function isRedisInstance(obj) {
  85. if (!obj) {
  86. return false;
  87. }
  88. const redisApi = ['connect', 'disconnect', 'duplicate'];
  89. return redisApi.every(name => typeof obj[name] === 'function');
  90. }
  91. export function isRedisCluster(obj) {
  92. return isRedisInstance(obj) && obj.isCluster;
  93. }
  94. export function decreaseMaxListeners(emitter, count) {
  95. increaseMaxListeners(emitter, -count);
  96. }
  97. export async function removeAllQueueData(client, queueName, prefix = process.env.BULLMQ_TEST_PREFIX || 'bull') {
  98. if (client instanceof Cluster) {
  99. // todo compat with cluster ?
  100. // @see https://github.com/luin/ioredis/issues/175
  101. return Promise.resolve(false);
  102. }
  103. const pattern = `${prefix}:${queueName}:*`;
  104. const pendingOperations = [];
  105. await new Promise((resolve, reject) => {
  106. const stream = client.scanStream({
  107. match: pattern,
  108. });
  109. stream.on('data', (keys) => {
  110. if (keys.length) {
  111. const pipeline = client.pipeline();
  112. keys.forEach(key => {
  113. pipeline.del(key);
  114. });
  115. const execPromise = pipeline.exec().catch(error => {
  116. reject(error);
  117. throw error;
  118. });
  119. pendingOperations.push(execPromise);
  120. }
  121. });
  122. stream.on('end', () => resolve());
  123. stream.on('error', error => reject(error));
  124. });
  125. // Wait for all pipeline operations to complete before closing the connection
  126. await Promise.all(pendingOperations);
  127. // Handle connection close with better error handling for Dragonfly
  128. try {
  129. await client.quit();
  130. }
  131. catch (error) {
  132. if (isNotConnectionError(error)) {
  133. throw error;
  134. }
  135. }
  136. }
  137. export function getParentKey(opts) {
  138. if (opts) {
  139. return `${opts.queue}:${opts.id}`;
  140. }
  141. }
  142. export const clientCommandMessageReg = /ERR unknown command ['`]\s*client\s*['`]/;
  143. export const DELAY_TIME_5 = 5000;
  144. export const DELAY_TIME_1 = 100;
  145. export function isNotConnectionError(error) {
  146. const { code, message: errorMessage } = error;
  147. return (errorMessage !== CONNECTION_CLOSED_ERROR_MSG &&
  148. !errorMessage.includes('ECONNREFUSED') &&
  149. code !== 'ECONNREFUSED');
  150. }
  151. export const asyncSend = (proc, msg) => {
  152. return new Promise((resolve, reject) => {
  153. if (typeof proc.send === 'function') {
  154. proc.send(msg, (err) => {
  155. if (err) {
  156. reject(err);
  157. }
  158. else {
  159. resolve();
  160. }
  161. });
  162. }
  163. else if (typeof proc.postMessage === 'function') {
  164. resolve(proc.postMessage(msg));
  165. }
  166. else {
  167. resolve();
  168. }
  169. });
  170. };
  171. export const childSend = (proc, msg) => asyncSend(proc, msg);
  172. export const isRedisVersionLowerThan = (currentVersion, minimumVersion, currentDatabaseType, desiredDatabaseType = 'redis') => {
  173. if (currentDatabaseType === desiredDatabaseType) {
  174. const version = semver.valid(semver.coerce(currentVersion));
  175. return semver.lt(version, minimumVersion);
  176. }
  177. return false;
  178. };
  179. export const parseObjectValues = (obj) => {
  180. const accumulator = {};
  181. for (const value of Object.entries(obj)) {
  182. accumulator[value[0]] = JSON.parse(value[1]);
  183. }
  184. return accumulator;
  185. };
  186. const getCircularReplacer = (rootReference) => {
  187. const references = new WeakSet();
  188. references.add(rootReference);
  189. return (_, value) => {
  190. if (typeof value === 'object' && value !== null) {
  191. if (references.has(value)) {
  192. return '[Circular]';
  193. }
  194. references.add(value);
  195. }
  196. return value;
  197. };
  198. };
  199. export const errorToJSON = (value) => {
  200. const error = {};
  201. Object.getOwnPropertyNames(value).forEach(function (propName) {
  202. error[propName] = value[propName];
  203. });
  204. return JSON.parse(JSON.stringify(error, getCircularReplacer(value)));
  205. };
  206. const INFINITY = 1 / 0;
  207. export const toString = (value) => {
  208. if (value == null) {
  209. return '';
  210. }
  211. // Exit early for strings to avoid a performance hit in some environments.
  212. if (typeof value === 'string') {
  213. return value;
  214. }
  215. if (Array.isArray(value)) {
  216. // Recursively convert values (susceptible to call stack limits).
  217. return `${value.map(other => (other == null ? other : toString(other)))}`;
  218. }
  219. if (typeof value == 'symbol' ||
  220. Object.prototype.toString.call(value) == '[object Symbol]') {
  221. return value.toString();
  222. }
  223. const result = `${value}`;
  224. return result === '0' && 1 / value === -INFINITY ? '-0' : result;
  225. };
  226. export const QUEUE_EVENT_SUFFIX = ':qe';
  227. export function removeUndefinedFields(obj) {
  228. const newObj = {};
  229. for (const key in obj) {
  230. if (obj[key] !== undefined) {
  231. newObj[key] = obj[key];
  232. }
  233. }
  234. return newObj;
  235. }
  236. /**
  237. * Wraps the code with telemetry and provides a span for configuration.
  238. *
  239. * @param telemetry - telemetry configuration. If undefined, the callback will be executed without telemetry.
  240. * @param spanKind - kind of the span: Producer, Consumer, Internal
  241. * @param queueName - queue name
  242. * @param operation - operation name (such as add, process, etc)
  243. * @param destination - destination name (normally the queue name)
  244. * @param callback - code to wrap with telemetry
  245. * @param srcPropagationMetadata -
  246. * @returns
  247. */
  248. export async function trace(telemetry, spanKind, queueName, operation, destination, callback, srcPropagationMetadata) {
  249. if (!telemetry) {
  250. return callback();
  251. }
  252. else {
  253. const { tracer, contextManager } = telemetry;
  254. const currentContext = contextManager.active();
  255. let parentContext;
  256. if (srcPropagationMetadata) {
  257. parentContext = contextManager.fromMetadata(currentContext, srcPropagationMetadata);
  258. }
  259. const spanName = destination ? `${operation} ${destination}` : operation;
  260. const span = tracer.startSpan(spanName, {
  261. kind: spanKind,
  262. }, parentContext);
  263. try {
  264. span.setAttributes({
  265. [TelemetryAttributes.QueueName]: queueName,
  266. [TelemetryAttributes.QueueOperation]: operation,
  267. });
  268. let messageContext;
  269. let dstPropagationMetadata;
  270. if (spanKind === SpanKind.CONSUMER && parentContext) {
  271. messageContext = span.setSpanOnContext(parentContext);
  272. }
  273. else {
  274. messageContext = span.setSpanOnContext(currentContext);
  275. }
  276. if (callback.length == 2) {
  277. dstPropagationMetadata = contextManager.getMetadata(messageContext);
  278. }
  279. return await contextManager.with(messageContext, () => callback(span, dstPropagationMetadata));
  280. }
  281. catch (err) {
  282. span.recordException(err);
  283. throw err;
  284. }
  285. finally {
  286. span.end();
  287. }
  288. }
  289. }
  290. //# sourceMappingURL=index.js.map