Skip to content

Commit e36b085

Browse files
feat(cmn): hard stop on multiple exit signals (#2378)
Adds a new feature to BaseServiceV2 to have a service exit immediately when 3 exit signals are received. Useful when the main loop would take a long time to exit but you want the service to exit immediately. Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 7a17900 commit e36b085

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

.changeset/fair-cows-think.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@eth-optimism/common-ts': patch
3+
---
4+
5+
Adds hard stop to BaseServiceV2 when multiple exit signals are received

packages/common-ts/src/base-service/base-service-v2.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,26 @@ export abstract class BaseServiceV2<
247247
this.logger = new Logger({ name: params.name })
248248

249249
// Gracefully handle stop signals.
250+
const maxSignalCount = 3
251+
let currSignalCount = 0
250252
const stop = async (signal: string) => {
251-
this.logger.info(`stopping service with signal`, { signal })
252-
await this.stop()
253-
process.exit(0)
253+
// Allow exiting fast if more signals are received.
254+
currSignalCount++
255+
if (currSignalCount === 1) {
256+
this.logger.info(`stopping service with signal`, { signal })
257+
await this.stop()
258+
process.exit(0)
259+
} else if (currSignalCount >= maxSignalCount) {
260+
this.logger.info(`performing hard stop`)
261+
process.exit(0)
262+
} else {
263+
this.logger.info(
264+
`send ${maxSignalCount - currSignalCount} more signal(s) to hard stop`
265+
)
266+
}
254267
}
268+
269+
// Handle stop signals.
255270
process.on('SIGTERM', stop)
256271
process.on('SIGINT', stop)
257272
}

0 commit comments

Comments
 (0)