Skip to content

Commit

Permalink
Improve client wait [ci fast]
Browse files Browse the repository at this point in the history
Signed-off-by: Paolo Di Tommaso <paolo.ditommaso@gmail.com>
  • Loading branch information
pditommaso committed Apr 12, 2024
1 parent c108374 commit 6e022f1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
29 changes: 12 additions & 17 deletions plugins/nf-wave/src/main/io/seqera/wave/plugin/WaveClient.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -586,28 +586,23 @@ class WaveClient {
}
}

protected URI imageToManifestUri(String image) {
final p = image.indexOf('/')
if( p==-1 ) throw new IllegalArgumentException("Invalid container name: $image")
final result = 'https://' + image.substring(0,p) + '/v2' + image.substring(p).replace(':','/manifests/')
return new URI(result)
}

void awaitCompletion(String buildId) {
final long maxAwait = Duration.ofMinutes(15).toMillis();
final long startTime = Instant.now().toEpochMilli();
final random = new Random()
while( true ) {
if( isComplete(buildId) ) {
return;
}
while( !isComplete(buildId) ) {
if( System.currentTimeMillis()-startTime > maxAwait ) {
break;
break
}
Thread.sleep((random.nextInt(5) + 9) * 1_000)
Thread.sleep(randomRange(10,15) * 1_000)
}
}

protected static int randomRange(int min, int max) {
assert min<max
Random rand = new Random();
return rand.nextInt((max - min) + 1) + min;
}

protected boolean isComplete(String buildId) {
final String statusEndpoint = endpoint + "/v1alpha1/builds/"+buildId+"/status";
final HttpRequest req = HttpRequest.newBuilder()
Expand All @@ -617,13 +612,13 @@ class WaveClient {
.build();

final HttpResponse<String> resp = httpSend(req);
log.debug("Wave response: statusCode={}; body={}", resp.statusCode(), resp.body());
log.debug("Wave build status response: statusCode={}; body={}", resp.statusCode(), resp.body())
if( resp.statusCode()==200 ) {
final result = jsonToBuildStatusResponse(resp.body())
return result.status == BuildStatusResponse.Status.COMPLETED;
return result.status == BuildStatusResponse.Status.COMPLETED
}
else {
String msg = String.format("Wave invalid response: [%s] %s", resp.statusCode(), resp.body());
String msg = String.format("Wave invalid response: GET %s [%s] %s", statusEndpoint, resp.statusCode(), resp.body());
throw new BadResponseException(msg)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1201,4 +1201,12 @@ class WaveClientTest extends Specification {
true)
}

def 'should test range' () {
expect:
100 .times { assert WaveClient.randomRange(10, 20) >= 10 }
100 .times { assert WaveClient.randomRange(10, 20) <= 20 }
100 .times { assert WaveClient.randomRange(0, 10) <= 10 }
100 .times { assert WaveClient.randomRange(0, 10) >= 0 }
}

}

0 comments on commit 6e022f1

Please sign in to comment.