-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* migrate pid file to core * use correct log level + add comment * move `unhandledRejection` to service's setup * update comment
- Loading branch information
1 parent
d190a2a
commit cdea019
Showing
11 changed files
with
278 additions
and
86 deletions.
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
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
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
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
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 |
---|---|---|
@@ -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>; |
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 |
---|---|---|
@@ -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 [ | ||
"debug", | ||
"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 [ | ||
"debug", | ||
"wrote pid file to /pid-file", | ||
], | ||
Array [ | ||
"warn", | ||
"pid file already exists at /pid-file", | ||
], | ||
] | ||
`); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* 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.debug(`wrote pid file to ${path}`, { path, pid }); | ||
|
||
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'); | ||
}); | ||
}; |
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
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
Oops, something went wrong.