commit | 9e2cf0d8d187dd222e5456ac40c8a4a4c347a812 | [log] [tgz] |
---|---|---|
author | Alexander Thomas <athom@google.com> | Mon Dec 21 21:46:55 2020 |
committer | GitHub <noreply@github.com> | Mon Dec 21 21:46:55 2020 |
tree | 12fa543fd1b82b87b11df8f113aa984e4848e34d | |
parent | 060c55a933d39798681a4f533b161b81dc48d77e [diff] |
Migrate to GitHub Actions (#53) * Migrate to GitHub Actions * Delete .travis.yml * Run `pub get` in travis.sh * Rename job to test
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.