1
1
const { AsyncLocalStorage } = require ( "node:async_hooks" ) ;
2
2
const knex = require ( "knex" ) ;
3
- const PgBoss = require ( "pg-boss" ) ;
4
3
const util = require ( "util" ) ;
5
4
const AppConfig = require ( "./src/app_config" ) ;
6
5
const ReportProcessingContext = require ( "./src/report_processing_context" ) ;
7
6
const Logger = require ( "./src/logger" ) ;
8
7
const Processor = require ( "./src/processor" ) ;
9
- const PgBossKnexAdapter = require ( "./src/pg_boss_knex_adapter" ) ;
8
+ const Queue = require ( "./src/queue/queue" ) ;
9
+ const ReportJobQueueMessage = require ( "./src/queue/report_job_queue_message" ) ;
10
10
11
11
/**
12
12
* Gets an array of JSON report objects from the application confing, then runs
@@ -37,12 +37,16 @@ async function run(options = {}) {
37
37
const appConfig = new AppConfig ( options ) ;
38
38
const context = new ReportProcessingContext ( new AsyncLocalStorage ( ) ) ;
39
39
const reportConfigs = appConfig . filteredReportConfigurations ;
40
+ const knexInstance = appConfig . shouldWriteToDatabase
41
+ ? await knex ( appConfig . knexConfig )
42
+ : undefined ;
40
43
const processor = Processor . buildAnalyticsProcessor (
41
44
appConfig ,
42
45
Logger . initialize ( {
43
46
agencyName : appConfig . agencyLogName ,
44
47
scriptName : appConfig . scriptName ,
45
48
} ) ,
49
+ knexInstance ,
46
50
) ;
47
51
48
52
for ( const reportConfig of reportConfigs ) {
@@ -124,7 +128,11 @@ async function runQueuePublish(options = {}) {
124
128
scriptName : appConfig . scriptName ,
125
129
} ) ;
126
130
const knexInstance = await knex ( appConfig . knexConfig ) ;
127
- const queueClient = await _initQueueClient ( knexInstance , appLogger ) ;
131
+ const queueClient = await _initQueueClient (
132
+ knexInstance ,
133
+ appConfig . messageQueueName ,
134
+ appLogger ,
135
+ ) ;
128
136
129
137
for ( const agency of agencies ) {
130
138
for ( const reportConfig of reportConfigs ) {
@@ -134,47 +142,35 @@ async function runQueuePublish(options = {}) {
134
142
scriptName : appConfig . scriptName ,
135
143
reportName : reportConfig . name ,
136
144
} ) ;
145
+ let messageId ;
137
146
try {
138
- let jobId = await queueClient . send (
139
- appConfig . messageQueueName ,
140
- _createQueueMessage (
141
- options ,
142
- agency ,
147
+ messageId = await queueClient . sendMessage (
148
+ new ReportJobQueueMessage ( {
149
+ agencyName : agency . agencyName ,
150
+ analyticsReportIds : agency . analyticsReportIds ,
151
+ awsBucketPath : agency . awsBucketPath ,
152
+ reportOptions : options ,
143
153
reportConfig,
144
- appConfig . scriptName ,
145
- ) ,
146
- {
147
- priority : _messagePriority ( reportConfig ) ,
148
- retryLimit : 2 ,
149
- retryDelay : 10 ,
150
- retryBackoff : true ,
151
- singletonKey : `${ appConfig . scriptName } -${ agency . agencyName } -${ reportConfig . name } ` ,
152
- } ,
154
+ scriptName : appConfig . scriptName ,
155
+ } ) ,
153
156
) ;
154
- if ( jobId ) {
157
+ if ( messageId ) {
155
158
reportLogger . info (
156
- `Created job in queue: ${ appConfig . messageQueueName } with job ID: ${ jobId } ` ,
159
+ `Created message in queue: ${ queueClient . name } with message ID: ${ messageId } ` ,
157
160
) ;
158
161
} else {
159
162
reportLogger . info (
160
- `Found a duplicate job in queue: ${ appConfig . messageQueueName } ` ,
163
+ `Found a duplicate message in queue: ${ queueClient . name } ` ,
161
164
) ;
162
165
}
163
166
} catch ( e ) {
164
- reportLogger . error (
165
- `Error sending to queue: ${ appConfig . messageQueueName } ` ,
166
- ) ;
167
- reportLogger . error ( util . inspect ( e ) ) ;
167
+ // Do nothing so that the remaining messages still process.
168
168
}
169
169
}
170
170
}
171
171
172
172
try {
173
173
await queueClient . stop ( ) ;
174
- appLogger . debug ( `Stopping queue client` ) ;
175
- } catch ( e ) {
176
- appLogger . error ( "Error stopping queue client" ) ;
177
- appLogger . error ( util . inspect ( e ) ) ;
178
174
} finally {
179
175
appLogger . debug ( `Destroying database connection pool` ) ;
180
176
knexInstance . destroy ( ) ;
@@ -198,49 +194,29 @@ function _initAgencies(agencies_file) {
198
194
return Array . isArray ( agencies ) ? agencies : legacyAgencies ;
199
195
}
200
196
201
- async function _initQueueClient ( knexInstance , logger ) {
202
- let queueClient ;
203
- try {
204
- queueClient = new PgBoss ( { db : new PgBossKnexAdapter ( knexInstance ) } ) ;
205
- await queueClient . start ( ) ;
206
- logger . debug ( "Starting queue client" ) ;
207
- } catch ( e ) {
208
- logger . error ( "Error starting queue client" ) ;
209
- logger . error ( util . inspect ( e ) ) ;
210
- }
211
-
197
+ async function _initQueueClient ( knexInstance , queueName , logger ) {
198
+ const queueClient = Queue . buildQueue ( {
199
+ knexInstance,
200
+ queueName,
201
+ messageClass : ReportJobQueueMessage ,
202
+ logger,
203
+ } ) ;
204
+ await queueClient . start ( ) ;
212
205
return queueClient ;
213
206
}
214
207
215
- function _createQueueMessage ( options , agency , reportConfig , scriptName ) {
216
- return {
217
- ...agency ,
218
- options,
219
- reportConfig,
220
- scriptName,
221
- } ;
222
- }
223
-
224
- function _messagePriority ( reportConfig ) {
225
- if ( ! reportConfig . frequency ) {
226
- return 0 ;
227
- } else if ( reportConfig . frequency == "daily" ) {
228
- return 1 ;
229
- } else if ( reportConfig . frequency == "hourly" ) {
230
- return 2 ;
231
- } else if ( reportConfig . frequency == "realtime" ) {
232
- return 3 ;
233
- }
234
- }
235
-
236
208
/**
237
209
* @returns {Promise } when the process ends
238
210
*/
239
211
async function runQueueConsume ( ) {
240
212
const appConfig = new AppConfig ( ) ;
241
213
const appLogger = Logger . initialize ( ) ;
242
214
const knexInstance = await knex ( appConfig . knexConfig ) ;
243
- const queueClient = await _initQueueClient ( knexInstance , appLogger ) ;
215
+ const queueClient = await _initQueueClient (
216
+ knexInstance ,
217
+ appConfig . messageQueueName ,
218
+ appLogger ,
219
+ ) ;
244
220
245
221
try {
246
222
const context = new ReportProcessingContext ( new AsyncLocalStorage ( ) ) ;
@@ -250,24 +226,19 @@ async function runQueueConsume() {
250
226
knexInstance ,
251
227
) ;
252
228
253
- await queueClient . work (
254
- appConfig . messageQueueName ,
255
- { newJobCheckIntervalSeconds : 1 } ,
256
- async ( message ) => {
257
- appLogger . info ( "Queue message received" ) ;
258
- process . env . AGENCY_NAME = message . data . agencyName ;
259
- process . env . ANALYTICS_REPORT_IDS = message . data . analyticsReportIds ;
260
- process . env . AWS_BUCKET_PATH = message . data . awsBucketPath ;
261
- process . env . ANALYTICS_SCRIPT_NAME = message . data . scriptName ;
262
-
263
- await _processReport (
264
- new AppConfig ( message . data . options ) ,
265
- context ,
266
- message . data . reportConfig ,
267
- processor ,
268
- ) ;
269
- } ,
270
- ) ;
229
+ await queueClient . poll ( async ( message ) => {
230
+ process . env . AGENCY_NAME = message . agencyName ;
231
+ process . env . ANALYTICS_REPORT_IDS = message . analyticsReportIds ;
232
+ process . env . AWS_BUCKET_PATH = message . awsBucketPath ;
233
+ process . env . ANALYTICS_SCRIPT_NAME = message . scriptName ;
234
+
235
+ await _processReport (
236
+ new AppConfig ( message . options ) ,
237
+ context ,
238
+ message . reportConfig ,
239
+ processor ,
240
+ ) ;
241
+ } ) ;
271
242
} catch ( e ) {
272
243
appLogger . error ( "Error polling queue for messages" ) ;
273
244
appLogger . error ( util . inspect ( e ) ) ;
0 commit comments