commit | 9c48418b7464d523c3e511d91311c48feaa6c21a | [log] [tgz] |
---|---|---|
author | Franklin Yow <58489007+franklinyow@users.noreply.github.com> | Wed Mar 31 15:21:25 2021 -0700 |
committer | GitHub <noreply@github.com> | Wed Mar 31 15:21:25 2021 -0700 |
tree | 8a479cbfe21d8a53c45d8ae898731ab6f4db542b | |
parent | 0885637b037979afbf5bcd05fd748b309fd669c0 [diff] | |
parent | dde6d3b22e0a1c494f468f3038469acaf11d8c90 [diff] |
Merge pull request #55 from dart-lang/franklinyow-patch-1 Update LICENSE
Tools for creating a persistent worker loop for bazel.
There are two abstract classes provided by this package, AsyncWorkerLoop
and SyncWorkerLoop
. These each have a performRequest
method which you must implement.
Lets look at a simple example of a SyncWorkerLoop
implementation:
import 'dart:io'; import 'package:bazel_worker/bazel_worker.dart'; void main() { // Blocks until it gets an EOF from stdin. new SyncSimpleWorker().run(); } class SyncSimpleWorker extends SyncWorkerLoop { /// Must synchronously return a [WorkResponse], since this is a /// [SyncWorkerLoop]. WorkResponse performRequest(WorkRequest request) { new File('hello.txt').writeAsStringSync('hello world!'); return new WorkResponse()..exitCode = EXIT_CODE_OK; } }
And now the same thing, implemented as an AsyncWorkerLoop
:
import 'dart:io'; import 'package:bazel_worker/bazel_worker.dart'; void main() { // Doesn't block, runs tasks async as they are received on stdin. new AsyncSimpleWorker().run(); } class AsyncSimpleWorker extends AsyncWorkerLoop { /// Must return a [Future<WorkResponse>], since this is an /// [AsyncWorkerLoop]. Future<WorkResponse> performRequest(WorkRequest request) async { await new File('hello.txt').writeAsString('hello world!'); return new WorkResponse()..exitCode = EXIT_CODE_OK; } }
As you can see, these are nearly identical, it mostly comes down to the constraints on your package and personal preference which one you choose to implement.
A package:bazel_worker/testing.dart
file is also provided, which can greatly assist with writing unit tests for your worker. See the test/worker_loop_test.dart
test included in this package for an example of how the helpers can be used.
Please file feature requests and bugs at the issue tracker.