Skip to content

Commit bde74d9

Browse files
authored
Make the scan timeout configurable via argument (#40)
1 parent 0f45dd4 commit bde74d9

5 files changed

Lines changed: 17 additions & 5 deletions

File tree

docs/configcat-scan.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ configcat scan ./dir -c <config-id> -l 5 --print
1313
| ------ | ----------- |
1414
| `--config-id`, `-c` | ID of the Config to scan against |
1515
| `--line-count`, `-l` | Context line count before and after the reference line (min: 1, max: 10) |
16+
| `--timeout`, `-to` | Scan timeout in seconds (default: 1800, min: 60) |
1617
| `--print`, `-p` | Print found references to output |
1718
| `--upload`, `-u` | Upload references to ConfigCat |
1819
| `--repo`, `-r` | Repository name. Mandatory for code reference upload |

src/ConfigCat.Cli.Services/Scan/FileScanner.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Task<IEnumerable<FlagReferenceResult>> ScanAsync(FlagModel[] flags,
1919
string[] matchPatterns,
2020
string[] usagePatterns,
2121
int contextLines,
22+
TimeSpan timeout,
2223
List<string> warningTracker,
2324
CancellationToken token);
2425
}
@@ -40,17 +41,18 @@ public FileScanner(IReferenceCollector referenceCollector,
4041
this.aliasCollector = aliasCollector;
4142
this.botPolicy = botPolicy;
4243
this.output = output;
43-
this.botPolicy.Configure(p => p.Timeout(t => t.After(TimeSpan.FromMinutes(30))));
4444
}
4545

4646
public async Task<IEnumerable<FlagReferenceResult>> ScanAsync(FlagModel[] flags,
4747
FileInfo[] filesToScan,
4848
string[] matchPatterns,
4949
string[] usagePatterns,
5050
int contextLines,
51+
TimeSpan timeout,
5152
List<string> warningTracker,
5253
CancellationToken token)
5354
{
55+
this.botPolicy.Configure(p => p.Timeout(t => t.After(timeout)));
5456
using var spinner = this.output.CreateSpinner(token);
5557
return await this.botPolicy.ExecuteAsync(async (_, cancellation) =>
5658
{

src/ConfigCat.Cli/CommandBuilder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,6 +1293,7 @@ private static CommandDescriptor BuildScanCommand() =>
12931293
{
12941294
new Option<string>(["--config-id", "-c"], "ID of the Config to scan against"),
12951295
new Option<int>(["--line-count", "-l"], () => 4, "Context line count before and after the reference line (min: 1, max: 10)"),
1296+
new Option<int>(["--timeout", "-to"], () => 1800, "Scan timeout in seconds (default: 1800, min: 60)"),
12961297
new Option<bool>(["--print", "-p"], "Print found references to output"),
12971298
new Option<bool>(["--upload", "-u"], "Upload references to ConfigCat"),
12981299
new Option<string>(["--repo", "-r"], "Repository name. Mandatory for code reference upload"),

src/ConfigCat.Cli/CommandDescriptor.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ public class CommandDescriptor(string name, string description, string example =
1616

1717
public bool IsHidden { get; init; }
1818

19-
public IEnumerable<Option> Options { get; init; } = Enumerable.Empty<Option>();
19+
public IEnumerable<Option> Options { get; init; } = [];
2020

21-
public IEnumerable<Argument> Arguments { get; init; } = Enumerable.Empty<Argument>();
21+
public IEnumerable<Argument> Arguments { get; init; } = [];
2222

23-
public IEnumerable<string> Aliases { get; init; } = Enumerable.Empty<string>();
23+
public IEnumerable<string> Aliases { get; init; } = [];
2424

25-
public IEnumerable<CommandDescriptor> SubCommands { get; init; } = Enumerable.Empty<CommandDescriptor>();
25+
public IEnumerable<CommandDescriptor> SubCommands { get; init; } = [];
2626

2727
public HandlerDescriptor Handler { get; init; }
2828
}

src/ConfigCat.Cli/Commands/Scan.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public async Task<int> InvokeAsync(DirectoryInfo directory,
4747
string[] aliasPatterns,
4848
string[] usagePatterns,
4949
string[] excludeFlagKeys,
50+
int timeout,
5051
CancellationToken token)
5152
{
5253
if (upload && repo.IsEmpty())
@@ -67,6 +68,10 @@ public async Task<int> InvokeAsync(DirectoryInfo directory,
6768
lineCount = lineCount is < 0 or > 10
6869
? 4
6970
: lineCount;
71+
72+
timeout = timeout < 60
73+
? 60
74+
: timeout;
7075

7176
var flags = await flagClient.GetFlagsAsync(configId, token);
7277
if (excludeFlagKeys is {Length: > 0})
@@ -78,6 +83,8 @@ public async Task<int> InvokeAsync(DirectoryInfo directory,
7883
if (excludeFlagKeys is {Length: > 0})
7984
deletedFlags = deletedFlags.Where(f => !excludeFlagKeys.Contains(f.Key));
8085

86+
output.Verbose($"Scanning {directory} with {timeout}s timeout");
87+
8188
var gitRepoDir = await gitClient.GetRepoRootDirectoryOrNull(directory);
8289

8390
var files = await fileCollector.CollectAsync(directory, gitRepoDir, token);
@@ -92,6 +99,7 @@ public async Task<int> InvokeAsync(DirectoryInfo directory,
9299
patternsFromEnv.Concat(aliasPatterns).ToArray(),
93100
usagePatternsFromEnv.Concat(usagePatterns).ToArray(),
94101
lineCount,
102+
TimeSpan.FromSeconds(timeout),
95103
warningTracker,
96104
token);
97105

0 commit comments

Comments
 (0)