Runner constructor
Runner({
- RunStepWrapper wrapRunStep = _defaultRunStep,
Create a Runner instance with an empty cache.
The optional wrapRunStep
parameters allows specification of a function
that will wrap the runStep
methods provided in the Step definitons.
This can be used to catch exceptions, run steps in a zone, measure
execution time of a step (excluding dependencies), or simply log step
execution as illustrated in the example below.
/// Define a step that return 'Hello World!'
final helloWorldStep = Step.define('hello-world').build(() => 'Hello World!');
/// Define a step that depends on helloWorldStep and prints its return
/// value.
final Step<void> printStep = Step.define('print-msg')
// Declare dependency on helloWorldStep
.dep(helloWorldStep)
// Build step, result from helloWorldStep will be passed in as `msg`
.build((msg) {
print(msg);
});
/// Implementation of [RunStepWrapper] that logs start and finish for
/// each step.
Future<T> wrapStepWithLogging<T>(
Step<T> step,
Future<T> Function() runStep,
) async {
print('Starting to run ${step.name}');
try {
return await runStep();
} finally {
print('Finishing running ${step.name}');
}
}
Future<void> main() async {
// This creates a Runner that will wrap calls to runStep with
// wrapStepWithLogging, hence, printing log entries for step execution.
final r = Runner(wrapRunStep: wrapStepWithLogging);
await r.run(printStep);
}
Implementation
Runner({
RunStepWrapper wrapRunStep = _defaultRunStep,
}) : _wrapRunStep = wrapRunStep;