-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
migrate pid file to core #77037
migrate pid file to core #77037
Changes from 2 commits
6914c15
ce9d900
fec23e4
eb0950c
72720df
47c5d5b
25ecc6a
7bd512d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { TypeOf, schema } from '@kbn/config-schema'; | ||
|
||
export const config = { | ||
path: 'pid', | ||
schema: schema.object({ | ||
file: schema.maybe(schema.string()), | ||
exclusive: schema.boolean({ defaultValue: false }), | ||
}), | ||
}; | ||
|
||
export type PidConfigType = TypeOf<typeof config.schema>; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { writeFile, exists } from './fs'; | ||
import { writePidFile } from './write_pid_file'; | ||
import { loggingSystemMock } from '../logging/logging_system.mock'; | ||
|
||
jest.mock('./fs', () => ({ | ||
writeFile: jest.fn(), | ||
exists: jest.fn(), | ||
})); | ||
|
||
const writeFileMock = writeFile as jest.MockedFunction<typeof writeFile>; | ||
const existsMock = exists as jest.MockedFunction<typeof exists>; | ||
|
||
const pid = String(process.pid); | ||
|
||
describe('writePidFile', () => { | ||
let logger: ReturnType<typeof loggingSystemMock.createLogger>; | ||
|
||
beforeEach(() => { | ||
logger = loggingSystemMock.createLogger(); | ||
jest.spyOn(process, 'once'); | ||
|
||
writeFileMock.mockImplementation(() => Promise.resolve()); | ||
existsMock.mockImplementation(() => Promise.resolve(false)); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
const allLogs = () => | ||
Object.entries(loggingSystemMock.collect(logger)).reduce((messages, [key, value]) => { | ||
return [...messages, ...(key === 'log' ? [] : (value as any[]).map(([msg]) => [key, msg]))]; | ||
}, [] as any[]); | ||
|
||
it('does nothing if `pid.file` is not set', async () => { | ||
await writePidFile({ | ||
pidConfig: { | ||
file: undefined, | ||
exclusive: false, | ||
}, | ||
logger, | ||
}); | ||
expect(writeFile).not.toHaveBeenCalled(); | ||
expect(process.once).not.toHaveBeenCalled(); | ||
expect(allLogs()).toMatchInlineSnapshot(`Array []`); | ||
}); | ||
|
||
it('writes the pid file to `pid.file`', async () => { | ||
existsMock.mockResolvedValue(false); | ||
|
||
await writePidFile({ | ||
pidConfig: { | ||
file: '/pid-file', | ||
exclusive: false, | ||
}, | ||
logger, | ||
}); | ||
|
||
expect(writeFile).toHaveBeenCalledTimes(1); | ||
expect(writeFile).toHaveBeenCalledWith('/pid-file', pid); | ||
|
||
expect(process.once).toHaveBeenCalledTimes(2); | ||
expect(process.once).toHaveBeenCalledWith('exit', expect.any(Function)); | ||
expect(process.once).toHaveBeenCalledWith('SIGINT', expect.any(Function)); | ||
|
||
expect(allLogs()).toMatchInlineSnapshot(` | ||
Array [ | ||
Array [ | ||
"info", | ||
"wrote pid file to /pid-file", | ||
], | ||
] | ||
`); | ||
}); | ||
|
||
it('throws an error if the file exists and `pid.exclusive is true`', async () => { | ||
existsMock.mockResolvedValue(true); | ||
|
||
await expect( | ||
writePidFile({ | ||
pidConfig: { | ||
file: '/pid-file', | ||
exclusive: true, | ||
}, | ||
logger, | ||
}) | ||
).rejects.toThrowErrorMatchingInlineSnapshot(`"pid file already exists at /pid-file"`); | ||
|
||
expect(writeFile).not.toHaveBeenCalled(); | ||
expect(process.once).not.toHaveBeenCalled(); | ||
expect(allLogs()).toMatchInlineSnapshot(`Array []`); | ||
}); | ||
|
||
it('logs a warning if the file exists and `pid.exclusive` is false', async () => { | ||
existsMock.mockResolvedValue(true); | ||
|
||
await writePidFile({ | ||
pidConfig: { | ||
file: '/pid-file', | ||
exclusive: false, | ||
}, | ||
logger, | ||
}); | ||
|
||
expect(writeFile).toHaveBeenCalledTimes(1); | ||
expect(writeFile).toHaveBeenCalledWith('/pid-file', pid); | ||
|
||
expect(process.once).toHaveBeenCalledTimes(2); | ||
expect(process.once).toHaveBeenCalledWith('exit', expect.any(Function)); | ||
expect(process.once).toHaveBeenCalledWith('SIGINT', expect.any(Function)); | ||
|
||
expect(allLogs()).toMatchInlineSnapshot(` | ||
Array [ | ||
Array [ | ||
"info", | ||
"wrote pid file to /pid-file", | ||
], | ||
Array [ | ||
"warn", | ||
"pid file already exists at /pid-file", | ||
], | ||
] | ||
`); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,68 @@ | ||||||||||||||||
/* | ||||||||||||||||
* Licensed to Elasticsearch B.V. under one or more contributor | ||||||||||||||||
* license agreements. See the NOTICE file distributed with | ||||||||||||||||
* this work for additional information regarding copyright | ||||||||||||||||
* ownership. Elasticsearch B.V. licenses this file to you under | ||||||||||||||||
* the Apache License, Version 2.0 (the "License"); you may | ||||||||||||||||
* not use this file except in compliance with the License. | ||||||||||||||||
* You may obtain a copy of the License at | ||||||||||||||||
* | ||||||||||||||||
* http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||
* | ||||||||||||||||
* Unless required by applicable law or agreed to in writing, | ||||||||||||||||
* software distributed under the License is distributed on an | ||||||||||||||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||||||||||||||||
* KIND, either express or implied. See the License for the | ||||||||||||||||
* specific language governing permissions and limitations | ||||||||||||||||
* under the License. | ||||||||||||||||
*/ | ||||||||||||||||
|
||||||||||||||||
import { unlinkSync as unlink } from 'fs'; | ||||||||||||||||
import once from 'lodash/once'; | ||||||||||||||||
import { Logger } from '../logging'; | ||||||||||||||||
import { writeFile, exists } from './fs'; | ||||||||||||||||
import { PidConfigType } from './pid_config'; | ||||||||||||||||
|
||||||||||||||||
export const writePidFile = async ({ | ||||||||||||||||
pidConfig, | ||||||||||||||||
logger, | ||||||||||||||||
}: { | ||||||||||||||||
pidConfig: PidConfigType; | ||||||||||||||||
logger: Logger; | ||||||||||||||||
}) => { | ||||||||||||||||
const path = pidConfig.file; | ||||||||||||||||
if (!path) { | ||||||||||||||||
return; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
const pid = String(process.pid); | ||||||||||||||||
|
||||||||||||||||
if (await exists(path)) { | ||||||||||||||||
const message = `pid file already exists at ${path}`; | ||||||||||||||||
if (pidConfig.exclusive) { | ||||||||||||||||
throw new Error(message); | ||||||||||||||||
} else { | ||||||||||||||||
logger.warn(message, { path, pid }); | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
await writeFile(path, pid); | ||||||||||||||||
|
||||||||||||||||
logger.info(`wrote pid file to ${path}`, { path, pid }); | ||||||||||||||||
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. It used to have |
||||||||||||||||
|
||||||||||||||||
const clean = once(() => { | ||||||||||||||||
unlink(path); | ||||||||||||||||
}); | ||||||||||||||||
|
||||||||||||||||
process.once('exit', clean); // for "natural" exits | ||||||||||||||||
process.once('SIGINT', () => { | ||||||||||||||||
// for Ctrl-C exits | ||||||||||||||||
clean(); | ||||||||||||||||
// resend SIGINT | ||||||||||||||||
process.kill(process.pid, 'SIGINT'); | ||||||||||||||||
}); | ||||||||||||||||
|
||||||||||||||||
process.on('unhandledRejection', function (reason) { | ||||||||||||||||
logger.warn(`Detected an unhandled Promise rejection.\n${reason}`); | ||||||||||||||||
}); | ||||||||||||||||
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. This was in the legacy file, so I kept it for now, but I honestly have no idea why this was here. This seems already handled by kibana/src/setup_node_env/exit_on_warning.js Lines 39 to 45 in bf04235
@spalger Am I safe to remove this from this file? 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. I wonder if this is here so that warnings are included in the standard kibana log, rather than only written to the console, once the logger is setup. That said, both of these listeners do seem to be setup. I'd ask @watson just to be sure though. (sorry for the late handoff, was out last week) 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. I must have missed the original The intention with the So in production, the listener in this file is still active (though output depends on log-level). Whether or not we want this in production is a good question that I don't know the answer to. It's worth noting that Node.js it self will output the following to STDERR in case there's no
So adding the listener in this file means that we'll suppress that default behaviour even if log-level is set below Side note: Starting in Node.js 15, the process will crash by default if an 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. Thanks for the answer. To avoid changing the behavior that was present in legacy, I will keep this now. |
||||||||||||||||
}; |
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 guess it's not worth the effort to test
process.exit
&SIGINT
, so that is ok