|
| 1 | +const R = require('ramda'); |
| 2 | +const BaseQueueDriver = require('./BaseQueueDriver'); |
| 3 | + |
| 4 | +class LocalQueueDriverConnection { |
| 5 | + constructor(driver, options) { |
| 6 | + this.redisQueuePrefix = options.redisQueuePrefix; |
| 7 | + this.continueWaitTimeout = options.continueWaitTimeout; |
| 8 | + this.orphanedTimeout = options.orphanedTimeout; |
| 9 | + this.heartBeatTimeout = options.heartBeatTimeout; |
| 10 | + this.concurrency = options.concurrency; |
| 11 | + this.driver = driver; |
| 12 | + this.results = driver.results; |
| 13 | + this.resultPromises = driver.resultPromises; |
| 14 | + this.queryDef = driver.queryDef; |
| 15 | + this.toProcess = driver.toProcess; |
| 16 | + this.recent = driver.recent; |
| 17 | + this.active = driver.active; |
| 18 | + } |
| 19 | + |
| 20 | + getResultPromise(resultListKey) { |
| 21 | + if (!this.resultPromises[resultListKey]) { |
| 22 | + let resolveMethod = null; |
| 23 | + this.resultPromises[resultListKey] = new Promise(resolve => { |
| 24 | + resolveMethod = resolve; |
| 25 | + }); |
| 26 | + this.resultPromises[resultListKey].resolve = resolveMethod; |
| 27 | + } |
| 28 | + return this.resultPromises[resultListKey]; |
| 29 | + } |
| 30 | + |
| 31 | + async getResultBlocking(queryKey, continueWaitTimeout) { |
| 32 | + const resultListKey = this.resultListKey(queryKey); |
| 33 | + const timeoutPromise = (timeout) => new Promise((resolve) => setTimeout(() => resolve(null), timeout)); |
| 34 | + |
| 35 | + const res = await Promise.race([ |
| 36 | + this.getResultPromise(resultListKey), |
| 37 | + timeoutPromise(continueWaitTimeout || this.continueWaitTimeout * 1000), |
| 38 | + ]); |
| 39 | + if (res) { |
| 40 | + delete this.resultPromises[resultListKey]; |
| 41 | + } |
| 42 | + return res; |
| 43 | + } |
| 44 | + |
| 45 | + async getResult(queryKey) { |
| 46 | + const resultListKey = this.resultListKey(queryKey); |
| 47 | + if (this.resultPromises[resultListKey]) { |
| 48 | + return this.getResultBlocking(queryKey, 5); |
| 49 | + } |
| 50 | + return null; |
| 51 | + } |
| 52 | + |
| 53 | + queueArray(queueObj, orderFilterLessThan) { |
| 54 | + return R.pipe( |
| 55 | + R.values, |
| 56 | + R.filter(orderFilterLessThan ? q => q.order < orderFilterLessThan : R.identity), |
| 57 | + R.sortBy(q => q.order), |
| 58 | + R.map(q => q.key) |
| 59 | + )(queueObj); |
| 60 | + } |
| 61 | + |
| 62 | + addToQueue(keyScore, queryKey, time, queryHandler, query, priority, options) { |
| 63 | + const queryQueueObj = { |
| 64 | + queryHandler, query, queryKey, stageQueryKey: options.stageQueryKey, priority |
| 65 | + }; |
| 66 | + const key = this.redisHash(queryKey); |
| 67 | + if (!this.queryDef[key]) { |
| 68 | + this.queryDef[key] = queryQueueObj; |
| 69 | + } |
| 70 | + let added = 0; |
| 71 | + if (!this.toProcess[key]) { |
| 72 | + this.toProcess[key] = { |
| 73 | + order: keyScore, |
| 74 | + key |
| 75 | + }; |
| 76 | + added = 1; |
| 77 | + } |
| 78 | + this.recent[key] = { order: time, key }; |
| 79 | + |
| 80 | + return [added, null, null, Object.keys(this.toProcess).length]; // TODO nulls |
| 81 | + } |
| 82 | + |
| 83 | + getToProcessQueries() { |
| 84 | + return this.queueArray(this.toProcess); |
| 85 | + } |
| 86 | + |
| 87 | + getActiveQueries() { |
| 88 | + return this.queueArray(this.active); |
| 89 | + } |
| 90 | + |
| 91 | + async getQueryAndRemove(queryKey) { |
| 92 | + const key = this.redisHash(queryKey); |
| 93 | + const query = this.queryDef[key]; |
| 94 | + delete this.active[key]; |
| 95 | + delete this.toProcess[key]; |
| 96 | + delete this.recent[key]; |
| 97 | + delete this.queryDef[key]; |
| 98 | + return [query]; |
| 99 | + } |
| 100 | + |
| 101 | + setResultAndRemoveQuery(queryKey, executionResult) { |
| 102 | + const key = this.redisHash(queryKey); |
| 103 | + const promise = this.getResultPromise(this.resultListKey(queryKey)); |
| 104 | + delete this.active[key]; |
| 105 | + delete this.toProcess[key]; |
| 106 | + delete this.recent[key]; |
| 107 | + delete this.queryDef[key]; |
| 108 | + promise.resolve(executionResult); |
| 109 | + } |
| 110 | + |
| 111 | + removeQuery(queryKey) { |
| 112 | + const key = this.redisHash(queryKey); |
| 113 | + delete this.active[key]; |
| 114 | + delete this.toProcess[key]; |
| 115 | + delete this.recent[key]; |
| 116 | + delete this.queryDef[key]; |
| 117 | + } |
| 118 | + |
| 119 | + getOrphanedQueries() { |
| 120 | + return this.queueArray(this.recent, new Date().getTime() - this.orphanedTimeout * 1000); |
| 121 | + } |
| 122 | + |
| 123 | + getStalledQueries() { |
| 124 | + return this.queueArray(this.active, new Date().getTime() - this.heartBeatTimeout * 1000); |
| 125 | + } |
| 126 | + |
| 127 | + async getQueryStageState() { |
| 128 | + return [this.queueArray(this.active), this.queueArray(this.toProcess), R.clone(this.queryDef)]; |
| 129 | + } |
| 130 | + |
| 131 | + async getQueryDef(queryKey) { |
| 132 | + return this.queryDef[this.redisHash(queryKey)]; |
| 133 | + } |
| 134 | + |
| 135 | + updateHeartBeat(queryKey) { |
| 136 | + const key = this.redisHash(queryKey); |
| 137 | + if (this.active[key]) { |
| 138 | + this.active[key] = { key, order: new Date().getTime() }; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + retrieveForProcessing(queryKey) { |
| 143 | + const key = this.redisHash(queryKey); |
| 144 | + let added = 0; |
| 145 | + if (Object.keys(this.active).length < this.concurrency && !this.active[key]) { |
| 146 | + this.active[key] = { key, order: new Date().getTime() }; |
| 147 | + added = 1; |
| 148 | + } |
| 149 | + return [added, null, this.queueArray(this.active), Object.keys(this.toProcess).length]; // TODO nulls |
| 150 | + } |
| 151 | + |
| 152 | + async optimisticQueryUpdate(queryKey, toUpdate) { |
| 153 | + const key = this.redisHash(queryKey); |
| 154 | + this.queryDef[key] = { ...this.queryDef[key], ...toUpdate }; |
| 155 | + } |
| 156 | + |
| 157 | + release() { |
| 158 | + } |
| 159 | + |
| 160 | + queryRedisKey(queryKey, suffix) { |
| 161 | + return `${this.redisQueuePrefix}_${this.redisHash(queryKey)}_${suffix}`; |
| 162 | + } |
| 163 | + |
| 164 | + resultListKey(queryKey) { |
| 165 | + return this.queryRedisKey(queryKey, 'RESULT'); |
| 166 | + } |
| 167 | + |
| 168 | + redisHash(queryKey) { |
| 169 | + return this.driver.redisHash(queryKey); |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +class LocalQueueDriver extends BaseQueueDriver { |
| 174 | + constructor(options) { |
| 175 | + super(); |
| 176 | + this.options = options; |
| 177 | + this.results = {}; |
| 178 | + this.resultPromises = {}; |
| 179 | + this.queryDef = {}; |
| 180 | + this.toProcess = {}; |
| 181 | + this.recent = {}; |
| 182 | + this.active = {}; |
| 183 | + } |
| 184 | + |
| 185 | + createConnection() { |
| 186 | + return new LocalQueueDriverConnection(this, this.options); |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +module.exports = LocalQueueDriver; |
0 commit comments