feat: add retry to db connection when startup.
This commit is contained in:
+3
-5
@@ -8,12 +8,11 @@ import 'package:logging/logging.dart';
|
||||
|
||||
void main() async {
|
||||
// Watch the config/ and web/ directories for changes, and hot-reload the server.
|
||||
hierarchicalLoggingEnabled = true;
|
||||
|
||||
var hot = HotReloader(() async {
|
||||
var logger = Logger.detached('dde_gesture_manager_api')
|
||||
Logger.root
|
||||
..level = Level.ALL
|
||||
..onRecord.listen(prettyLog);
|
||||
var logger = Logger.detached('dde_gesture_manager_api');
|
||||
var app = Angel(logger: logger, reflector: MirrorsReflector());
|
||||
await app.configure(configureServer);
|
||||
return app;
|
||||
@@ -23,6 +22,5 @@ void main() async {
|
||||
]);
|
||||
|
||||
var server = await hot.startServer('127.0.0.1', 3000);
|
||||
print(
|
||||
'dde_gesture_manager_api server listening at http://${server.address.address}:${server.port}');
|
||||
print('dde_gesture_manager_api server listening at http://${server.address.address}:${server.port}');
|
||||
}
|
||||
|
||||
@@ -3,11 +3,32 @@ import 'dart:io';
|
||||
import 'package:angel3_framework/angel3_framework.dart';
|
||||
import 'package:angel3_orm/angel3_orm.dart';
|
||||
import 'package:angel3_orm_postgres/angel3_orm_postgres.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:postgres/postgres.dart';
|
||||
|
||||
const times = ['', 'st', 'nd', 'rd'];
|
||||
|
||||
const retryTimeOut = Duration(seconds: 30);
|
||||
|
||||
Future<void> configureServer(Angel app) async {
|
||||
var connection = await connectToPostgres(app.configuration);
|
||||
final _log = Logger('OrmPlugin');
|
||||
late PostgreSQLConnection connection;
|
||||
var _startTime = DateTime.now();
|
||||
var _retry = 1;
|
||||
while (_startTime.difference(DateTime.now()).abs() <= retryTimeOut) {
|
||||
try {
|
||||
connection = await connectToPostgres(app.configuration);
|
||||
await connection.open();
|
||||
break;
|
||||
} catch (e, st) {
|
||||
await connection.close();
|
||||
_log.severe(
|
||||
'Failed to connect, the $_retry${_retry <= 3 ? times[_retry] : 'th'} retry will do in a second.', e, st);
|
||||
sleep(Duration(seconds: 1));
|
||||
_retry++;
|
||||
if (_startTime.difference(DateTime.now()).abs() > retryTimeOut) rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
var logger = app.environment.isProduction ? null : app.logger;
|
||||
var executor = PostgreSqlExecutor(connection, logger: logger);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'package:angel3_framework/angel3_framework.dart';
|
||||
import 'package:angel3_cors/angel3_cors.dart';
|
||||
import 'package:angel3_static/angel3_static.dart';
|
||||
import 'package:file/file.dart';
|
||||
import 'controllers/auth_controllers.dart' as auth_controllers;
|
||||
import 'controllers/system_controllers.dart' as system_controllers;
|
||||
@@ -28,6 +29,9 @@ AngelConfigurer configureServer(FileSystem fileSystem) {
|
||||
await app.configure(auth_controllers.configureServer);
|
||||
await app.configure(scheme_controllers.configureServer);
|
||||
|
||||
var vDir = VirtualDirectory(app, fileSystem, source: fileSystem.directory('web'));
|
||||
app.fallback(vDir.handleRequest);
|
||||
|
||||
// Throw a 404 if no route matched the request.
|
||||
app.fallback((req, res) => throw AngelHttpException.notFound());
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ dependencies:
|
||||
angel3_orm: ^4.0.6
|
||||
angel3_orm_postgres: ^3.3.0
|
||||
angel3_serialize: ^4.1.0
|
||||
angel3_static: ^4.1.0
|
||||
angel3_production: ^3.1.2
|
||||
belatuk_pretty_logging: ^4.0.0
|
||||
optional: ^6.0.0
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<head>
|
||||
<meta charset="utf-8"/>
|
||||
<title>{% block title %}{% endblock %}</title>
|
||||
</head>
|
||||
<body>
|
||||
{% block body %}{% endblock %}
|
||||
</body>
|
||||
<link rel="shortcut icon" href="/images/favicon.png"/>
|
||||
<link rel="bookmark" href="/images/favicon.png"/>
|
||||
<link rel="stylesheet" type="text/css" href="/css/site.css"/>
|
||||
</head>
|
||||
<body>
|
||||
{% block body %}{% endblock %}
|
||||
</body>
|
||||
</html>
|
||||
+3
-21
@@ -1,27 +1,9 @@
|
||||
html, body {
|
||||
height: 100%;
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
width: 100%;
|
||||
display: table;
|
||||
font-weight: 100;
|
||||
font-family: 'Lato', sans-serif;
|
||||
}
|
||||
|
||||
.container {
|
||||
text-align: center;
|
||||
display: table-cell;
|
||||
vertical-align: middle;
|
||||
}
|
||||
|
||||
.content {
|
||||
text-align: center;
|
||||
display: inline-block;
|
||||
}
|
||||
|
||||
.title {
|
||||
font-size: 96px;
|
||||
margin: .5em;
|
||||
}
|
||||
Reference in New Issue
Block a user