Skip to content
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 2 commits into from
Jul 7, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion platform/plugins/pid/PidFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ export class PidFile {
this.log.debug(`deleting pid file [${path}]`);
unlinkSync(path);
}
}
}
25 changes: 12 additions & 13 deletions platform/plugins/pid/PidService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

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)?

Copy link
Contributor Author

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:

screen shot 2017-07-07 at 18 09 41

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 specified logging.level: debug first in the kibana config to see the debug logs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then sighup the process (kill -1 it)

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 :)

Copy link
Contributor Author

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree

// is received or if `stop` is called below.)

return new Observable<PidFile | void>(observable => {
return new Observable(observable => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: observable argument is not used, so we can get rid of it.

const pid = new PidFile(process.pid, config, logger);

pid.writeFile();

return function unsubscribe() {
Expand Down