Skip to content

Commit

Permalink
Add server.control(). Closes #3767
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Mar 30, 2018
1 parent ab14720 commit d8848da
Show file tree
Hide file tree
Showing 6 changed files with 1,653 additions and 1,594 deletions.
11 changes: 10 additions & 1 deletion API.md
@@ -1,4 +1,4 @@
# v17.2.x API Reference
# v17.3.x API Reference

<!-- toc -->

Expand Down Expand Up @@ -58,6 +58,7 @@
- [`server.bind(context)`](#server.bind())
- [`server.cache(options)`](#server.cache())
- [`await server.cache.provision(options)`](#server.cache.provision())
- [`server.control(server)`](#server.control())
- [`server.decoder(encoding, decoder)`](#server.decoder())
- [`server.decorate(type, property, method, [options])`](#server.decorate())
- [`server.dependency(dependencies, [after])`](#server.dependency())
Expand Down Expand Up @@ -1424,6 +1425,14 @@ async function example() {
}
```

### <a name="server.control()" /> `server.control(server)`

Links another server to the initialize/start/stop state of the current server by calling the
controlled server `initialize()`/`start()`/`stop()` methods whenever the current server methods
are called, where:

- `server` - the **hapi** server object to be controlled.

### <a name="server.decoder()" /> `server.decoder(encoding, decoder)`

Registers a custom content decoding compressor to extend the built-in support for `'gzip'` and
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,4 +1,4 @@
Copyright (c) 2011-2017, Project contributors
Copyright (c) 2011-2018, Project contributors
Copyright (c) 2011-2014, Walmart
Copyright (c) 2011, Yahoo Inc.
All rights reserved.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -10,7 +10,7 @@ developers to focus on writing reusable application logic in a highly modular an

Version 17.x only supports node v8.9.0 and over. For older version of node please use version 16.x.

Development version: **17.1.x** ([release notes](https://github.com/hapijs/hapi/issues?labels=release+notes&page=1&state=closed))
Development version: **17.3.x** ([release notes](https://github.com/hapijs/hapi/issues?labels=release+notes&page=1&state=closed))
[![Build Status](https://secure.travis-ci.org/hapijs/hapi.svg?branch=master)](http://travis-ci.org/hapijs/hapi)

For the latest updates, [change log](http://hapijs.com/updates), and release information visit [hapijs.com](http://hapijs.com) and follow [@hapijs](https://twitter.com/hapijs) on twitter. If you have questions, please open an issue in the
Expand Down
14 changes: 14 additions & 0 deletions lib/core.js
Expand Up @@ -63,6 +63,7 @@ exports = module.exports = internals.Core = class {
this.auth = new Auth(this);
this.caches = new Map(); // Cache clients
this.compression = new Compression();
this.controlled = null; // Other servers linked to the phases of this server
this.decorations = { handler: [], request: [], server: [], toolkit: [] }; // Public decoration names
this.dependencies = []; // Plugin dependencies
this.events = new Podium(internals.events);
Expand Down Expand Up @@ -251,6 +252,10 @@ exports = module.exports = internals.Core = class {
await this.events.emit('start');

try {
if (this.controlled) {
await Promise.all(this.controlled.map((control) => control.start()));
}

await this._invoke('onPostStart');
}
catch (err) {
Expand Down Expand Up @@ -319,6 +324,10 @@ exports = module.exports = internals.Core = class {
await this._invoke('onPreStart');
this.heavy.start();
this.phase = 'initialized';

if (this.controlled) {
await Promise.all(this.controlled.map((control) => control.initialize()));
}
}
catch (err) {
this.phase = 'invalid';
Expand Down Expand Up @@ -365,6 +374,11 @@ exports = module.exports = internals.Core = class {

await this.events.emit('stop');
this.heavy.stop();

if (this.controlled) {
await Promise.all(this.controlled.map((control) => control.stop(options)));
}

await this._invoke('onPostStop');
this.phase = 'stopped';
}
Expand Down
8 changes: 8 additions & 0 deletions lib/server.js
Expand Up @@ -99,6 +99,14 @@ internals.Server = class {
this.realm.settings.bind = context;
}

control(server) {

Hoek.assert(server instanceof internals.Server, 'Can only control Server objects');

this._core.controlled = this._core.controlled || [];
this._core.controlled.push(server);
}

decoder(encoding, decoder) {

return this._core.compression.addDecoder(encoding, decoder);
Expand Down

0 comments on commit d8848da

Please sign in to comment.