@@ -66,6 +66,14 @@ export class Sequencer<Modules extends SequencerModulesRecord>
66
66
* modules to start each
67
67
*/
68
68
public async start ( ) {
69
+ // The sequencer uses Tysringe to resolve modules (and their dependencies)
70
+ // and then starts them. However, this can be problematic as although Tysringe may resolve
71
+ // dependencies, it doesn't actually start them. For example, a database may be created,
72
+ // but the connection strings, etc, won't be constructed until it's started, and this may
73
+ // cause an error if a module that relies on it is started first. The way to fix this is
74
+ // ensure that we start modules based on the order they were resolved.
75
+ // We iterate through the methods three times:
76
+
69
77
this . useDependencyFactory ( this . container . resolve ( MethodIdFactory ) ) ;
70
78
71
79
// Log startup info
@@ -75,6 +83,8 @@ export class Sequencer<Modules extends SequencerModulesRecord>
75
83
log . info ( "Starting sequencer..." ) ;
76
84
log . info ( "Modules:" , moduleClassNames ) ;
77
85
86
+ // Iteration #1: We invoke the afterResolution feature for the container
87
+ // to ensure every time a module is resolved it gets recorded.
78
88
const orderedModules : Extract < keyof Modules , string > [ ] = [ ] ;
79
89
// eslint-disable-next-line guard-for-in
80
90
for ( const moduleName in this . definition . modules ) {
@@ -88,6 +98,8 @@ export class Sequencer<Modules extends SequencerModulesRecord>
88
98
}
89
99
) ;
90
100
}
101
+ // Iteration #2: We resolve each module and thus populate
102
+ // the orderedModules list to understand the sequencing.
91
103
// eslint-disable-next-line guard-for-in
92
104
for ( const moduleName in this . definition . modules ) {
93
105
const sequencerModule = this . resolve ( moduleName ) ;
@@ -96,6 +108,8 @@ export class Sequencer<Modules extends SequencerModulesRecord>
96
108
) ;
97
109
}
98
110
111
+ // Iteration #3: We now iterate though the orderedModules list
112
+ // and start the modules in the order they were resolved.
99
113
for ( const moduleName of orderedModules ) {
100
114
const sequencerModule = this . resolve ( moduleName ) ;
101
115
// eslint-disable-next-line no-await-in-loop
0 commit comments