@@ -4,38 +4,57 @@ import { getMongoose } from './mongoose.js';
44import { getLogger } from '../utils/logger.js' ;
55
66const { chainId } = chain ;
7-
87const logger = getLogger ( 'agenda' ) ;
98
109let agendaPromise ;
1110
11+ /**
12+ * Initializes and returns a singleton Agenda instance.
13+ *
14+ * This uses a MongoDB URI extracted from a Mongoose v8 connection,
15+ * ensuring full compatibility with Agenda's expected configuration shape.
16+ *
17+ * Notes on Mongoose v8 compatibility:
18+ * - Mongoose v8 removed global buffering; `bufferCommands: false` is used in the loader.
19+ * - `mongoose.createConnection()` returns a `Connection` instance.
20+ * - The native MongoClient URI is available at `connection.db.client.s.url`.
21+ * - This URI is passed directly to Agenda's `db.address` option.
22+ */
1223const getAgenda = async ( ) => {
13- if ( agendaPromise ) {
14- return agendaPromise ;
15- }
24+ if ( agendaPromise ) return agendaPromise ;
25+
1626 agendaPromise = new Promise ( ( resolve , reject ) => {
17- logger . log ( 'connecting ' ) ;
27+ logger . log ( 'Connecting to Mongo... ' ) ;
1828 getMongoose ( { db : `${ chainId } _jobs` } )
1929 . then ( async ( mongooseConnection ) => {
20- const { db } = mongooseConnection ;
21- const agenda = new Agenda ( { mongo : db } ) ;
22- logger . log ( 'starting worker' ) ;
23- agenda
24- . start ( )
25- . then ( ( ) => {
26- logger . log ( 'running' ) ;
27- resolve ( agenda ) ;
28- } )
29- . catch ( ( e ) => {
30- logger . warn ( 'start failed' , e ) ;
31- reject ( e ) ;
32- } ) ;
30+ // Passing Native db dosn't go through anymore preventing agenda from running and it is blocked at index creation level
31+ // Extract raw Mongo URI from Mongoose connection (v8-safe)
32+ const uri = mongooseConnection . db . client . s . url ;
33+
34+ // Create Agenda instance with URI and disable auto-indexing to avoid blocking on start
35+ const agenda = new Agenda ( {
36+ db : {
37+ address : uri ,
38+ disableAutoIndex : true ,
39+ }
40+ } ) ;
41+
42+ logger . log ( 'Starting Agenda worker...' ) ;
43+ try {
44+ await agenda . start ( ) ;
45+ logger . log ( 'Agenda is running ✅' ) ;
46+ resolve ( agenda ) ;
47+ } catch ( e ) {
48+ logger . warn ( 'Agenda start failed' , e ) ;
49+ reject ( e ) ;
50+ }
3351 } )
3452 . catch ( ( e ) => {
35- logger . warn ( 'getAgenda()' , e ) ;
53+ logger . warn ( 'getAgenda() failed ' , e ) ;
3654 reject ( e ) ;
3755 } ) ;
3856 } ) ;
57+
3958 return agendaPromise ;
4059} ;
4160
0 commit comments