process_manager.dart 2.02 KB
Newer Older
1 2 3 4 5 6
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

7 8
import 'package:process/process.dart';
import 'package:process/record_replay.dart';
9

10
import 'common.dart';
11
import 'context.dart';
12
import 'file_system.dart';
13
import 'process.dart';
14

15
const String _kRecordingType = 'process';
16
const ProcessManager _kLocalProcessManager = LocalProcessManager();
17

18
/// The active process manager.
19
ProcessManager get processManager => context.get<ProcessManager>() ?? _kLocalProcessManager;
20

21 22
/// Gets a [ProcessManager] that will record process invocation activity to the
/// specified base recording [location].
23
///
24 25 26 27
/// Activity will be recorded in a subdirectory of [location] named `"process"`.
/// It is permissible for [location] to represent an existing non-empty
/// directory as long as there is no collision with the `"process"`
/// subdirectory.
28
RecordingProcessManager getRecordingProcessManager(String location) {
29
  final Directory dir = getRecordingSink(location, _kRecordingType);
30
  const ProcessManager delegate = LocalProcessManager();
31
  final RecordingProcessManager manager = RecordingProcessManager(delegate, dir);
32 33 34
  addShutdownHook(() async {
    await manager.flush(finishRunningProcesses: true);
  }, ShutdownStage.SERIALIZE_RECORDING);
35
  return manager;
36
}
37

38 39
/// Gets a [ProcessManager] that replays process activity from a previously
/// recorded set of invocations.
40
///
41 42
/// [location] must represent a directory to which process activity has been
/// recorded (i.e. the result of having been previously passed to
43 44
/// [getRecordingProcessManager]), or a [ToolExit] will be thrown.
Future<ReplayProcessManager> getReplayProcessManager(String location) async {
45
  final Directory dir = getReplaySource(location, _kRecordingType);
46 47 48

  ProcessManager manager;
  try {
49
    manager = await ReplayProcessManager.create(dir);
50 51
  } on ArgumentError catch (error) {
    throwToolExit('Invalid replay-from: $error');
52 53
  }

54
  return manager;
55
}