Skip to content
Snippets Groups Projects
Commit 03577e5d authored by Robin Leber's avatar Robin Leber
Browse files

pause and restart

parent 858e6ac4
No related branches found
No related tags found
No related merge requests found
[ [
{ {
"type": "Sender", "type": "Sender",
"id": "7470e3ef-d810-46e9-8d17-5f5153eccd81", "id": "95d31679-bbf6-43b5-82d9-665ae85d6eb2",
"code": "// publisher.js\nconst amqp = require('amqplib/callback_api');\n\nconst rabbitmqUrl = 'amqp://mquser:mqpass@rabbit:5672';\n\namqp.connect(rabbitmqUrl, (error0, connection) => {\n if (error0) {\n throw error0;\n }\n connection.createChannel((error1, channel) => {\n if (error1) {\n throw error1;\n }\n\n const queue = 'hello';\n\n channel.assertQueue(queue, {\n durable: false\n });\n\n const sendMessage = () => {\n const msg = 'Hello World! ' + new Date().toISOString();\n channel.sendToQueue(queue, Buffer.from(msg));\n console.log(\" [x] Sent '%s'\", msg);\n };\n\n setInterval(sendMessage, 1000);\n });\n});\n" "code": "// publisher.js\nconst amqp = require('amqplib/callback_api');\n\nconst rabbitmqUrl = 'amqp://mquser:mqpass@rabbit:5672';\n\namqp.connect(rabbitmqUrl, (error0, connection) => {\n if (error0) {\n throw error0;\n }\n connection.createChannel((error1, channel) => {\n if (error1) {\n throw error1;\n }\n\n const queue = 'hello';\n\n channel.assertQueue(queue, {\n durable: false\n });\n\n const sendMessage = () => {\n const msg = 'Hello World! ' + new Date().toISOString();\n channel.sendToQueue(queue, Buffer.from(msg));\n console.log(\" [x] Sent '%s'\", msg);\n };\n\n setInterval(sendMessage, 1000);\n });\n});\n"
}, },
{ {
"type": "Receiver", "type": "Receiver",
"id": "4e6acb5f-49f6-40ea-a7d0-2e84bb601897", "id": "bb3a3016-f851-4487-8255-db4a664417f6",
"code": "// consumer.js\nconst amqp = require('amqplib/callback_api');\n\nconst rabbitmqUrl = 'amqp://mquser:mqpass@rabbit:5672';\n\namqp.connect(rabbitmqUrl, (error0, connection) => {\n if (error0) {\n throw error0;\n }\n connection.createChannel((error1, channel) => {\n if (error1) {\n throw error1;\n }\n\n const queue = 'hello';\n\n channel.assertQueue(queue, {\n durable: false\n });\n\n console.log(\" [*] Waiting for messages in %s. To exit press CTRL+C\", queue);\n\n channel.consume(queue, (msg) => {\n console.log(\" [x] Received '%s'\", msg.content.toString());\n }, {\n noAck: true\n });\n });\n});\n" "code": "// consumer.js\nconst amqp = require('amqplib/callback_api');\n\nconst rabbitmqUrl = 'amqp://mquser:mqpass@rabbit:5672';\n\namqp.connect(rabbitmqUrl, (error0, connection) => {\n if (error0) {\n throw error0;\n }\n connection.createChannel((error1, channel) => {\n if (error1) {\n throw error1;\n }\n\n const queue = 'hello';\n\n channel.assertQueue(queue, {\n durable: false\n });\n\n console.log(\" [*] Waiting for messages in %s. To exit press CTRL+C\", queue);\n\n channel.consume(queue, (msg) => {\n console.log(\" [x] Received '%s'\", msg.content.toString());\n }, {\n noAck: true\n });\n });\n});\n"
} }
] ]
\ No newline at end of file
const express = require("express"); const express = require("express");
const bodyParser = require("body-parser"); const bodyParser = require("body-parser");
const cors = require("cors"); const cors = require("cors");
const { const { emptyCustomCodeDatabase } = require("./src/functions/helperFunctions");
emptyCustomCodeDatabase, const { startRabbitmq } = require("./src/docker/dockerManager");
clearDocker,
} = require("./src/functions/helperFunctions");
const customCodeRouter = require("./src/routes/customCodeRouter"); const customCodeRouter = require("./src/routes/customCodeRouter");
const deployRouter = require("./src/routes/deployRouter"); const deployRouter = require("./src/routes/deployRouter");
...@@ -22,6 +20,7 @@ server.get("/", (req, res) => { ...@@ -22,6 +20,7 @@ server.get("/", (req, res) => {
}); });
emptyCustomCodeDatabase(); emptyCustomCodeDatabase();
startRabbitmq();
// clearDocker(); // clearDocker();
server.listen(PORT, () => { server.listen(PORT, () => {
......
...@@ -181,6 +181,40 @@ function getContainerImageInfosByName(name) { ...@@ -181,6 +181,40 @@ function getContainerImageInfosByName(name) {
return imageInfosArray; return imageInfosArray;
} }
function pauseDockerContainer(containerId) {
console.log(`::Start pausing Docker container ${containerId}`);
let pauseProcess = spawnSync(`docker pause ${containerId}`, [], {
shell: true,
encoding: "utf-8",
stdio: ["inherit", "inherit", "pipe"],
});
if (pauseProcess.stderr && pauseProcess.stderr.length > 0) {
console.log(`::Failed to pause Docker container ${containerId}`);
throw new Error(pauseProcess.stderr);
}
console.log(`::Docker container ${containerId} has been paused`);
}
function unpauseDockerContainer(containerId) {
console.log(`::Start unpausing Docker container ${containerId}`);
let unpauseProcess = spawnSync(`docker unpause ${containerId}`, [], {
shell: true,
encoding: "utf-8",
stdio: ["inherit", "inherit", "pipe"],
});
if (unpauseProcess.stderr && unpauseProcess.stderr.length > 0) {
console.log(`::Failed to unpause Docker container ${containerId}`);
throw new Error(unpauseProcess.stderr);
}
console.log(`::Docker container ${containerId} has been unpaused`);
}
function buildAndRunDocker( function buildAndRunDocker(
path, path,
dockerImageName, dockerImageName,
...@@ -204,4 +238,6 @@ module.exports = { ...@@ -204,4 +238,6 @@ module.exports = {
killDockerContainer, killDockerContainer,
removeDockerContainer, removeDockerContainer,
removeDockerImage, removeDockerImage,
pauseDockerContainer,
unpauseDockerContainer,
}; };
const fs = require("fs").promises; const fs = require("fs").promises;
const path = require("path"); const path = require("path");
const { const {
startRabbitmq,
buildDockerImage, buildDockerImage,
runDockerContainer, runDockerContainer,
pauseDockerContainer,
unpauseDockerContainer,
} = require("../docker/dockerManager"); } = require("../docker/dockerManager");
const fillDockerJS = async (code) => { const fillDockerJS = async (code) => {
...@@ -92,8 +93,6 @@ let HOSTPORT = 9090; ...@@ -92,8 +93,6 @@ let HOSTPORT = 9090;
let CONTAINERPORT = 9090; let CONTAINERPORT = 9090;
const deployArchitecture = async () => { const deployArchitecture = async () => {
await startRabbitmq();
await sleep(5000);
const customCodes = await getCustomCodes(); const customCodes = await getCustomCodes();
for (const customCode of customCodes) { for (const customCode of customCodes) {
await fillDockerJS(customCode.code); await fillDockerJS(customCode.code);
...@@ -113,6 +112,20 @@ const deployArchitecture = async () => { ...@@ -113,6 +112,20 @@ const deployArchitecture = async () => {
} }
}; };
const stopArchitecture = async () => {
const customCodes = await getCustomCodes();
for (const customCode of customCodes) {
await pauseDockerContainer(customCode.id);
}
};
const restartArchitecture = async () => {
const customCodes = await getCustomCodes();
for (const customCode of customCodes) {
await unpauseDockerContainer(customCode.id);
}
};
const clearDocker = () => { const clearDocker = () => {
// Docker Container // Docker Container
// Docker Files // Docker Files
...@@ -127,4 +140,6 @@ module.exports = { ...@@ -127,4 +140,6 @@ module.exports = {
getCustomCodes, getCustomCodes,
deployArchitecture, deployArchitecture,
clearDocker, clearDocker,
stopArchitecture,
restartArchitecture,
}; };
const express = require("express"); const express = require("express");
const { deployArchitecture } = require("../functions/helperFunctions"); const {
deployArchitecture,
stopArchitecture,
restartArchitecture,
} = require("../functions/helperFunctions");
const deployRouter = express.Router(); const deployRouter = express.Router();
deployRouter.post("/", async (req, res) => { deployRouter.post("/run", async (req, res) => {
try { try {
deployArchitecture(); deployArchitecture();
res.status(200).json({ message: "Deployment erfolgreich!" }); res.status(200).json({ message: "Deployment erfolgreich!" });
...@@ -13,4 +17,24 @@ deployRouter.post("/", async (req, res) => { ...@@ -13,4 +17,24 @@ deployRouter.post("/", async (req, res) => {
} }
}); });
deployRouter.post("/stop", async (req, res) => {
try {
stopArchitecture();
res.status(200).json({ message: "Stop erfolgreich!" });
} catch (e) {
console.error(e);
res.status(500).json({ message: "Stop fehlgeschlagen!" });
}
});
deployRouter.post("/restart", async (req, res) => {
try {
restartArchitecture();
res.status(200).json({ message: "Stop erfolgreich!" });
} catch (e) {
console.error(e);
res.status(500).json({ message: "Stop fehlgeschlagen!" });
}
});
module.exports = deployRouter; module.exports = deployRouter;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment