Migrated production

This commit is contained in:
thomashii@dukefirehawk.com 2021-05-01 10:03:46 +08:00
parent 5994b1f405
commit b091b435e4
6 changed files with 61 additions and 57 deletions

View file

@ -22,8 +22,8 @@
* Migrated jael to 4.0.0 (20/20 tests passed) * Migrated jael to 4.0.0 (20/20 tests passed)
* Migrated jael_preprocessor to 3.0.0 (5/5 tests passed) * Migrated jael_preprocessor to 3.0.0 (5/5 tests passed)
* Migrated angel_jael to 4.0.0 (1/1 test passed) * Migrated angel_jael to 4.0.0 (1/1 test passed)
* Updated pub_sub to 4.0.0 (16/16 tests passed) * Migrated pub_sub to 4.0.0 (16/16 tests passed)
* Updated production to 2.0.0 (in progress) * Migrated production to 3.0.0 (0/0 tests passed)
* Updated hot to 3.0.0 (in progress) * Updated hot to 3.0.0 (in progress)
* Updated static to 3.0.0 (in progress) * Updated static to 3.0.0 (in progress)
* Update basic-sdk-2.12.x boilerplate (in progress) * Update basic-sdk-2.12.x boilerplate (in progress)

View file

@ -4,12 +4,12 @@ import 'package:angel_framework/angel_framework.dart';
import 'package:angel_production/angel_production.dart'; import 'package:angel_production/angel_production.dart';
import 'package:pub_sub/pub_sub.dart' as pub_sub; import 'package:pub_sub/pub_sub.dart' as pub_sub;
main(List<String> args) => Runner('example', configureServer).run(args); void main(List<String> args) => Runner('example', configureServer).run(args);
Future configureServer(Angel app) async { Future configureServer(Angel app) async {
// Use the injected `pub_sub.Client` to send messages. // Use the injected `pub_sub.Client` to send messages.
var client = app.container.make<pub_sub.Client>(); var client = app.container!.make<pub_sub.Client>()!;
var greeting = 'Hello! This is the default greeting.'; String? greeting = 'Hello! This is the default greeting.';
// We can listen for an event to perform some behavior. // We can listen for an event to perform some behavior.
// //
@ -32,7 +32,7 @@ Future configureServer(Angel app) async {
// This route will push a new value for `greeting`. // This route will push a new value for `greeting`.
app.get('/change_greeting/:newGreeting', (req, res) { app.get('/change_greeting/:newGreeting', (req, res) {
greeting = req.params['newGreeting'] as String; greeting = req.params['newGreeting'] as String?;
client.publish('greeting_changed', greeting); client.publish('greeting_changed', greeting);
return 'Changed greeting -> $greeting'; return 'Changed greeting -> $greeting';
}); });

View file

@ -1,6 +1,6 @@
/// Information about the currently-running instance. /// Information about the currently-running instance.
class InstanceInfo { class InstanceInfo {
final int id; final int? id;
const InstanceInfo({this.id}); const InstanceInfo({this.id});
} }

View file

@ -30,13 +30,13 @@ class RunnerOptions {
..addOption('key-file', help: 'The PEM key file to read.') ..addOption('key-file', help: 'The PEM key file to read.')
..addOption('key-password', help: 'The PEM key file password.'); ..addOption('key-password', help: 'The PEM key file password.');
final String hostname, final String? hostname,
certificateFile, certificateFile,
keyFile, keyFile,
certificatePassword, certificatePassword,
keyPassword; keyPassword;
final int concurrency, port; final int concurrency, port;
final bool useZone, respawn, quiet, ssl, http2; final bool? useZone, respawn, quiet, ssl, http2;
RunnerOptions( RunnerOptions(
{this.hostname = '127.0.0.1', {this.hostname = '127.0.0.1',
@ -54,25 +54,25 @@ class RunnerOptions {
factory RunnerOptions.fromArgResults(ArgResults argResults) { factory RunnerOptions.fromArgResults(ArgResults argResults) {
return RunnerOptions( return RunnerOptions(
hostname: argResults['address'] as String, hostname: argResults['address'] as String?,
port: int.parse(argResults['port'] as String), port: int.parse(argResults['port'] as String),
concurrency: int.parse(argResults['concurrency'] as String), concurrency: int.parse(argResults['concurrency'] as String),
useZone: argResults['use-zone'] as bool, useZone: argResults['use-zone'] as bool?,
respawn: argResults['respawn'] as bool, respawn: argResults['respawn'] as bool?,
quiet: argResults['quiet'] as bool, quiet: argResults['quiet'] as bool?,
certificateFile: argResults.wasParsed('certificate-file') certificateFile: argResults.wasParsed('certificate-file')
? argResults['certificate-file'] as String ? argResults['certificate-file'] as String?
: null, : null,
keyFile: argResults.wasParsed('key-file') keyFile: argResults.wasParsed('key-file')
? argResults['key-file'] as String ? argResults['key-file'] as String?
: null, : null,
ssl: argResults['ssl'] as bool, ssl: argResults['ssl'] as bool?,
http2: argResults['http2'] as bool, http2: argResults['http2'] as bool?,
certificatePassword: argResults.wasParsed('certificate-password') certificatePassword: argResults.wasParsed('certificate-password')
? argResults['certificate-password'] as String ? argResults['certificate-password'] as String?
: null, : null,
keyPassword: argResults.wasParsed('key-password') keyPassword: argResults.wasParsed('key-password')
? argResults['key-password'] as String ? argResults['key-password'] as String?
: null, : null,
); );
} }

View file

@ -35,9 +35,9 @@ _ ___ | /| / / /_/ / _ /___ _ /___
'''; ''';
static void handleLogRecord(LogRecord record, RunnerOptions options) { static void handleLogRecord(LogRecord? record, RunnerOptions options) {
if (options.quiet) return; if (options.quiet!) return;
var code = chooseLogColor(record.level); var code = chooseLogColor(record!.level);
if (record.error == null) print(code.wrap(record.toString())); if (record.error == null) print(code.wrap(record.toString()));
@ -55,15 +55,17 @@ _ ___ | /| / / /_/ / _ /___ _ /___
/// Chooses a color based on the logger [level]. /// Chooses a color based on the logger [level].
static AnsiCode chooseLogColor(Level level) { static AnsiCode chooseLogColor(Level level) {
if (level == Level.SHOUT) if (level == Level.SHOUT) {
return backgroundRed; return backgroundRed;
else if (level == Level.SEVERE) } else if (level == Level.SEVERE) {
return red; return red;
else if (level == Level.WARNING) } else if (level == Level.WARNING) {
return yellow; return yellow;
else if (level == Level.INFO) } else if (level == Level.INFO) {
return cyan; return cyan;
else if (level == Level.FINER || level == Level.FINEST) return lightGray; } else if (level == Level.FINER || level == Level.FINEST) {
return lightGray;
}
return resetAll; return resetAll;
} }
@ -94,11 +96,12 @@ _ ___ | /| / / /_/ / _ /___ _ /___
.then((isolate) {}) .then((isolate) {})
.catchError(c.completeError); .catchError(c.completeError);
onLogRecord.listen((msg) => handleLogRecord(msg as LogRecord, options)); onLogRecord.listen((msg) => handleLogRecord(msg as LogRecord?, options));
onError.listen((msg) { onError.listen((msg) {
if (msg is List) { if (msg is List) {
var e = msg[0], st = StackTrace.fromString(msg[1].toString()); dynamic e = msg[0];
var st = StackTrace.fromString(msg[1].toString());
handleLogRecord( handleLogRecord(
LogRecord( LogRecord(
Level.SEVERE, 'Fatal error', runnerArgs.loggerName, e, st), Level.SEVERE, 'Fatal error', runnerArgs.loggerName, e, st),
@ -111,7 +114,7 @@ _ ___ | /| / / /_/ / _ /___ _ /___
}); });
onExit.listen((_) { onExit.listen((_) {
if (options.respawn) { if (options.respawn!) {
handleLogRecord( handleLogRecord(
LogRecord( LogRecord(
Level.WARNING, Level.WARNING,
@ -132,13 +135,13 @@ _ ___ | /| / / /_/ / _ /___ _ /___
/// Starts a number of isolates, running identical instances of an Angel application. /// Starts a number of isolates, running identical instances of an Angel application.
Future run(List<String> args) async { Future run(List<String> args) async {
pub_sub.Server server; late pub_sub.Server server;
try { try {
var argResults = RunnerOptions.argParser.parse(args); var argResults = RunnerOptions.argParser.parse(args);
var options = RunnerOptions.fromArgResults(argResults); var options = RunnerOptions.fromArgResults(argResults);
if (options.ssl || options.http2) { if (options.ssl! || options.http2!) {
if (options.certificateFile == null) { if (options.certificateFile == null) {
throw ArgParserException('Missing --certificate-file option.'); throw ArgParserException('Missing --certificate-file option.');
} else if (options.keyFile == null) { } else if (options.keyFile == null) {
@ -148,7 +151,7 @@ _ ___ | /| / / /_/ / _ /___ _ /___
print(darkGray.wrap(asciiArt.trim() + print(darkGray.wrap(asciiArt.trim() +
'\n\n' + '\n\n' +
"A batteries-included, full-featured, full-stack framework in Dart." + 'A batteries-included, full-featured, full-stack framework in Dart.' +
'\n\n' + '\n\n' +
'https://angel-dart.github.io\n')); 'https://angel-dart.github.io\n'));
@ -157,13 +160,13 @@ _ ___ | /| / / /_/ / _ /___ _ /___
return; return;
} }
print('Starting `${name}` application...'); print('Starting `$name` application...');
var adapter = pub_sub.IsolateAdapter(); var adapter = pub_sub.IsolateAdapter();
server = pub_sub.Server([adapter]); server = pub_sub.Server([adapter]);
// Register clients // Register clients
for (int i = 0; i < Platform.numberOfProcessors; i++) { for (var i = 0; i < Platform.numberOfProcessors; i++) {
server.registerClient(pub_sub.ClientInfo('client$i')); server.registerClient(pub_sub.ClientInfo('client$i'));
} }
@ -184,7 +187,7 @@ _ ___ | /| / / /_/ / _ /___ _ /___
..writeln(red.wrap(st.toString())); ..writeln(red.wrap(st.toString()));
exitCode = 1; exitCode = 1;
} finally { } finally {
await server?.close(); await server.close();
} }
} }
@ -203,8 +206,8 @@ _ ___ | /| / / /_/ / _ /___ _ /___
pub_sub.IsolateClient('client${argsWithId.id}', args.pubSubSendPort); pub_sub.IsolateClient('client${argsWithId.id}', args.pubSubSendPort);
var app = Angel(reflector: args.reflector) var app = Angel(reflector: args.reflector)
..container.registerSingleton<pub_sub.Client>(client) ..container!.registerSingleton<pub_sub.Client>(client)
..container.registerSingleton(InstanceInfo(id: argsWithId.id)); ..container!.registerSingleton(InstanceInfo(id: argsWithId.id));
app.shutdownHooks.add((_) => client.close()); app.shutdownHooks.add((_) => client.close());
@ -216,31 +219,31 @@ _ ___ | /| / / /_/ / _ /___ _ /___
} }
AngelHttp http; AngelHttp http;
SecurityContext securityContext; late SecurityContext securityContext;
Uri serverUrl; Uri serverUrl;
if (args.options.ssl || args.options.http2) { if (args.options.ssl! || args.options.http2!) {
securityContext = SecurityContext(); securityContext = SecurityContext();
securityContext.useCertificateChain(args.options.certificateFile, securityContext.useCertificateChain(args.options.certificateFile!,
password: args.options.certificatePassword); password: args.options.certificatePassword);
securityContext.usePrivateKey(args.options.keyFile, securityContext.usePrivateKey(args.options.keyFile!,
password: args.options.keyPassword); password: args.options.keyPassword);
} }
if (args.options.ssl) { if (args.options.ssl!) {
http = AngelHttp.custom(app, startSharedSecure(securityContext), http = AngelHttp.custom(app, startSharedSecure(securityContext),
useZone: args.options.useZone); useZone: args.options.useZone!);
} else { } else {
http = http =
AngelHttp.custom(app, startShared, useZone: args.options.useZone); AngelHttp.custom(app, startShared, useZone: args.options.useZone!);
} }
Driver driver; Driver driver;
if (args.options.http2) { if (args.options.http2!) {
securityContext.setAlpnProtocols(['h2'], true); securityContext.setAlpnProtocols(['h2'], true);
var http2 = AngelHttp2.custom(app, securityContext, startSharedHttp2, var http2 = AngelHttp2.custom(app, securityContext, startSharedHttp2,
useZone: args.options.useZone); useZone: args.options.useZone!);
http2.onHttp1.listen(http.handleRequest); http2.onHttp1.listen(http.handleRequest);
driver = http2; driver = http2;
} else { } else {
@ -249,8 +252,9 @@ _ ___ | /| / / /_/ / _ /___ _ /___
await driver.startServer(args.options.hostname, args.options.port); await driver.startServer(args.options.hostname, args.options.port);
serverUrl = driver.uri; serverUrl = driver.uri;
if (args.options.ssl || args.options.http2) if (args.options.ssl! || args.options.http2!) {
serverUrl = serverUrl.replace(scheme: 'https'); serverUrl = serverUrl.replace(scheme: 'https');
}
print('Instance #${argsWithId.id} listening at $serverUrl'); print('Instance #${argsWithId.id} listening at $serverUrl');
}); });
} }

View file

@ -1,29 +1,29 @@
name: angel_production name: angel_production
version: 2.0.0 version: 3.0.0
description: Helpers for concurrency, message-passing, rotating loggers, and other production functionality in Angel. description: Helpers for concurrency, message-passing, rotating loggers, and other production functionality in Angel.
author: Tobe O <thosakwe@gmail.com> author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/production homepage: https://github.com/angel-dart/production
publish_to: none publish_to: none
environment: environment:
sdk: ">=2.10.0 <3.0.0" sdk: '>=2.12.0 <3.0.0'
dependencies: dependencies:
angel_container: angel_container:
git: git:
url: https://github.com/dukefirehawk/angel.git url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x ref: sdk-2.12.x_nnbd
path: packages/container/angel_container path: packages/container/angel_container
angel_framework: angel_framework:
git: git:
url: https://github.com/dukefirehawk/angel.git url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x ref: sdk-2.12.x_nnbd
path: packages/framework path: packages/framework
pub_sub: pub_sub:
git: git:
url: https://github.com/dukefirehawk/angel.git url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x ref: sdk-2.12.x_nnbd
path: packages/pub_sub path: packages/pub_sub
args: ^2.0.0 args: ^2.1.0
io: ^0.3.5 io: ^1.0.0
logging: ^1.0.0 logging: ^1.0.1
dev_dependencies: dev_dependencies:
pedantic: ^1.0.0 pedantic: ^1.11.0