Skip to content

Commit

Permalink
Merge pull request #575 from atsign-foundation/gkc/add_max_connect_at…
Browse files Browse the repository at this point in the history
…tempts

feat: at_cli_commons 1.1.0 : CLIBase: add maxConnectAttempts parameter
  • Loading branch information
gkc authored May 7, 2024
2 parents e208637 + 686fae4 commit a4b8c94
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 23 deletions.
18 changes: 18 additions & 0 deletions packages/at_cli_commons/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
## 1.1.0

- feat: Add `maxConnectAttempts` parameter to CLIBase. The default is 20,
i.e. 20 attempts to connect, with a 3-second delay between attempts. When
used in scripts this is important, as the previous behaviour (retry
forever) is usually not what is required.

## 1.0.5

- fix: Make CLIBase write progress messages to stderr, not stdout

## 1.0.4

- fix: handle malformed atsigns (no leading `@`) in CLIBase constructor
- build: updated dependencies

## 1.0.3

- Added `example/` package, moved code samples from `bin/` to `example/`

## 1.0.2

- docs: Added some code samples in bin/ directory
- docs: Added some class and method documentation to CLIBase
- docs: Updated README
- feat: Added static `fromCommandLineArgs` factory method to CLIBase

## 1.0.1

- Small edits to README

## 1.0.0

- Initial version.
62 changes: 40 additions & 22 deletions packages/at_cli_commons/lib/src/cli_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:logging/logging.dart';
import 'package:version/version.dart';

class CLIBase {
static const defaultMaxConnectAttempts = 20;
/// An ArgParser which has all of the options and flags required by [CLIBase]
/// Used by [fromCommandLineArgs] if the `parser` parameter isn't supplied.
static final ArgParser argsParser = ArgParser()
Expand All @@ -35,7 +36,11 @@ class CLIBase {
help: 'Root Domain',
defaultsTo: 'root.atsign.org')
..addFlag('verbose', abbr: 'v', negatable: false, help: 'More logging')
..addFlag('never-sync', negatable: false, help: 'Do not run sync');
..addFlag('never-sync', negatable: false, help: 'Do not run sync')
..addOption('max-connect-attempts',
help: 'Number of times to attempt to initially connect to atServer.'
' Note: there is a 3-second delay between connection attempts.',
defaultsTo: defaultMaxConnectAttempts.toString());

/// Constructs a CLIBase from a list of command-line arguments
/// and calls [init] on it.
Expand All @@ -61,15 +66,17 @@ class CLIBase {
}

CLIBase cliBase = CLIBase(
atSign: parsedArgs['atsign'],
atKeysFilePath: parsedArgs['key-file'],
nameSpace: parsedArgs['namespace'],
rootDomain: parsedArgs['root-domain'],
homeDir: getHomeDirectory(),
storageDir: parsedArgs['storage-dir'],
verbose: parsedArgs['verbose'],
cramSecret: parsedArgs['cram-secret'],
syncDisabled: parsedArgs['never-sync']);
atSign: parsedArgs['atsign'],
atKeysFilePath: parsedArgs['key-file'],
nameSpace: parsedArgs['namespace'],
rootDomain: parsedArgs['root-domain'],
homeDir: getHomeDirectory(),
storageDir: parsedArgs['storage-dir'],
verbose: parsedArgs['verbose'],
cramSecret: parsedArgs['cram-secret'],
syncDisabled: parsedArgs['never-sync'],
maxConnectAttempts: int.parse(parsedArgs['max-connect-attempts']),
);

await cliBase.init();

Expand All @@ -85,6 +92,7 @@ class CLIBase {
final String? downloadDir;
final String? cramSecret;
final bool syncDisabled;
final int maxConnectAttempts;

late final String atKeysFilePathToUse;
late final String localStoragePathToUse;
Expand All @@ -111,17 +119,19 @@ class CLIBase {
/// cliBase.logger.logger.level = Level.FINEST;
/// ```
/// Throws an [IllegalArgumentException] if the parameters fail validation.
CLIBase(
{required String atSign,
required this.nameSpace,
required this.rootDomain,
this.homeDir,
this.verbose = false,
this.atKeysFilePath,
this.storageDir,
this.downloadDir,
this.cramSecret,
this.syncDisabled = false}) {
CLIBase({
required String atSign,
required this.nameSpace,
required this.rootDomain,
this.homeDir,
this.verbose = false,
this.atKeysFilePath,
this.storageDir,
this.downloadDir,
this.cramSecret,
this.syncDisabled = false,
this.maxConnectAttempts = defaultMaxConnectAttempts,
}) {
this.atSign = AtUtils.fixAtSign(atSign);
if (homeDir == null) {
if (atKeysFilePath == null) {
Expand Down Expand Up @@ -188,9 +198,11 @@ class CLIBase {

bool authenticated = false;
Duration retryDuration = Duration(seconds: 3);
while (!authenticated) {
int attempts = 0;
while (!authenticated && attempts < maxConnectAttempts) {
try {
stderr.write(chalk.brightBlue('\r\x1b[KConnecting ... '));
attempts++;
await Future.delayed(Duration(
milliseconds:
1000)); // Pause just long enough for the retry to be visible
Expand All @@ -203,6 +215,12 @@ class CLIBase {
await Future.delayed(retryDuration);
}
}
if (!authenticated) {
stderr.writeln();
var msg = 'Failed to connect after $attempts attempts';
stderr.writeln(chalk.brightRed(msg));
throw SecondaryServerConnectivityException(msg);
}
stderr.writeln(chalk.brightGreen('Connected'));

// Get the AtClient which the onboardingService just authenticated
Expand Down
2 changes: 1 addition & 1 deletion packages/at_cli_commons/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: at_cli_commons
description: Library of useful stuff when building cli programs which use the AtClient SDK
version: 1.0.5
version: 1.1.0

repository: https://github.com/atsign-foundation/at_libraries/tree/trunk/packages/at_cli_commons
homepage: https://docs.atsign.com/
Expand Down

0 comments on commit a4b8c94

Please sign in to comment.