Skip to content

Commit

Permalink
Restart do PM2 no frontend
Browse files Browse the repository at this point in the history
No .env do backend deve ser passado o ID do Frontend e Backend no pm2.
  • Loading branch information
rtenorioh committed Aug 30, 2023
1 parent 163d08c commit 448bc9f
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 20 deletions.
35 changes: 19 additions & 16 deletions backend/.env.example
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
NODE_ENV=
BACKEND_URL=http://localhost
FRONTEND_URL=http://localhost:3000
PORT=8080
PROXY_PORT=8080
CHROME_BIN=C:\Program Files\Google\Chrome\Application\chrome.exe
NODE_ENV=
BACKEND_URL=http://localhost
FRONTEND_URL=http://localhost:3000
PORT=8080
PROXY_PORT=8080
CHROME_BIN=C:\Program Files\Google\Chrome\Application\chrome.exe

DB_DIALECT=mysql
DB_HOST=localhost
DB_TIMEZONE=-03:00
DB_USER=root
DB_PASS=
DB_NAME=pressticket
DB_DIALECT=mysql
DB_HOST=localhost
DB_TIMEZONE=-03:00
DB_USER=root
DB_PASS=
DB_NAME=pressticket

USER_LIMIT=3
CONNECTIONS_LIMIT=1
USER_LIMIT=3
CONNECTIONS_LIMIT=1

JWT_SECRET=5g1yk7pD9q3YL0iBEuUlPwOiWLj3I5tK+/rhHm+jgdE=
JWT_REFRESH_SECRET=F2c8gag5nvqQkBOmOu5dWkK+gqZnjPUzHmx7S2tWkvs=
PM2_FRONTEND=0
PM2_BACKEND=1

JWT_SECRET=5g1yk7pD9q3YL0iBEuUlPwOiWLj3I5tK+/rhHm+jgdE=
JWT_REFRESH_SECRET=F2c8gag5nvqQkBOmOu5dWkK+gqZnjPUzHmx7S2tWkvs=
20 changes: 20 additions & 0 deletions backend/src/controllers/SystemController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Request, Response } from "express";

export const restartPm2 = async (
req: Request,
res: Response
): Promise<Response> => {
if (process.env.PM2_FRONTEND && process.env.PM2_BACKEND) {
setTimeout(function () {
const { execSync } = require("child_process");
execSync(`pm2 restart ${process.env.PM2_FRONTEND}`);
execSync(`pm2 restart ${process.env.PM2_BACKEND}`);
}, 100);
return res.status(200).json({ status: "Reiniciando o Sistema" });
}
return res
.status(200)
.json(
"Erro falta Adicionar ao Arquivo .env do backend os parâmetros PM2_FRONTEND PM2_BACKEND"
);
};
4 changes: 4 additions & 0 deletions backend/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import quickAnswerRoutes from "./quickAnswerRoutes";
import apiRoutes from "./apiRoutes";
import tagRoutes from "./tagRoutes";
import integrationRoutes from "./integrationRoutes";
// import companyRoutes from "./companyRoutes";
import systemRoutes from "./systemRoutes";

const routes = Router();

Expand All @@ -29,5 +31,7 @@ routes.use(quickAnswerRoutes);
routes.use("/api/messages", apiRoutes);
routes.use(tagRoutes);
routes.use(integrationRoutes);
// routes.use(companyRoutes);
routes.use(systemRoutes);

export default routes;
10 changes: 10 additions & 0 deletions backend/src/routes/systemRoutes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Router } from "express";
import isAuth from "../middleware/isAuth";

import * as sytemController from "../controllers/SystemController";

const systemRoutes = Router();

systemRoutes.post("/restartpm2", isAuth, sytemController.restartPm2);

export default systemRoutes;
8 changes: 7 additions & 1 deletion docs/INSTALL_VPS.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,20 @@ FRONTEND_URL=https://ticket.pressticket.com.br
PORT=8080
PROXY_PORT=443
CHROME_BIN=/usr/bin/google-chrome-stable

DB_DIALECT=mysql
DB_HOST=localhost
DB_TIMEZONE=-03:00
DB_USER=root
DB_PASS=
DB_NAME=pressticket
DB_NAME=pressticket

USER_LIMIT=3
CONNECTIONS_LIMIT=1

PM2_FRONTEND=0
PM2_BACKEND=1

JWT_SECRET=
JWT_REFRESH_SECRET=
```
Expand Down
24 changes: 21 additions & 3 deletions frontend/src/pages/Connections/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
SignalCellular4Bar,
CropFree,
DeleteOutline,
SyncOutlined,
WhatsApp
} from "@material-ui/icons";

Expand All @@ -33,11 +34,11 @@ import MainHeader from "../../components/MainHeader";
import MainHeaderButtonsWrapper from "../../components/MainHeaderButtonsWrapper";
import Title from "../../components/Title";
import TableRowSkeleton from "../../components/TableRowSkeleton";

import api from "../../services/api";
import WhatsAppModal from "../../components/WhatsAppModal";
import ConfirmationModal from "../../components/ConfirmationModal";
import QrcodeModal from "../../components/QrcodeModal";

import api from "../../services/api";
import { i18n } from "../../translate/i18n";
import { WhatsAppsContext } from "../../context/WhatsApp/WhatsAppsContext";
import toastError from "../../errors/toastError";
Expand Down Expand Up @@ -291,6 +292,14 @@ const Connections = () => {
);
};

const restartpm2 = async () => {
try {
await api.post('/restartpm2');
} catch (err) {
toastError(err);
}
}

return (
<MainContainer>
<ConfirmationModal
Expand All @@ -314,6 +323,15 @@ const Connections = () => {
<MainHeader>
<Title>{i18n.t("connections.title")} ({whatsApps.length})</Title>
<MainHeaderButtonsWrapper>
<Tooltip title={i18n.t("connections.buttons.restart")}>
<Button
variant="contained"
color="primary"
onClick={restartpm2}
>
<SyncOutlined />
</Button>
</Tooltip>
<Tooltip title={i18n.t("connections.buttons.add")}>
<Button
variant="contained"
Expand All @@ -329,7 +347,7 @@ const Connections = () => {
<Table size="small">
<TableHead>
<TableRow>
<TableCell align="center">
<TableCell align="center">
{i18n.t("connections.table.id")}
</TableCell>
<TableCell align="center">
Expand Down
1 change: 1 addition & 0 deletions frontend/src/translate/languages/pt.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const messages = {
},
buttons: {
add: "Adicionar WhatsApp",
restart: "Restart",
disconnect: "desconectar",
tryAgain: "Tentar novamente",
qrcode: "QR CODE",
Expand Down

0 comments on commit 448bc9f

Please sign in to comment.