-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
bbiq.ts
124 lines (111 loc) · 3.74 KB
/
bbiq.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Copyright (C) 2023 David Kellner
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
import {ImportQueue, type ImportQueueOptions} from './queue.ts';
import config from './helpers/config.ts';
import consumerPromise from './consumer/consumer.ts';
// eslint-disable-next-line import/no-internal-modules
import {hideBin} from 'yargs/helpers';
import log from './helpers/logger.ts';
import process from 'node:process';
import yargs from 'yargs';
function createQueue({connection, test, failureQueue, queue}: BBIQArguments) {
const queueOptions: Partial<ImportQueueOptions> = {
connectionUrl: connection || config.queue?.connection,
failureQueue: failureQueue || false,
isPersistent: !test,
queueName: queue
};
if (test && !queue) {
// AMQP does not allow us to re-declare the same default queue `bookbrainz-import` as non-persistent
queueOptions.queueName = 'bookbrainz-import-test';
}
return new ImportQueue(queueOptions);
}
async function useQueue(
queue: ImportQueue,
task: (queue: ImportQueue) => Promise<void>,
errorMessage = 'Failed to use queue'
) {
let exitCode = 0;
try {
const queueInfo = await queue.open();
log.info('Import queue has been opened:', queueInfo);
await task(queue);
}
catch (error) {
log.error(`${errorMessage}:`, error);
exitCode = 1;
}
finally {
if (await queue.close()) {
log.debug('Import queue has been closed');
}
}
return exitCode;
}
const {argv} = yargs(hideBin(process.argv))
.scriptName('bbiq')
.usage('BookBrainz Import Queue management\nUsage: $0 <command>')
.help()
.alias('help', 'h')
.wrap(100)
.option('connection', {
alias: 'c',
describe: 'Connection URL to an AMQP server (overrides config file)',
type: 'string'
})
.option('queue', {
alias: 'q',
describe: 'Name of the queue which stores pending imports',
requiresArg: true,
type: 'string'
})
.option('failure-queue', {
alias: 'f',
describe: 'Name of the queue which stores failed imports (discarded by default)',
requiresArg: true,
type: 'string'
})
.option('test', {
alias: 't',
default: false,
describe: 'Use the non-persistent test import queue',
type: 'boolean'
})
.command('consume', 'Await queued entities and insert them into the BB database', {}, (args) => {
const queue = createQueue(args as BBIQArguments);
process.on('SIGINT', async () => {
log.info('Consumer has been terminated');
await queue.close();
log.debug('Import queue has been force-closed');
});
useQueue(queue, (q) => consumerPromise({id: 0, queue: q}), 'Failed to consume queue').then(process.exit);
})
.command('info', 'Show information about the import queue', {}, (args) => {
const queue = createQueue(args as BBIQArguments);
useQueue(queue, () => Promise.resolve()).then(process.exit);
})
.command('purge', 'Drop all entities from the import queue', {}, (args) => {
const queue = createQueue(args as BBIQArguments);
useQueue(queue, async (q) => {
const result = await q.purge();
log.info('Purged messages:', result.messageCount);
}, 'Failed to purge queue').then(process.exit);
})
.demandCommand();
type BBIQArguments = Awaited<typeof argv>;