Skip to content

Commit

Permalink
fix: teach idle to wait for db
Browse files Browse the repository at this point in the history
  • Loading branch information
eseidel committed Aug 11, 2024
1 parent 5836843 commit 30c4178
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
24 changes: 11 additions & 13 deletions packages/cli/bin/idle_queue.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@ import 'package:cli/cli.dart';
import 'package:cli/logic/idle_queue.dart';
import 'package:cli/net/auth.dart';

Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
final api = await defaultApi(db, getPriority: () => networkPriorityLow);

var agentSymbol = await db.getAgentSymbol();
while (agentSymbol == null) {
logger.info('No agent symbol found in database, waiting 1 minute.');
Future<T> waitFor<T>(Database db, Future<T?> Function() get) async {
var value = await get();
while (value == null) {
logger.info('$T not yet in database, waiting 1 minute.');
await Future<void>.delayed(const Duration(minutes: 1));
agentSymbol = await db.getAgentSymbol();
value = await get();
}
return value;
}

var agent = await db.getAgent(symbol: agentSymbol);
while (agent == null) {
logger.info('Agent not yet found in database, waiting 1 minute.');
await Future<void>.delayed(const Duration(minutes: 1));
agent = await db.getAgent(symbol: agentSymbol);
}
Future<void> command(FileSystem fs, Database db, ArgResults argResults) async {
final api = await waitForApi(db, getPriority: () => networkPriorityLow);
final agentSymbol = await waitFor(db, () => db.getAgentSymbol());
final agent = await waitFor(db, () => db.getAgent(symbol: agentSymbol));

final systemSymbol = agent.headquarters.system;
var queue = IdleQueue();
Expand Down
13 changes: 13 additions & 0 deletions packages/cli/lib/net/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ Api apiFromAuthToken(
return Api(apiClient);
}

/// Waits for the auth token to be available and then creates an API.
Future<Api> waitForApi(
Database db, {
int Function() getPriority = defaultGetPriority,
}) async {
var token = await db.getAuthToken();
while (token == null) {
await Future<void>.delayed(const Duration(minutes: 1));
token = await db.getAuthToken();
}
return apiFromAuthToken(token, db, getPriority: getPriority);
}

/// defaultApi creates an Api with the default auth token read from the
/// given file system.
Future<Api> defaultApi(
Expand Down

0 comments on commit 30c4178

Please sign in to comment.