-
Notifications
You must be signed in to change notification settings - Fork 8.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cleaner Pid observable code #12702
Merged
Merged
Cleaner Pid observable code #12702
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,4 +47,4 @@ export class PidFile { | |
this.log.debug(`deleting pid file [${path}]`); | ||
unlinkSync(path); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,35 +6,34 @@ import { PidFile } from './PidFile'; | |
import { LoggerFactory } from '../../logger'; | ||
|
||
export class PidService { | ||
private readonly pid$: Observable<PidFile | void>; | ||
private readonly pid$: Observable<undefined>; | ||
private subscription?: Subscription; | ||
|
||
constructor(pidConfig$: Observable<PidConfig | void>, logger: LoggerFactory) { | ||
constructor( | ||
pidConfig$: Observable<PidConfig | undefined>, | ||
logger: LoggerFactory | ||
) { | ||
this.pid$ = pidConfig$ | ||
.map( | ||
config => | ||
config !== undefined | ||
? new PidFile(process.pid, config, logger) | ||
: undefined | ||
) | ||
// Explanation of `switchMap`: | ||
// It's kinda like a normal `flatMap`, except it's producing observables | ||
// and you _only_ care about the latest observable it produced. It's | ||
// usually used if you need to control what happens both when you create | ||
// and when you're done with an observable, like here where we want to | ||
// write the pid file we receive a pid config, and delete it when we | ||
// receive new config values (or when we stop the pid service). | ||
.switchMap(pid => { | ||
if (pid === undefined) { | ||
// If pid is not specified, we return an observable that does nothing | ||
return new Observable<PidFile | void>(noop); | ||
.switchMap(config => { | ||
if (config === undefined) { | ||
// If there is no pid config we return an observable that does nothing | ||
return new Observable(noop); | ||
} | ||
|
||
// Otherwise we return an observable that writes the pid when | ||
// subscribed to and deletes it when unsubscribed (e.g. if new config | ||
// is received or if `stop` is called below.) | ||
|
||
return new Observable<PidFile | void>(observable => { | ||
return new Observable(observable => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
const pid = new PidFile(process.pid, config, logger); | ||
|
||
pid.writeFile(); | ||
|
||
return function unsubscribe() { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: does
e.g. if new config is received
mean I can change pid file path on-the-fly and old file will be deleted and new one created? Is it supposed to work at the moment (didn't manage to see that behaviour)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, it's doing that now:
I added some newlines here, and performed the sighup in another window. But just change the config, then sighup the process (
kill -1
it), and it should re-apply the config. I specifiedlogging.level: debug
first in the kibana config to see the debug logs.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, that the reason, I didn't do that (for some reason I thought we watch the config file for changes all the time), thanks :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Watching is "less safe" as you can be in intermediate states when it triggers. E.g. if you're moving stuff around in the config and hit save at some point (e.g. my IDE that saves whenever I cmd-tab away). It's better to let the user actively notify that the config is ready.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree