Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Consume BackgroundProcessTool from ddev #24

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ homepage: https://github.com/Workiva/sockjs_client_wrapper
environment:
sdk: '>=2.4.0 <3.0.0'

dependency_overrides:
dart_dev:
git:
url: [email protected]:Workiva/dart_dev.git
ref: background_process_tool

dependencies:
js: ^0.6.1
w_common: ^1.20.1
Expand Down
65 changes: 9 additions & 56 deletions tool/dart_dev/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,68 +12,21 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import 'dart:async';
import 'dart:io';

import 'package:dart_dev/dart_dev.dart';

final config = {
final Map<String, DevTool> config = {
...coreConfig,
'test': CompoundTool()
..addTool(DevTool.fromFunction(startTestServer), alwaysRun: true)
..addTool(_testServer.starter, alwaysRun: true)
..addTool(TestTool(), argMapper: takeAllArgs)
..addTool(DevTool.fromFunction(stopTestServer), alwaysRun: true),
..addTool(_testServer.stopper, alwaysRun: true),
'serve': CompoundTool()
..addTool(DevTool.fromFunction(startExampleServer), alwaysRun: true)
..addTool(_exampleServer.starter, alwaysRun: true)
..addTool(WebdevServeTool()..webdevArgs = ['example:8080'])
..addTool(DevTool.fromFunction(stopExampleServer), alwaysRun: true),
..addTool(_exampleServer.stopper, alwaysRun: true),
};

Process _exampleServer;
Process _testServer;

Future<int> startExampleServer(DevToolExecutionContext _) async {
_exampleServer = await Process.start('node', ['example/server.js'],
mode: ProcessStartMode.inheritStdio);
return firstOf([
// Exit early if it fails to start,
_exampleServer.exitCode,
// otherwise just wait a little bit to ensure it starts up completely.
Future.delayed(Duration(seconds: 2)).then((_) => 0),
]);
}

Future<int> stopExampleServer(DevToolExecutionContext _) async {
_exampleServer?.kill();
await _exampleServer.exitCode;
return 0;
}

Future<int> startTestServer(DevToolExecutionContext _) async {
_testServer = await Process.start('node', ['tool/server.js'],
mode: ProcessStartMode.inheritStdio);
return firstOf([
// Exit early if it fails to start,
_testServer.exitCode,
// otherwise just wait a little bit to ensure it starts up completely.
Future.delayed(Duration(seconds: 2)).then((_) => 0),
]);
}

Future<int> stopTestServer(DevToolExecutionContext _) async {
_testServer?.kill();
await _testServer.exitCode;
return 0;
}

Future<T> firstOf<T>(Iterable<Future<T>> futures) {
final c = Completer<T>();
for (final future in futures) {
future.then((v) {
if (!c.isCompleted) {
c.complete(v);
}
}).catchError(c.completeError);
}
return c.future;
}
final _exampleServer = ProcessTool('node', ['example/server.js'])
.backgrounded(startAfterDelay: Duration(seconds: 2));
final _testServer = ProcessTool('node', ['tool/server.js'])
.backgrounded(startAfterDelay: Duration(seconds: 2));