This repository has been archived by the owner on Oct 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add stop command to unmock-server. (#374)
* Add stop command by writing server PID to file. * Fix prettier. * Add simple test. * Fix prettier. * Fix deletion in test. * Delete server PID file when process exits. * Clean-up.
- Loading branch information
Kimmo Sääskilahti
committed
Jan 28, 2020
1 parent
5e21b98
commit c2a3d21
Showing
7 changed files
with
161 additions
and
2 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
Empty 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,15 @@ | ||
import * as path from "path"; | ||
import { deletePidFile, readPidIfExists, writePid } from "../pid"; | ||
|
||
const LOCAL_CONFIG_DIRECTORY = path.resolve(__dirname, ".unmock"); | ||
|
||
afterEach(() => { | ||
deletePidFile(LOCAL_CONFIG_DIRECTORY); | ||
}); | ||
|
||
it("should write process PID to given file and read it", () => { | ||
const pid = process.pid; | ||
writePid(LOCAL_CONFIG_DIRECTORY); | ||
const writtenPid = readPidIfExists(LOCAL_CONFIG_DIRECTORY); | ||
expect(writtenPid).toBe(pid); | ||
}); |
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,44 @@ | ||
import { Command, flags } from "@oclif/command"; | ||
import debug from "debug"; | ||
import { readPidIfExists, TERM_SIGNAL } from "../pid"; | ||
|
||
const debugLog = debug("unmock-server:stop"); | ||
const log = (...args: any[]) => console.log(...args); // tslint:disable-line | ||
|
||
export default class Stop extends Command { | ||
public static description = "Stop unmock server and proxy."; | ||
|
||
public static examples = [ | ||
`$ unmock-server stop | ||
`, | ||
]; | ||
|
||
public static flags = { | ||
help: flags.help({ char: "h" }), | ||
}; | ||
|
||
public static args = []; | ||
|
||
/** | ||
* Server process is stopped by sending a SIGTERM to the process PID written to the file-system. | ||
*/ | ||
public async run() { | ||
const pidOrNull = readPidIfExists(); | ||
|
||
if (pidOrNull === null) { | ||
log("Server not running."); | ||
return; | ||
} | ||
|
||
try { | ||
process.kill(pidOrNull, 0); | ||
} catch (ex) { | ||
log("Server not running."); | ||
return; | ||
} | ||
|
||
debugLog("Sending %s to PID %d", TERM_SIGNAL, pidOrNull); | ||
process.kill(pidOrNull, TERM_SIGNAL); | ||
log("Server stopped."); | ||
} | ||
} |
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 @@ | ||
/* Code for saving process PID into local file system.*/ | ||
import debug from "debug"; | ||
import * as fs from "fs"; | ||
import * as mkdirp from "mkdirp"; | ||
import * as os from "os"; | ||
import * as path from "path"; | ||
|
||
const debugLog = debug("unmock-server:pid"); | ||
|
||
export const DEFAULT_CONFIG_DIRECTORY = path.resolve(os.homedir(), ".unmock"); | ||
|
||
// Signal used for terminating process | ||
export const TERM_SIGNAL = "SIGTERM"; | ||
|
||
export const PID_FILENAME = "server.pid"; | ||
|
||
const ensureDirExists = (directory: string) => { | ||
if (!fs.existsSync(directory)) { | ||
debugLog(`Creating directory: ${directory}`); | ||
return mkdirp.sync(directory); | ||
} | ||
|
||
if (!fs.lstatSync(directory).isDirectory()) { | ||
throw Error(`Destination exists but is not directory: ${directory}`); | ||
} | ||
|
||
debugLog(`Directory exists: ${directory}`); | ||
|
||
return; | ||
}; | ||
|
||
export const writePid = (directory = DEFAULT_CONFIG_DIRECTORY) => { | ||
const pid = process.pid; | ||
|
||
ensureDirExists(directory); | ||
|
||
const pidFile = path.join(directory, PID_FILENAME); | ||
debugLog("Writing pid (%d) to %s", pid, pidFile); | ||
fs.writeFileSync(pidFile, pid); | ||
}; | ||
|
||
export const readPidIfExists = ( | ||
directory = DEFAULT_CONFIG_DIRECTORY, | ||
): number | null => { | ||
const pidFile = path.join(directory, PID_FILENAME); | ||
if (!fs.existsSync(pidFile)) { | ||
debugLog("File not found: %s", pidFile); | ||
return null; | ||
} | ||
|
||
const contents = fs.readFileSync(pidFile).toString(); | ||
|
||
return parseInt(contents, 10); | ||
}; | ||
|
||
export const deletePidFile = (directory = DEFAULT_CONFIG_DIRECTORY): void => { | ||
const pidFile = path.join(directory, PID_FILENAME); | ||
if (!fs.existsSync(pidFile)) { | ||
debugLog("File not found: %s", pidFile); | ||
return; | ||
} | ||
debugLog("Deleting file %s.", pidFile); | ||
fs.unlinkSync(pidFile); | ||
}; |
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