@@ -75,21 +75,25 @@ class RedisQueueDriverConnection {
75
75
}
76
76
77
77
async setResultAndRemoveQuery ( queryKey , executionResult , processingId ) {
78
- await this . redisClient . watchAsync ( this . queryProcessingLockKey ( queryKey ) ) ;
79
- const currentProcessId = await this . redisClient . getAsync ( this . queryProcessingLockKey ( queryKey ) ) ;
80
- if ( processingId !== currentProcessId ) {
81
- return false ;
82
- }
78
+ try {
79
+ await this . redisClient . watchAsync ( this . queryProcessingLockKey ( queryKey ) ) ;
80
+ const currentProcessId = await this . redisClient . getAsync ( this . queryProcessingLockKey ( queryKey ) ) ;
81
+ if ( processingId !== currentProcessId ) {
82
+ return false ;
83
+ }
83
84
84
- return this . redisClient . multi ( )
85
- . lpush ( [ this . resultListKey ( queryKey ) , JSON . stringify ( executionResult ) ] )
86
- . zrem ( [ this . activeRedisKey ( ) , this . redisHash ( queryKey ) ] )
87
- . zrem ( [ this . heartBeatRedisKey ( ) , this . redisHash ( queryKey ) ] )
88
- . zrem ( [ this . toProcessRedisKey ( ) , this . redisHash ( queryKey ) ] )
89
- . zrem ( [ this . recentRedisKey ( ) , this . redisHash ( queryKey ) ] )
90
- . hdel ( [ this . queriesDefKey ( ) , this . redisHash ( queryKey ) ] )
91
- . del ( this . queryProcessingLockKey ( queryKey ) )
92
- . execAsync ( ) ;
85
+ return this . redisClient . multi ( )
86
+ . lpush ( [ this . resultListKey ( queryKey ) , JSON . stringify ( executionResult ) ] )
87
+ . zrem ( [ this . activeRedisKey ( ) , this . redisHash ( queryKey ) ] )
88
+ . zrem ( [ this . heartBeatRedisKey ( ) , this . redisHash ( queryKey ) ] )
89
+ . zrem ( [ this . toProcessRedisKey ( ) , this . redisHash ( queryKey ) ] )
90
+ . zrem ( [ this . recentRedisKey ( ) , this . redisHash ( queryKey ) ] )
91
+ . hdel ( [ this . queriesDefKey ( ) , this . redisHash ( queryKey ) ] )
92
+ . del ( this . queryProcessingLockKey ( queryKey ) )
93
+ . execAsync ( ) ;
94
+ } finally {
95
+ await this . redisClient . unwatchAsync ( ) ;
96
+ }
93
97
}
94
98
95
99
getOrphanedQueries ( ) {
@@ -144,43 +148,51 @@ class RedisQueueDriverConnection {
144
148
}
145
149
146
150
async freeProcessingLock ( queryKey , processingId , activated ) {
147
- const lockKey = this . queryProcessingLockKey ( queryKey ) ;
148
- await this . redisClient . watchAsync ( lockKey ) ;
149
- const currentProcessId = await this . redisClient . getAsync ( lockKey ) ;
150
- if ( currentProcessId === processingId ) {
151
- let removeCommand = this . redisClient . multi ( )
152
- . del ( lockKey ) ;
153
- if ( activated ) {
154
- removeCommand = removeCommand . zrem ( [ this . activeRedisKey ( ) , this . redisHash ( queryKey ) ] ) ;
151
+ try {
152
+ const lockKey = this . queryProcessingLockKey ( queryKey ) ;
153
+ await this . redisClient . watchAsync ( lockKey ) ;
154
+ const currentProcessId = await this . redisClient . getAsync ( lockKey ) ;
155
+ if ( currentProcessId === processingId ) {
156
+ let removeCommand = this . redisClient . multi ( )
157
+ . del ( lockKey ) ;
158
+ if ( activated ) {
159
+ removeCommand = removeCommand . zrem ( [ this . activeRedisKey ( ) , this . redisHash ( queryKey ) ] ) ;
160
+ }
161
+ await removeCommand
162
+ . execAsync ( ) ;
155
163
}
156
- await removeCommand
157
- . execAsync ( ) ;
164
+ } finally {
165
+ await this . redisClient . unwatchAsync ( ) ;
158
166
}
159
167
}
160
168
161
169
async optimisticQueryUpdate ( queryKey , toUpdate , processingId ) {
162
- let query = await this . getQueryDef ( queryKey ) ;
163
- for ( let i = 0 ; i < 10 ; i ++ ) {
164
- if ( query ) {
165
- // eslint-disable-next-line no-await-in-loop
166
- await this . redisClient . watchAsync ( this . queryProcessingLockKey ( queryKey ) ) ;
167
- const currentProcessId = await this . redisClient . getAsync ( this . queryProcessingLockKey ( queryKey ) ) ;
168
- if ( currentProcessId !== processingId ) {
169
- return false ;
170
- }
171
- let [ beforeUpdate ] = await this . redisClient
172
- . multi ( )
173
- . hget ( [ this . queriesDefKey ( ) , this . redisHash ( queryKey ) ] )
174
- . hset ( [ this . queriesDefKey ( ) , this . redisHash ( queryKey ) , JSON . stringify ( { ...query , ...toUpdate } ) ] )
175
- . execAsync ( ) ;
176
- beforeUpdate = JSON . parse ( beforeUpdate ) ;
177
- if ( JSON . stringify ( query ) === JSON . stringify ( beforeUpdate ) ) {
178
- return true ;
170
+ try {
171
+ let query = await this . getQueryDef ( queryKey ) ;
172
+ for ( let i = 0 ; i < 10 ; i ++ ) {
173
+ if ( query ) {
174
+ // eslint-disable-next-line no-await-in-loop
175
+ await this . redisClient . watchAsync ( this . queryProcessingLockKey ( queryKey ) ) ;
176
+ const currentProcessId = await this . redisClient . getAsync ( this . queryProcessingLockKey ( queryKey ) ) ;
177
+ if ( currentProcessId !== processingId ) {
178
+ return false ;
179
+ }
180
+ let [ beforeUpdate ] = await this . redisClient
181
+ . multi ( )
182
+ . hget ( [ this . queriesDefKey ( ) , this . redisHash ( queryKey ) ] )
183
+ . hset ( [ this . queriesDefKey ( ) , this . redisHash ( queryKey ) , JSON . stringify ( { ...query , ...toUpdate } ) ] )
184
+ . execAsync ( ) ;
185
+ beforeUpdate = JSON . parse ( beforeUpdate ) ;
186
+ if ( JSON . stringify ( query ) === JSON . stringify ( beforeUpdate ) ) {
187
+ return true ;
188
+ }
189
+ query = beforeUpdate ;
179
190
}
180
- query = beforeUpdate ;
181
191
}
192
+ throw new Error ( `Can't update ${ queryKey } with ${ JSON . stringify ( toUpdate ) } ` ) ;
193
+ } finally {
194
+ await this . redisClient . unwatchAsync ( ) ;
182
195
}
183
- throw new Error ( `Can't update ${ queryKey } with ${ JSON . stringify ( toUpdate ) } ` ) ;
184
196
}
185
197
186
198
release ( ) {
0 commit comments