Skip to content

Commit 7dbf541

Browse files
committed
feat(api): add detailed logging to server initialization sequence
Instruments the `run` function in `lib/server.dart` with detailed, sequential log messages. This provides clear visibility into the server startup process, from logger setup and database connection to dependency creation and the opening of the request gate. This will aid in debugging initialization-related issues.
1 parent 6013ff8 commit 7dbf541

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

lib/server.dart

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,10 @@ Future<HttpServer> run(Handler handler, InternetAddress ip, int port) async {
8282
'${record.loggerName}: ${record.message}',
8383
);
8484
});
85+
_log.info('[INIT_SEQ] 1. Logger setup complete.');
8586

8687
// 2. Establish Database Connection
87-
_log.info('Connecting to PostgreSQL database...');
88+
_log.info('[INIT_SEQ] 2. Connecting to PostgreSQL database...');
8889
final dbUri = Uri.parse(EnvironmentConfig.databaseUrl);
8990
String? username;
9091
String? password;
@@ -106,18 +107,24 @@ Future<HttpServer> run(Handler handler, InternetAddress ip, int port) async {
106107
),
107108
settings: const ConnectionSettings(sslMode: SslMode.require),
108109
);
109-
_log.info('PostgreSQL database connection established.');
110+
_log.info('[INIT_SEQ] 2. PostgreSQL database connection established.');
110111

111112
// 3. Initialize and run database seeding
113+
_log.info('[INIT_SEQ] 3. Initializing database seeding service...');
112114
final seedingService = DatabaseSeedingService(
113115
connection: _connection,
114116
log: _log,
115117
);
118+
_log.info('[INIT_SEQ] 3. Creating tables if they do not exist...');
116119
await seedingService.createTables();
120+
_log.info('[INIT_SEQ] 3. Seeding global fixture data...');
117121
await seedingService.seedGlobalFixtureData();
122+
_log.info('[INIT_SEQ] 3. Seeding initial admin and config...');
118123
await seedingService.seedInitialAdminAndConfig();
119-
120-
// 4. Initialize Repositories
124+
_log
125+
..info('[INIT_SEQ] 3. Database seeding complete.')
126+
// 4. Initialize Repositories
127+
..info('[INIT_SEQ] 4. Initializing data repositories...');
121128
final headlineRepository = _createRepository<Headline>(
122129
tableName: 'headlines',
123130
fromJson: Headline.fromJson,
@@ -159,8 +166,10 @@ Future<HttpServer> run(Handler handler, InternetAddress ip, int port) async {
159166
fromJson: AppConfig.fromJson,
160167
toJson: (c) => c.toJson(),
161168
);
162-
163-
// 5. Initialize Services
169+
_log
170+
..info('[INIT_SEQ] 4. Data repositories initialized.')
171+
// 5. Initialize Services
172+
..info('[INIT_SEQ] 5. Initializing services...');
164173
const emailRepository = HtEmailRepository(
165174
emailClient: HtEmailInMemoryClient(),
166175
);
@@ -181,19 +190,20 @@ Future<HttpServer> run(Handler handler, InternetAddress ip, int port) async {
181190
userContentPreferencesRepository: userContentPreferencesRepository,
182191
uuidGenerator: const Uuid(),
183192
);
184-
final dashboardSummaryService =
185-
DashboardSummaryService(
186-
headlineRepository: headlineRepository,
187-
categoryRepository: categoryRepository,
188-
sourceRepository: sourceRepository,
189-
);
193+
final dashboardSummaryService = DashboardSummaryService(
194+
headlineRepository: headlineRepository,
195+
categoryRepository: categoryRepository,
196+
sourceRepository: sourceRepository,
197+
);
190198
const permissionService = PermissionService();
191199
final UserPreferenceLimitService userPreferenceLimitService =
192200
DefaultUserPreferenceLimitService(
193201
appConfigRepository: appConfigRepository,
194202
);
195-
196-
// 6. Populate the DependencyContainer
203+
_log
204+
..info('[INIT_SEQ] 5. Services initialized.')
205+
// 6. Populate the DependencyContainer
206+
..info('[INIT_SEQ] 6. Populating dependency container...');
197207
DependencyContainer.instance.init(
198208
headlineRepository: headlineRepository,
199209
categoryRepository: categoryRepository,
@@ -214,21 +224,25 @@ Future<HttpServer> run(Handler handler, InternetAddress ip, int port) async {
214224
);
215225

216226
// 7. Start the server with the gated handler
227+
_log.info('[INIT_SEQ] 7. Starting server with gated handler...');
217228
final server = await serve(gatedHandler, ip, port);
218-
_log.info('Server listening on port ${server.port}');
229+
_log.info('[INIT_SEQ] 7. Server listening on port ${server.port}');
219230

220231
// 8. Open the gate now that the server is ready.
232+
_log.info('[INIT_SEQ] 8. Server ready. Opening request gate.');
221233
initCompleter.complete();
222234

223235
// 9. Handle graceful shutdown
236+
_log.info('[INIT_SEQ] 9. Registering graceful shutdown handler.');
224237
ProcessSignal.sigint.watch().listen((_) async {
225-
_log.info('Received SIGINT. Shutting down...');
238+
_log.info('[SHUTDOWN] Received SIGINT. Shutting down...');
226239
await _connection.close();
227-
_log.info('Database connection closed.');
240+
_log.info('[SHUTDOWN] Database connection closed.');
228241
await server.close(force: true);
229-
_log.info('Server shut down.');
242+
_log.info('[SHUTDOWN] Server shut down.');
230243
exit(0);
231244
});
245+
_log.info('[INIT_SEQ] Server initialization sequence complete.');
232246

233247
return server;
234248
}

0 commit comments

Comments
 (0)