feat: introduce the new BaseServiceV2 class #2210
Conversation
🦋 Changeset detectedLatest commit: 860fef4 The changes in this PR will be included in the next version bump. This PR includes changesets to release 5 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Codecov Report
@@ Coverage Diff @@
## develop #2210 +/- ##
========================================
Coverage 80.08% 80.08%
========================================
Files 77 77
Lines 2460 2460
Branches 450 450
========================================
Hits 1970 1970
Misses 490 490
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
|
|
||
| dotenv.config() | ||
|
|
||
| const main = async () => { |
There was a problem hiding this comment.
We can entirely cut out this configuration and execution file because the new base service handles all of this for us.
| await sleep(this.options.pollingInterval) | ||
| protected async main(): Promise<void> { | ||
| // If we're already at the tip, then update the latest tip and loop again. | ||
| if (this.state.highestCheckedL2Tx > this.state.highestKnownL2Tx) { |
There was a problem hiding this comment.
I reimplemented this to take advantage of the fact that the base service now loops around main() by default, meaning we don't need to do our own looping logic.
| } | ||
| } | ||
|
|
||
| if (require.main === module) { |
There was a problem hiding this comment.
Adding this small piece of boilerplate means the service can be run by directly executing this file.
| * @param params.options Options to pass to the service. | ||
| * @param params.loops Whether or not the service should loop. Defaults to true. | ||
| * @param params.loopIntervalMs Loop interval in milliseconds. Defaults to zero. | ||
| */ |
There was a problem hiding this comment.
Note to self: I'd like a nice way of handling "service containers" -- groups of services that run together. Not sure of the best way to represent this. I'm not particularly happy with the current thing of running a service that runs other services.
a6c949b to
2bf9a98
Compare
93fc39e to
38a5944
Compare
38a5944 to
6acf7c1
Compare
cd82e91 to
04e44a4
Compare
| const program = new Command() | ||
| for (const [optionName, optionSpec] of Object.entries(params.optionsSpec)) { | ||
| program.addOption( | ||
| new Option(`--${optionName.toLowerCase()}`, `${optionSpec.desc}`).env( |
There was a problem hiding this comment.
note that this can be done with bcfg to remove a dep
There was a problem hiding this comment.
I don't think you need to define upfront bcfg which config options there are
There was a problem hiding this comment.
I did it with commander mainly so that I could get the --help command by default (you can run ts-node ./path/to/service --help and get useful help info).
| public run(): void { | ||
| const _run = async () => { | ||
| if (this.init) { | ||
| this.logger.info('initializing service') |
There was a problem hiding this comment.
Does the logger indicate which service is being initialized? This would be pretty useful info to have.
There was a problem hiding this comment.
Yeah the logger includes the name of the service in each log line, so it will be clear which service is being initialized.
f26a017 to
947783d
Compare
947783d to
2b36e73
Compare
|
✔️ Deploy Preview for optimism-sdk ready! 🔨 Explore the source changes: 2b36e73 🔍 Inspect the deploy log: https://app.netlify.com/sites/optimism-sdk/deploys/62267ffe81944400086ed6e2 😎 Browse the preview: https://deploy-preview-2210--optimism-sdk.netlify.app |
Introduces the new BaseServiceV2 class to eventually replace the older BaseService class. BaseServiceV2 includes many convenience features like automatic environment variable and argv parsing.
ProviderLike is too loose because it allows "any" to be ProviderLike. This tightens the type to only include strings or ethers.Provider objects.
Rewrites the message-relayer service to use BaseServiceV2. Significantly reduces the footprint of the message-relayer and enforces stronger input validation.
2b36e73 to
860fef4
Compare
Description
This PR introduces a new
BaseServiceV2class.BaseServiceV2is a more advanced version of the originalBaseServiceclass with the following key advantages over the original:BaseServiceV2automatically picks names of environment variables and command-line arguments based on the service's defined options.BaseServiceV2automatically pulls inputs from the environment,.envfiles, and command-line arguments as well as the normal options object passed to the service constructor. This means that services based onBaseServiceV2can be started without an extra boilerplaterun.tsfile that parses variables.BaseServiceV2requires that all options come with validators so inputs can be checked before the service starts. No more input validation outside of the service.BaseServiceV2has built-in looping, removing the need to include looping logic within every long-running service (but still has the option to only run once withloop: false).BaseServiceV2includes an optionalinitfunction that allows services to run once-on-init logic reliably.