beginReporting function
Resolves config, opens or attaches to a run, and arms the buffer.
Any failure soft-disables reporting unless TRACERA_REQUIRE_REPORT is set,
in which case the process exits non-zero.
Implementation
Future<void> beginReporting(ReporterState state) async {
final env = mergeEnvWithDotenv(envMap(), cwd: Directory.current.path);
setLogLevel(env['TRACERA_LOG_LEVEL']);
final requireReport = parseBool(env['TRACERA_REQUIRE_REPORT'], false);
final Config config;
try {
config = loadConfig(env);
} on ConfigError catch (error) {
final message = 'Tracera: ${error.message}';
if (requireReport) failRequired(message);
logWarn('$message — stopped reporting');
state.softDisabled = true;
return;
}
void softDisable(Object error) {
if (config.requireReport) failRequired(error.toString());
logWarn('Tracera: $error — stopped reporting');
state.softDisabled = true;
}
final client = Client(config);
final AppConfig app;
try {
app = await client.getAppConfig();
client.ensureCompatible(app);
} on Object catch (error) {
softDisable(error);
return;
}
var peerLabel = peerWorkerLabelFor(state.peerWorker);
if (peerLabel.isEmpty) peerLabel = peerWorkerLabel(env);
var testRunId = config.testRunId;
var owned = false;
var peerHandoff = false;
try {
if (testRunId > 0) {
// Attaching to a run someone else owns.
} else if (peerLabel.isNotEmpty) {
logDebug('Tracera: $peerLabel — sharing one test run with its peers');
final claim = await claimPeerRun(
config.projectId,
config.runTitle,
client.createRun,
);
testRunId = claim.runId;
peerHandoff = true;
} else {
testRunId = await client.createRun();
owned = true;
}
} on Object catch (error) {
softDisable(error);
return;
}
final session = Session(
client: client,
config: config,
testRunId: testRunId,
ownedByAdapter: owned,
peerHandoff: peerHandoff,
);
session.peerFinisher = peerHandoff && state.peerFinisher;
state._session = session;
state._buffer = ResultBuffer(session);
state._uploadPool = JobPool(config.uploadConcurrency);
logDebug(
owned
? 'Tracera: created test run #$testRunId'
: 'Tracera: using existing test run #$testRunId',
);
}