Skip to content

Commit 417b5f6

Browse files
committed
fix: resolve issues causing failures
Signed-off-by: Nathan Klick <[email protected]>
1 parent 0dfae4c commit 417b5f6

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

src/core/lease.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ export class Lease {
161161
try {
162162
return await this.client.readNamespacedLease(this.LeaseName, this.Namespace)
163163
} catch (e: any) {
164+
if (!(e instanceof SoloError)) {
165+
throw new LeaseAcquisitionError(`failed to read the lease named '${this.LeaseName}' in the ` +
166+
`'${this.Namespace}' namespace, caused by: ${e.message}`, e)
167+
}
168+
164169
if (e.meta.statusCode !== 404) {
165170
throw new LeaseAcquisitionError('failed to read existing leases, unexpected server response of' +
166171
`'${e.meta.statusCode}' received`, e)

src/core/lease_manager.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import type { SoloLogger } from './logging.ts'
2222
import { type LeaseRenewalService } from './lease_renewal.js'
2323
import { Lease } from './lease.js'
2424
import { LeaseHolder } from './lease_holder.js'
25+
import { LeaseAcquisitionError } from './lease_errors.js'
2526

2627
export class LeaseManager {
2728
constructor (
@@ -37,7 +38,7 @@ export class LeaseManager {
3738
}
3839

3940
public async create (): Promise<Lease> {
40-
return new Lease(this.k8, this.renewalService, LeaseHolder.default(), await this.currentNamespace(), await this.currentNamespace())
41+
return new Lease(this.k8, this.renewalService, LeaseHolder.default(), await this.currentNamespace())
4142
}
4243

4344
public get RenewalService (): LeaseRenewalService {
@@ -57,7 +58,7 @@ export class LeaseManager {
5758
await this.k8.createNamespace(namespace)
5859

5960
if (!await this.k8.hasNamespace(namespace)) {
60-
throw new SoloError(`failed to create the '${namespace}' namespace`)
61+
throw new LeaseAcquisitionError(`failed to create the '${namespace}' namespace`)
6162
}
6263
}
6364

src/core/listr_lease.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { LeaseAcquisitionError } from './lease_errors.js'
2222

2323
export class ListrLease {
2424
public static readonly DEFAULT_LEASE_ACQUIRE_ATTEMPTS = 10
25+
public static readonly ACQUIRE_LEASE_TASK_TITLE = 'Acquire lease'
2526

2627
private constructor () {
2728
throw new Error('This class cannot be instantiated')
@@ -30,7 +31,7 @@ export class ListrLease {
3031
public static newAcquireLeaseTask (lease: Lease, task: ListrTaskWrapper<any, any, any>) {
3132
return task.newListr([
3233
{
33-
title: 'Acquire lease',
34+
title: ListrLease.ACQUIRE_LEASE_TASK_TITLE,
3435
task: async (_, task) => {
3536
await ListrLease.acquireWithRetry(lease, task)
3637
}
@@ -49,17 +50,17 @@ export class ListrLease {
4950

5051
let attempt: number
5152
let innerError: Error | null = null
52-
for (attempt = 1; attempt <= maxAttempts; attempt++) {
53+
for (attempt = 0; attempt < maxAttempts; attempt++) {
5354
try {
5455
await lease.acquire()
5556
task.title = `${title} - ${chalk.green('lease acquired successfully')}` +
56-
`, attempt: ${chalk.cyan(attempt.toString())}/${chalk.cyan(maxAttempts.toString())}`
57+
`, attempt: ${chalk.cyan((attempt + 1).toString())}/${chalk.cyan(maxAttempts.toString())}`
5758
return
5859
} catch (e: LeaseAcquisitionError | any) {
5960
task.title = `${title} - ${chalk.gray(`lease exists, attempting again in ${lease.DurationSeconds} seconds`)}` +
60-
`, attempt: ${chalk.cyan(attempt.toString())}/${chalk.cyan(maxAttempts.toString())}`
61+
`, attempt: ${chalk.cyan((attempt + 1).toString())}/${chalk.cyan(maxAttempts.toString())}`
6162

62-
if (attempt === maxAttempts) {
63+
if (attempt === maxAttempts - 1) {
6364
innerError = e
6465
}
6566
}

0 commit comments

Comments
 (0)