|
| 1 | +// (c) Copyright 2022, SAP SE and ClearlyDefined contributors. Licensed under the MIT license. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +const NestedQueue = require('./nestedQueue') |
| 5 | + |
| 6 | +const VISIBILITY_TIMEOUT_TO_REMAIN_ON_LOCAL_QUEUE = 8 * 60 * 60 // 8 hours |
| 7 | +const VISIBILITY_TIMEOUT_FOR_PROCESSING = 1 * 60 * 60 // 1 hours, similar to storage queue pop visibility timeout |
| 8 | + |
| 9 | +class StorageBackedQueue extends NestedQueue { |
| 10 | + |
| 11 | + constructor(queue, storageQueue, options) { |
| 12 | + super(queue) |
| 13 | + this.options = options |
| 14 | + this.logger = options.logger |
| 15 | + this._sharedStorageQueue = storageQueue |
| 16 | + } |
| 17 | + |
| 18 | + async push(requests) { |
| 19 | + requests = Array.isArray(requests) ? requests : [requests] |
| 20 | + await this._pushToStorage(requests) |
| 21 | + await super.push(requests) |
| 22 | + } |
| 23 | + |
| 24 | + async _pushToStorage(requests) { |
| 25 | + const visibilityTimeout = this.options.visibilityTimeout_remainLocal |
| 26 | + const storageReceipts = await this._sharedStorageQueue.push(requests, { visibilityTimeout }) |
| 27 | + requests.map((request, index) => Object.assign(request, storageReceipts[index])) |
| 28 | + } |
| 29 | + |
| 30 | + async pop() { |
| 31 | + const request = await super.pop() |
| 32 | + if (!request) return |
| 33 | + const success = await this._hideInStorage(request) |
| 34 | + if (success) return request |
| 35 | + return await this.pop() |
| 36 | + } |
| 37 | + |
| 38 | + async _hideInStorage(request) { |
| 39 | + try { |
| 40 | + const receipt = await this._sharedStorageQueue.updateVisibilityTimeout(request, this.options.visibilityTimeout) |
| 41 | + Object.assign(request, receipt) |
| 42 | + return true |
| 43 | + } catch (error) { |
| 44 | + if (!this._sharedStorageQueue.isMessageNotFound(error)) throw error |
| 45 | + // Message not found for the popReceipt and messageId stored in the request. This means |
| 46 | + // that the message popReceipt (and possibly messageId) in the request is stale. This can |
| 47 | + // happen when the message visibility timeout expired and thus was visible and later |
| 48 | + // updated/processed by others. Because the request is picked up by others, just log and |
| 49 | + // continue to the next. |
| 50 | + this._log('Failed to update stale message', request) |
| 51 | + return false |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + async done(request) { |
| 56 | + await Promise.all([ |
| 57 | + super.done(request), |
| 58 | + this._doneInStorage(request)]) |
| 59 | + } |
| 60 | + |
| 61 | + async _doneInStorage(request) { |
| 62 | + try { |
| 63 | + await this._sharedStorageQueue.done(request) |
| 64 | + } catch (error) { |
| 65 | + if (!this._sharedStorageQueue.isMessageNotFound(error)) throw error |
| 66 | + // Message not found for the popReceipt and messageId stored in the request. This means |
| 67 | + // that the message popReceipt (and possibly messageId) in the request is stale. This can |
| 68 | + // happen when the message visibility timeout expired and thus was visible and later |
| 69 | + // updated by others. This is ok because the deletion of the request can be left to the |
| 70 | + // caller with the updated popReceipt. Log and continue. |
| 71 | + this._log('Failed to remove stale message', request) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + async subscribe() { |
| 76 | + await Promise.all([ |
| 77 | + super.subscribe(), |
| 78 | + this._sharedStorageQueue.subscribe()]) |
| 79 | + } |
| 80 | + |
| 81 | + async unsubscribe() { |
| 82 | + const results = await Promise.allSettled([ |
| 83 | + super.unsubscribe(), |
| 84 | + this._sharedStorageQueue.unsubscribe()]) |
| 85 | + this._throwIfError(results, 'Failed to unsubscribe') |
| 86 | + } |
| 87 | + |
| 88 | + async flush() { |
| 89 | + const deleteRequests = [] |
| 90 | + const info = await this.getInfo() |
| 91 | + for (let count = info.count; count > 0; count--) { |
| 92 | + const deleteOne = super.pop().then(request => this.done(request)) |
| 93 | + deleteRequests.push(deleteOne) |
| 94 | + } |
| 95 | + const results = await Promise.allSettled(deleteRequests) |
| 96 | + this._throwIfError(results, 'Failed to flush') |
| 97 | + } |
| 98 | + |
| 99 | + _throwIfError(results, message) { |
| 100 | + const errors = results.filter(result => result.status === 'rejected') |
| 101 | + .map(rejected => new Error(rejected.reason)) |
| 102 | + if (errors.length) throw new AggregateError(errors, message) |
| 103 | + } |
| 104 | + |
| 105 | + _log(actionMessage, request) { |
| 106 | + this.logger.verbose(`${actionMessage} ${request.type} ${request.url}`) |
| 107 | + } |
| 108 | + |
| 109 | + static create(queue, storageQueue, options = {}) { |
| 110 | + const defaultOptions = { |
| 111 | + visibilityTimeout_remainLocal: VISIBILITY_TIMEOUT_TO_REMAIN_ON_LOCAL_QUEUE, |
| 112 | + visibilityTimeout: VISIBILITY_TIMEOUT_FOR_PROCESSING |
| 113 | + } |
| 114 | + const optionsWithDefaults = { ...defaultOptions, ...options } |
| 115 | + return new StorageBackedQueue(queue, storageQueue, optionsWithDefaults) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +module.exports = StorageBackedQueue |
0 commit comments