Implement kill instance

Add ability to kill running instances in case it is desirable to stop
the server without saving over the current save file.
This commit is contained in:
Hornwitser committed 2022-03-25 20:52:31 +01:00
1 parent a93f048814
commit 1e4d709a06
8 files changed
+160 -64

No files matched your search

+9
View File
@@ -42,6 +42,15 @@ List instances.
Create a new instance. `name` is the instance name. `--id` is the optional id. Create a new instance. `name` is the instance name. `--id` is the optional id.
### Kill instance
ctl> instance kill <name>
Terminates the running Factorio server without giving it a chance to save or cleanup after itself.
Note: This may cause loss of data.
### Config management ### Config management
#### List Configuration #### List Configuration
+13
View File
@@ -755,6 +755,19 @@ instanceCommands.add(new libCommand.Command({
}, },
})); }));
instanceCommands.add(new libCommand.Command({
definition: ["kill <instance>", "Kill instance", (yargs) => {
yargs.positional("instance", { describe: "Instance to kill", type: "string" });
}],
handler: async function(args, control) {
let instanceId = await libCommand.resolveInstance(control, args.instance);
await control.setLogSubscriptions({ instance_ids: [instanceId] });
await libLink.messages.killInstance.send(control, {
instance_id: instanceId,
});
},
}));
instanceCommands.add(new libCommand.Command({ instanceCommands.add(new libCommand.Command({
definition: ["delete <instance>", "Delete instance", (yargs) => { definition: ["delete <instance>", "Delete instance", (yargs) => {
yargs.positional("instance", { describe: "Instance to delete", type: "string" }); yargs.positional("instance", { describe: "Instance to delete", type: "string" });
+17 -8
View File
@@ -487,6 +487,7 @@ class FactorioServer extends events.EventEmitter {
// Array of possible causes for an unexpected shutdown // Array of possible causes for an unexpected shutdown
this._unexpected = []; this._unexpected = [];
this._killed = false;
// Track autosaving // Track autosaving
this._runningAutosave = null; this._runningAutosave = null;
@@ -694,13 +695,16 @@ class FactorioServer extends events.EventEmitter {
_watchExit() { _watchExit() {
this._server.on("exit", (code, signal) => { this._server.on("exit", (code, signal) => {
if (this._state !== "stopping") { if (this._state !== "stopping") {
if (this._unexpected.length === 0) {
if (signal === "SIGKILL") { if (signal === "SIGKILL") {
if (this._killed) {
this.emit("error", new libErrors.EnvironmentError("Factorio server was killed"));
} else {
this.emit("error", new libErrors.EnvironmentError( this.emit("error", new libErrors.EnvironmentError(
"Factorio server was unexpectedly killed, is the system low on memory?" "Factorio server was unexpectedly killed, is the system low on memory?"
)); ));
}
} else { } else if (this._unexpected.length === 0) {
let msg; let msg;
if (code !== null) { if (code !== null) {
msg = `Factorio server unexpectedly shut down with code ${code}`; msg = `Factorio server unexpectedly shut down with code ${code}`;
@@ -708,14 +712,13 @@ class FactorioServer extends events.EventEmitter {
msg = `Factorio server was unexpectedly shut by down by signal ${signal}`; msg = `Factorio server was unexpectedly shut by down by signal ${signal}`;
} }
this.emit("error", new Error(msg)); this.emit("error", new libErrors.EnvironmentError(msg));
}
} else if (this._unexpected.length === 1) { } else if (this._unexpected.length === 1) {
this.emit("error", new Error(this._unexpected[0])); this.emit("error", new libErrors.EnvironmentError(this._unexpected[0]));
} else { } else {
this.emit("error", new Error( this.emit("error", new libErrors.EnvironmentError(
"Factorio server unexpectedly shut down. Possible causes:"+ "Factorio server unexpectedly shut down. Possible causes:"+
`\n- ${this._unexpected.join("\n- ")}` `\n- ${this._unexpected.join("\n- ")}`
)); ));
@@ -1004,12 +1007,18 @@ class FactorioServer extends events.EventEmitter {
* Kill the server * Kill the server
* *
* Terminates the server without any cleanup or saving. * Terminates the server without any cleanup or saving.
* @param {boolean} [unexpected=false] -
* If true raise an error event as a result of killing the Factorio
* process.
*/ */
async kill() { async kill(unexpected = false) {
this._check(["running", "stopping"]); this._check(["running", "stopping"]);
this._killed = true;
if (!unexpected) {
this._state = "stopping"; this._state = "stopping";
}
this._server.kill("SIGKILL"); this._server.kill("SIGKILL");
await events.once(this._server, "exit"); await new Promise(resolve => this._server.once("exit", resolve));
} }
/** /**
+7
View File
@@ -636,6 +636,13 @@ messages.stopInstance = new Request({
forwardTo: "instance", forwardTo: "instance",
}); });
messages.killInstance = new Request({
type: "kill_instance",
links: ["control-master", "master-slave", "slave-instance"],
permission: "core.instance.kill",
forwardTo: "instance",
});
messages.deleteInstance = new Request({ messages.deleteInstance = new Request({
type: "delete_instance", type: "delete_instance",
links: ["control-master", "master-slave"], links: ["control-master", "master-slave"],
+5
View File
@@ -427,6 +427,11 @@ definePermission({
title: "Stop instance", title: "Stop instance",
description: "Stop instances.", description: "Stop instances.",
}); });
definePermission({
name: "core.instance.kill",
title: "Kill instance",
description: "Terminate running instances without saving or cleanup.",
});
definePermission({ definePermission({
name: "core.instance.delete", name: "core.instance.delete",
title: "Delete instance", title: "Delete instance",
+16 -1
View File
@@ -186,7 +186,11 @@ class Instance extends libLink.Link {
}); });
this.server.on("error", err => { this.server.on("error", err => {
this.logger.error(`Error in instance ${this.name}:\n${err.stack}`); if (err instanceof libErrors.EnvironmentError) {
this.logger.error(err.message);
} else {
this.logger.error(`${this.name}:\n${err.stack}`);
}
}); });
this.server.on("autosave-finished", name => { this.server.on("autosave-finished", name => {
@@ -697,6 +701,13 @@ class Instance extends libLink.Link {
} }
} }
async kill() {
if (this._status === "stopped") {
return;
}
await this.server.kill(true);
}
async masterConnectionEventEventHandler(message) { async masterConnectionEventEventHandler(message) {
await libPlugin.invokeHook(this.plugins, "onMasterConnectionEvent", message.data.event); await libPlugin.invokeHook(this.plugins, "onMasterConnectionEvent", message.data.event);
} }
@@ -833,6 +844,10 @@ class Instance extends libLink.Link {
await this.stop(); await this.stop();
} }
async killInstanceRequestHandler() {
await this.kill();
}
async sendRconRequestHandler(message) { async sendRconRequestHandler(message) {
let result = await this.sendRcon(message.data.command); let result = await this.sendRcon(message.data.command);
return { result }; return { result };
@@ -1,7 +1,8 @@
import React, { useContext, useState } from "react"; import React, { useContext, useState } from "react";
import { useHistory, useParams } from "react-router-dom"; import { useHistory, useParams } from "react-router-dom";
import { Alert, Button, Descriptions, Popconfirm, Space, Spin, Typography } from "antd"; import { Alert, Button, Descriptions, Dropdown, Menu, Modal, PageHeader, Space, Spin, Typography } from "antd";
import DeleteOutlined from "@ant-design/icons/DeleteOutlined"; import DeleteOutlined from "@ant-design/icons/DeleteOutlined";
import DownOutlined from "@ant-design/icons/DownOutlined";
import { libLink } from "@clusterio/lib"; import { libLink } from "@clusterio/lib";
@@ -24,6 +25,36 @@ import InstanceStatusTag from "./InstanceStatusTag";
const { Title } = Typography; const { Title } = Typography;
function InstanceDescription(props) {
let account = useAccount();
const { slave, instance } = props;
let assigned = instance["assigned_slave"] !== null;
return <Descriptions
bordered
size="small"
>
<Descriptions.Item label="Slave">
{!assigned
? <em>Unassigned</em>
: slave["name"] || instance["assigned_slave"]
}
{account.hasPermission("core.instance.assign") && <AssignInstanceModal
id={instance["id"]}
slaveId={instance["assigned_slave"]}
buttonProps={{
size: "small",
style: { float: "Right" },
type: assigned ? "default" : "primary",
disabled: !["unknown", "unassigned", "stopped"].includes(instance["status"]),
}}
buttonContent={assigned ? "Reassign" : "Assign"}
/>}
</Descriptions.Item>
<Descriptions.Item label="Status"><InstanceStatusTag status={instance["status"]} /></Descriptions.Item>
</Descriptions>;
}
export default function InstanceViewPage(props) { export default function InstanceViewPage(props) {
let params = useParams(); let params = useParams();
let instanceId = Number(params.id); let instanceId = Number(params.id);
@@ -61,16 +92,9 @@ export default function InstanceViewPage(props) {
</PageLayout>; </PageLayout>;
} }
let instanceButtons = <Space> let instanceButtonsMenu = <Menu
{ onClick={({ key }) => {
account.hasAnyPermission("core.instance.start", "core.instance.stop") if (key === "export") {
&& <StartStopInstanceButton instance={instance} />
}
{account.hasPermission("core.instance.load_scenario") && <LoadScenarioModal instance={instance} />}
{account.hasPermission("core.instance.export_data") && <Button
loading={exportingData}
disabled={instance.status !== "stopped"}
onClick={() => {
setExportingData(true); setExportingData(true);
libLink.messages.exportData.send( libLink.messages.exportData.send(
control, { instance_id: instanceId } control, { instance_id: instanceId }
@@ -79,59 +103,69 @@ export default function InstanceViewPage(props) {
).finally(() => { ).finally(() => {
setExportingData(false); setExportingData(false);
}); });
}}
> } else if (key === "kill") {
Export data libLink.messages.killInstance.send(
</Button>} control, { instance_id: instanceId }
{account.hasPermission("core.instance.delete") && <Popconfirm ).catch(notifyErrorHandler("Error killing instance"));
title="Permanently delete instance and server saves?"
okText="Delete" } else if (key === "delete") {
placement="bottomRight" Modal.confirm({
okButtonProps={{ danger: true }} autoFocusButton: "cancel",
onConfirm={() => { content: "Permamently delete instance and server saves?",
okText: "Delete",
okButtonProps: { danger: true },
onOk: () => {
libLink.messages.deleteInstance.send( libLink.messages.deleteInstance.send(
control, { instance_id: instanceId } control, { instance_id: instanceId }
).then(() => { ).then(() => {
history.push("/instances"); history.push("/instances");
}).catch(notifyErrorHandler("Error deleting instance")); }).catch(notifyErrorHandler("Error deleting instance"));
},
});
}
}} }}
> >
<Button {account.hasPermission("core.instance.export_data") && <Menu.Item
danger disabled={exportingData || instance.status !== "stopped"}
key="export"
>Export data</Menu.Item>}
{account.hasPermission("core.instance.kill") && <Menu.Item
disabled={["unknown", "unassigned", "stopped"].includes(instance["status"])}
key="kill"
>Kill process</Menu.Item>}
{account.hasPermission("core.instance.delete") && <>
<Menu.Divider />
<Menu.Item
disabled={!["unknown", "unassigned", "stopped"].includes(instance["status"])} disabled={!["unknown", "unassigned", "stopped"].includes(instance["status"])}
> danger
<DeleteOutlined /> key="delete"
</Button> icon={<DeleteOutlined />}
</Popconfirm>} >Delete</Menu.Item>
</>}
</Menu>;
let instanceButtons = <Space>
{
account.hasAnyPermission("core.instance.start", "core.instance.stop")
&& <StartStopInstanceButton instance={instance} />
}
{account.hasPermission("core.instance.load_scenario") && <LoadScenarioModal instance={instance} />}
{account.hasAnyPermission(
"core.instance.export_data",
"core.instance.kill",
"core.instance.delete",
) && <Dropdown placement="bottomRight" trigger={["click"]} overlay={instanceButtonsMenu}>
<Button>More <DownOutlined /></Button>
</Dropdown>}
</Space>; </Space>;
let assigned = instance["assigned_slave"] !== null;
return <PageLayout nav={nav}> return <PageLayout nav={nav}>
<Descriptions <PageHeader
bordered className="site-page-header"
size="small"
title={instance["name"]} title={instance["name"]}
extra={instanceButtons} extra={instanceButtons}
> />
<Descriptions.Item label="Slave"> <InstanceDescription slave={slave} instance={instance} />
{!assigned
? <em>Unassigned</em>
: slave["name"] || instance["assigned_slave"]
}
{account.hasPermission("core.instance.assign") && <AssignInstanceModal
id={instanceId}
slaveId={instance["assigned_slave"]}
buttonProps={{
size: "small",
style: { float: "Right" },
type: assigned ? "default" : "primary",
disabled: !["unknown", "unassigned", "stopped"].includes(instance["status"]),
}}
buttonContent={assigned ? "Reassign" : "Assign"}
/>}
</Descriptions.Item>
<Descriptions.Item label="Status"><InstanceStatusTag status={instance["status"]} /></Descriptions.Item>
</Descriptions>
{ {
account.hasAllPermission("core.instance.save.list", "core.instance.save.list_subscribe") account.hasAllPermission("core.instance.save.list", "core.instance.save.list_subscribe")
+8 -4
View File
@@ -481,7 +481,7 @@ describe("Integration of Clusterio", function() {
await execCtl("instance config set 44 research_sync.load_plugin false"); await execCtl("instance config set 44 research_sync.load_plugin false");
await execCtl("instance config set 44 statistics_exporter.load_plugin false"); await execCtl("instance config set 44 statistics_exporter.load_plugin false");
await execCtl("instance config set 44 subspace_storage.load_plugin false"); await execCtl("instance config set 44 subspace_storage.load_plugin false");
try {
let exchangeString = testStrings.modified.replace(/[\n\r]+/g, ""); let exchangeString = testStrings.modified.replace(/[\n\r]+/g, "");
let args = `base/freeplay --seed 1234 --map-exchange-string "${exchangeString}"`; let args = `base/freeplay --seed 1234 --map-exchange-string "${exchangeString}"`;
await execCtl(`instance load-scenario test ${args}`); await execCtl(`instance load-scenario test ${args}`);
@@ -493,10 +493,14 @@ describe("Integration of Clusterio", function() {
assert.equal( assert.equal(
await sendRcon(44, "/c rcon.print(game.difficulty_settings.research_queue_setting)"), "never\n" await sendRcon(44, "/c rcon.print(game.difficulty_settings.research_queue_setting)"), "never\n"
); );
});
});
} finally { describe("instance kill", function() {
await execCtl("instance stop test"); it("kills the instance", async function() {
} slowTest(this);
await execCtl("instance kill test");
await checkInstanceStatus(44, "stopped");
}); });
}); });