Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
7af146da
Unverified
Commit
7af146da
authored
Mar 08, 2022
by
smartcontracts
Committed by
GitHub
Mar 08, 2022
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2294 from ethereum-optimism/sc/cmn-base-service-stop
feat(cmn): gracefully handle BaseServiceV2 exits
parents
5eb81ec4
b3f9bdef
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
65 additions
and
25 deletions
+65
-25
purple-peaches-serve.md
.changeset/purple-peaches-serve.md
+5
-0
base-service-v2.ts
packages/common-ts/src/base-service/base-service-v2.ts
+60
-25
No files found.
.changeset/purple-peaches-serve.md
0 → 100644
View file @
7af146da
---
'
@eth-optimism/common-ts'
:
patch
---
Have BaseServiceV2 gracefully catch exit signals
packages/common-ts/src/base-service/base-service-v2.ts
View file @
7af146da
...
...
@@ -52,6 +52,16 @@ export abstract class BaseServiceV2<
*/
protected
loopIntervalMs
:
number
/**
* Whether or not the service is currently running.
*/
protected
running
:
boolean
/**
* Whether or not the service has run to completion.
*/
protected
done
:
boolean
/**
* Logger class for this service.
*/
...
...
@@ -181,43 +191,68 @@ export abstract class BaseServiceV2<
},
{})
as
TMetrics
this
.
logger
=
new
Logger
({
name
:
params
.
name
})
// Gracefully handle stop signals.
const
stop
=
async
(
signal
:
string
)
=>
{
this
.
logger
.
info
(
`stopping service`
,
{
signal
})
await
this
.
stop
()
process
.
exit
(
0
)
}
process
.
on
(
'
SIGTERM
'
,
stop
)
process
.
on
(
'
SIGINT
'
,
stop
)
}
/**
* Runs the main function. If this service is set up to loop, will repeatedly loop around the
* main function. Will also catch unhandled errors.
*/
public
run
():
void
{
const
_run
=
async
()
=>
{
if
(
this
.
init
)
{
this
.
logger
.
info
(
'
initializing service
'
)
await
this
.
init
()
this
.
logger
.
info
(
'
service initialized
'
)
}
public
async
run
():
Promise
<
void
>
{
this
.
done
=
false
if
(
this
.
loop
)
{
this
.
logger
.
info
(
'
starting main loop
'
)
while
(
true
)
{
try
{
await
this
.
main
()
}
catch
(
err
)
{
this
.
logger
.
error
(
'
caught an unhandled exception
'
,
{
message
:
err
.
message
,
stack
:
err
.
stack
,
code
:
err
.
code
,
})
}
// Always sleep between loops
if
(
this
.
init
)
{
this
.
logger
.
info
(
'
initializing service
'
)
await
this
.
init
()
this
.
logger
.
info
(
'
service initialized
'
)
}
if
(
this
.
loop
)
{
this
.
logger
.
info
(
'
starting main loop
'
)
this
.
running
=
true
while
(
this
.
running
)
{
try
{
await
this
.
main
()
}
catch
(
err
)
{
this
.
logger
.
error
(
'
caught an unhandled exception
'
,
{
message
:
err
.
message
,
stack
:
err
.
stack
,
code
:
err
.
code
,
})
}
// Sleep between loops if we're still running (service not stopped).
if
(
this
.
running
)
{
await
sleep
(
this
.
loopIntervalMs
)
}
}
else
{
this
.
logger
.
info
(
'
running main function
'
)
await
this
.
main
()
}
}
else
{
this
.
logger
.
info
(
'
running main function
'
)
await
this
.
main
()
}
_run
()
this
.
done
=
true
}
/**
* Tries to gracefully stop the service. Service will continue running until the current loop
* iteration is finished and will then stop looping.
*/
public
async
stop
():
Promise
<
void
>
{
this
.
running
=
false
// Wait until the main loop has finished.
while
(
!
this
.
done
)
{
await
sleep
(
1000
)
}
}
/**
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment