angel_production 1.0.0-alpha.1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 86

production #

Helpers for concurrency, message-passing, rotating loggers, and other production functionality in Angel.

Screenshot

This will become the de-facto way to run Angel applications in deployed environments, as it takes care of inter-isolate communication, respawning dead processes, and other housekeeping for you automatically.

Most users will want to use the Runner class.

Runner #

Runner is a utility, powered by package:args, that is intended to be the entry point of your application.

Instantiate it as follows, and your file will become a command-line executable that spawns multiple instances of your application:

import 'dart:async';
import 'dart:isolate';
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_production/angel_production.dart';

main(List<String> args) => new Runner('example', configureServer).run(args);

Future configureServer(Angel app) async {
  app.get('/', (req, res) => 'Hello, production world!');

  app.get('/crash', (req, res) {
    // We'll crash this instance deliberately, but the Runner will auto-respawn for us.
    new Timer(const Duration(seconds: 3), Isolate.current.kill);
    return 'Crashing in 3s...';
  });
}

Runner will automatically re-spawn crashed instances, unless --no-respawn is passed. This can prevent your server from entirely going down at the first error, and adds a layer of fault tolerance to your infrastructure.

When combined with systemd, deploying Angel applications on Linux can be very simple.

Message Passing #

The Runner class uses package:pub_sub to coordinate message passing between isolates.

When one isolate sends a message, all other isolates will receive the same message, except for the isolate that sent it.

It is injected into your application's Container as pub_sub.Client, so you can use it as follows:

// Use the injected `pub_sub.Client` to send messages.
var client = app.container.make<pub_sub.Client>();

// We can listen for an event to perform some behavior.
//
// Here, we use message passing to synchronize some common state.
var onGreetingChanged = await client.subscribe('user_upgraded');
onGreetingChanged
    .cast<User>()
    .listen((user) {
      // Do something...
    });

Run-time Metadata #

At run-time, you may want to know information about the currently-running instance, for example, which number instance. For this, the InstanceInfo class is injected into each instance:

var instanceInfo = app.container.make<InstanceInfo>();
print('This is instance #${instanceInfo.id}');

Command-line Options #

The Runner class supplies options like the following:

wg-dhcp201d194d221:production thosakwe$ dart example/main.dart --help
____________   ________________________ 
___    |__  | / /_  ____/__  ____/__  / 
__  /| |_   |/ /_  / __ __  __/  __  /  
_  ___ |  /|  / / /_/ / _  /___  _  /___
/_/  |_/_/ |_/  ____/  /_____/  /_____/

A batteries-included, full-featured, full-stack framework in Dart.

https://angel-dart.github.io

Options:
-h, --help            Print this help information.
    --[no-]respawn    Automatically respawn crashed application instances.
                      (defaults to on)

    --use-zone        Create a new Zone for each request.
-a, --address         The address to listen on.
                      (defaults to "127.0.0.1")

-j, --concurrency     The number of isolates to spawn.
                      (defaults to "4")

-p, --port            The port to listen on.
                      (defaults to "3000")

1.0.0-alpha.1 #

  • Import framework/http.

1.0.0-alpha #

  • Initial version.

example/main.dart

import 'dart:async';
import 'dart:isolate';
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_production/angel_production.dart';
import 'package:pub_sub/pub_sub.dart' as pub_sub;

main(List<String> args) => new Runner('example', configureServer).run(args);

Future configureServer(Angel app) async {
  // Use the injected `pub_sub.Client` to send messages.
  var client = app.container.make<pub_sub.Client>();
  var greeting = 'Hello! This is the default greeting.';

  // We can listen for an event to perform some behavior.
  //
  // Here, we use message passing to synchronize some common state.
  var onGreetingChanged = await client.subscribe('greeting_changed');
  onGreetingChanged
      .cast<String>()
      .listen((newGreeting) => greeting = newGreeting);

  // Add some routes...
  app.get('/', (req, res) => 'Hello, production world!');

  // Create some routes to demonstrate message passing.
  app.get('/greeting', (req, res) => greeting);

  // This route will push a new value for `greeting`.
  app.get('/change_greeting/:newGreeting', (req, res) {
    greeting = req.params['newGreeting'] as String;
    client.publish('greeting_changed', greeting);
    return 'Changed greeting -> $greeting';
  });

  // The `Runner` helps with fault tolerance.
  app.get('/crash', (req, res) {
    // We'll crash this instance deliberately, but the Runner will auto-respawn for us.
    new Timer(const Duration(seconds: 3), Isolate.current.kill);
    return 'Crashing in 3s...';
  });
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  angel_production: ^1.0.0-alpha.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:angel_production/angel_production.dart';
  
Version Uploaded Documentation Archive
1.0.0-alpha.1 Nov 6, 2018 Go to the documentation of angel_production 1.0.0-alpha.1 Download angel_production 1.0.0-alpha.1 archive
1.0.0-alpha Sep 4, 2018 Go to the documentation of angel_production 1.0.0-alpha Download angel_production 1.0.0-alpha archive
Popularity:
Describes how popular the package is relative to other packages. [more]
77
Health:
Code health derived from static analysis. [more]
97
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
95
Overall:
Weighted score of the above. [more]
86
Learn more about scoring.

We analyzed this package on Apr 23, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.2.0
  • pana: 0.12.14

Platforms

Detected platforms: Flutter, other

Primary library: package:angel_production/angel_production.dart with components: io, isolate.

Health suggestions

Fix lib/src/options.dart. (-2.48 points)

Analysis of lib/src/options.dart reported 5 hints:

line 28 col 21: Use = to separate a named parameter from its default value.

line 29 col 16: Use = to separate a named parameter from its default value.

line 30 col 23: Use = to separate a named parameter from its default value.

line 31 col 19: Use = to separate a named parameter from its default value.

line 32 col 19: Use = to separate a named parameter from its default value.

Fix lib/src/runner.dart. (-1 points)

Analysis of lib/src/runner.dart reported 2 hints:

line 26 col 22: Use = to separate a named parameter from its default value.

line 171 col 7: Future results in async function bodies must be awaited or marked unawaited using package:pedantic.

Maintenance suggestions

Package is pre-release. (-5 points)

Pre-release versions should be used with caution; their API can change in breaking ways.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev <3.0.0
angel_container ^1.0.0-alpha 1.0.4
angel_framework ^2.0.0-alpha 2.0.0-rc.8
args ^1.0.0 1.5.1
io ^0.3.2 0.3.3
logging ^0.11.3 0.11.3+2
pub_sub ^2.0.0 2.2.0
Transitive dependencies
angel_http_exception 1.1.0
angel_model 1.0.2
angel_route 3.0.6
async 2.2.0
charcode 1.1.2
code_buffer 1.0.1
collection 1.14.11
combinator 1.1.0
convert 2.1.1
crypto 2.0.6
dart2_constant 1.0.2+dart2
file 5.0.7
http2 1.0.0
http_parser 3.1.3
http_server 0.9.8+1
intl 0.15.8
json_rpc_2 2.1.0
matcher 0.12.5
merge_map 1.0.2
meta 1.1.7
mime 0.9.6+2
mock_request 1.0.5
path 1.6.2
pedantic 1.5.0
quiver 2.0.3
quiver_hashcode 2.0.0
source_span 1.5.5
stack_trace 1.9.3
stream_channel 1.7.0 2.0.0
string_scanner 1.0.4
term_glyph 1.1.0
tuple 1.0.2
typed_data 1.1.6
uuid 2.0.1