@qingfu/core-env 0.3.8 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/queue.js +70 -3
  2. package/package.json +4 -3
package/lib/queue.js CHANGED
@@ -7,14 +7,14 @@ if ( process.env.QUEUE_URI ) {
7
7
  global.Queue = module.exports = ( name, options ) => {
8
8
  let queue = new Bull( name, process.env.QUEUE_URI, _.defaultsDeep( options, defaults ));
9
9
  queue.on( 'error', err => {
10
- logger.error( Object.assign( err, { queue: name }));
10
+ logger.error( err, { queue: name });
11
11
  });
12
12
  queue.on( 'failed', ( job, err ) => {
13
- logger.error( Object.assign( err, { queue: name, data: job.data }));
13
+ logger.error( err, { queue: name, data: job.data });
14
14
  });
15
15
  queue.close = _.wrap( queue.close, function( func, ...args ) {
16
16
  _.pull( list, this );
17
- func.apply( this, args );
17
+ return func.apply( this, args );
18
18
  });
19
19
  list.push( queue );
20
20
  return queue;
@@ -25,3 +25,70 @@ if ( process.env.QUEUE_URI ) {
25
25
  };
26
26
 
27
27
  }
28
+
29
+ if ( process.env.BULLMQ_URI ) {
30
+
31
+ const { parseURL } = require( 'ioredis/built/utils' );
32
+ const { Queue, Worker } = require( 'bullmq' );
33
+ const queues = [], workers = [], flows = [];
34
+ const DEFAULT_PREFIX = 'bullmq', DEFAULT_NAME = 'default name';
35
+
36
+ global.BullQueue = _.memoize(( name, options ) => {
37
+ let connection = parseURL( process.env.BULLMQ_URI );
38
+ let queue = new Queue( name, { connection, prefix: DEFAULT_PREFIX, ...options });
39
+ queue.add = _.wrap( queue.add, function( func, ...args ) {
40
+ if ( !_.isString( args[ 0 ])) args = [ DEFAULT_NAME, ...args ];
41
+ let cron = _.get( _.last( args ), 'repeat.cron' );
42
+ if ( cron ) {
43
+ _.set( _.last( args ), 'repeat.pattern', cron );
44
+ _.set( _.last( args ), 'repeat.cron', null );
45
+ }
46
+ return func.apply( this, args );
47
+ });
48
+ queue.addBulk = _.wrap( queue.addBulk, function( func, ...args ) {
49
+ if ( _.isArray( args[ 0 ])) args[ 0 ] = args[ 0 ]
50
+ .map( v => _.defaults( v, { name: DEFAULT_NAME }));
51
+ return func.apply( this, args );
52
+ });
53
+ queue.close = _.wrap( queue.close, function( func, ...args ) {
54
+ _.pull( queues, this );
55
+ return func.apply( this, args );
56
+ });
57
+ queues.push( queue );
58
+ return queue;
59
+ });
60
+
61
+ global.BullWorker = ( name, func, options ) => {
62
+ let connection = parseURL( process.env.BULLMQ_URI );
63
+ let defaults = { removeOnComplete: { count: 0 }, removeOnFail: { count: 0 }};
64
+ let worker = new Worker( name, func, { connection, prefix: DEFAULT_PREFIX, ...defaults, ...options });
65
+ worker.on( 'error', err => {
66
+ logger.error( err, { queue: name });
67
+ });
68
+ worker.on( 'failed', ( job, err ) => {
69
+ logger.error( err, { queue: name, data: job.data });
70
+ });
71
+ worker.close = _.wrap( worker.close, function( func, ...args ) {
72
+ _.pull( workers, this );
73
+ return func.apply( this, args );
74
+ });
75
+ workers.push( worker );
76
+ return worker;
77
+ };
78
+
79
+ global.BullFlow = options => {
80
+ let connection = parseURL( process.env.BULLMQ_URI );
81
+ let flow = new FlowProducer({ connection, prefix: DEFAULT_PREFIX, ...options });
82
+ flow.close = _.wrap( flow.close, function( func, ...args ) {
83
+ _.pull( flows, this );
84
+ return func.apply( this, args );
85
+ });
86
+ flows.push( flow );
87
+ return flow;
88
+ };
89
+
90
+ exports.stopPromise = () => {
91
+ return Promise.all([ ...queues, ...workers, ...flows ].map( v => v.close() ));
92
+ };
93
+
94
+ }
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@qingfu/core-env",
3
- "version": "0.3.8",
3
+ "version": "0.4.1",
4
4
  "readmeFilename": "README.md",
5
5
  "dependencies": {
6
6
  "@koa/cors": "5.0.0",
7
7
  "@koa/router": "12.0.1",
8
8
  "base-x": "4.0.0",
9
- "bull": "4.12.9",
9
+ "bull": "4.15.1",
10
+ "bullmq": "5.10.3",
10
11
  "busboy": "1.6.0",
11
12
  "cookie": "0.6.0",
12
13
  "dotenv-extended": "2.9.0",
@@ -36,7 +37,7 @@
36
37
  "qr-image": "3.2.0",
37
38
  "superagent": "8.1.2",
38
39
  "wechat-encrypt": "1.1.1",
39
- "winston": "3.13.0",
40
+ "winston": "3.13.1",
40
41
  "winston-mongodb": "5.1.1",
41
42
  "xlsx": "0.18.5",
42
43
  "xml2js": "0.4.23"