Skip to content

Commit 3d0f360

Browse files
committed
Applies clustering graceful shutdown patch
1 parent 08c09e3 commit 3d0f360

4 files changed

Lines changed: 19 additions & 9 deletions

File tree

operations/operator-registry-controller-live.hcl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@ job "operator-registry-controller-live" {
1313

1414
update {
1515
max_parallel = 1
16-
canary = 1
1716
min_healthy_time = "30s"
1817
healthy_deadline = "5m"
19-
auto_revert = true
20-
auto_promote = true
2118
}
2219

2320
network {
@@ -28,6 +25,7 @@ job "operator-registry-controller-live" {
2825

2926
task "operator-registry-controller-live-service" {
3027
driver = "docker"
28+
kill_timeout = "30s"
3129
config {
3230
image = "ghcr.io/anyone-protocol/operator-registry-controller:[[ .commit_sha ]]"
3331
network_mode = "host"

operations/operator-registry-controller-stage.hcl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@ job "operator-registry-controller-stage" {
1313

1414
update {
1515
max_parallel = 1
16-
canary = 1
1716
min_healthy_time = "30s"
1817
healthy_deadline = "5m"
19-
auto_revert = true
20-
auto_promote = true
2118
}
2219

2320
network {
@@ -28,6 +25,7 @@ job "operator-registry-controller-stage" {
2825

2926
task "operator-registry-controller-stage-service" {
3027
driver = "docker"
28+
kill_timeout = "30s"
3129
config {
3230
network_mode = "host"
3331
image = "ghcr.io/anyone-protocol/operator-registry-controller:[[ .commit_sha ]]"

src/cluster/cluster.service.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export class ClusterService
2424
private serviceName: string
2525
private sessionId: string | null = null
2626
private consul?: Consul
27+
private renewInterval?: NodeJS.Timeout
2728

2829
constructor(
2930
private readonly config: ConfigService<{
@@ -122,7 +123,13 @@ export class ClusterService
122123
}
123124

124125
async beforeApplicationShutdown(): Promise<void> {
126+
this.logger.log('Shutting down cluster...')
127+
if (this.renewInterval) {
128+
clearInterval(this.renewInterval);
129+
this.renewInterval = undefined;
130+
}
125131
if (this.consul && this.isLocalLeader() && this.sessionId) {
132+
this.logger.log('Cleaning up leader locks...')
126133
await this.consul.session.destroy(this.sessionId)
127134
}
128135
}
@@ -135,11 +142,16 @@ export class ClusterService
135142
const { ID } = await this.consul.session.create({
136143
name: this.serviceId,
137144
ttl: '15s',
145+
behavior: 'delete',
138146
})
139147

140-
setInterval(() => {
148+
this.renewInterval = setInterval(async () =>{
141149
if (this.consul) {
142-
this.consul.session.renew(ID)
150+
try {
151+
await this.consul.session.renew(ID)
152+
} catch (error) {
153+
this.logger.error('Failed to renew consul session', error)
154+
}
143155
}
144156
}, 10000)
145157

@@ -156,7 +168,7 @@ export class ClusterService
156168
const result = await this.consul.kv.set({
157169
key: leaderKey,
158170
value: this.serviceId,
159-
acquire: this.sessionId,
171+
acquire: this.sessionId
160172
})
161173

162174
this.isLeader = result

src/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ async function bootstrap() {
2727
})
2828
})
2929

30+
app.enableShutdownHooks()
31+
3032
const port = process.env.PORT || 3000
3133
logz.info(`Listening on ${port}`)
3234

0 commit comments

Comments
 (0)