Skip to content

Commit

Permalink
feat: 优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
galaxy-s10 committed Jun 2, 2023
1 parent bee32d7 commit 0c4d8df
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 25 deletions.
3 changes: 1 addition & 2 deletions src/config/rabbitmq/consumer.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { orderMQChannel, orderQueue } from './index';

export const initRabbitMQConsumer = () => {
console.log('initRabbitMQConsumer', orderMQChannel);
if (orderMQChannel.channel.val) {
orderMQChannel.channel.val.consume(
orderQueue,
(msg) => {
console.log(msg, '----');
console.log(msg?.content.toString(), '----');
},
{ noAck: true }
);
Expand Down
2 changes: 1 addition & 1 deletion src/config/rabbitmq/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const connectRabbitMQ = async () => {
channel.val = await connection.createChannel();
console.log(chalkSUCCESS('RabbitMQ成功'), channel);
channel.val.assertQueue(orderQueue, { durable: false });
channel.val.sendToQueue(orderQueue, Buffer.from('aaa'));
channel.val.sendToQueue(orderQueue, Buffer.from(`aaa${new Date()}`));
// setTimeout(() => {
// console.log(chalkWARN('RabbitMQ关闭'));
// connection.close();
Expand Down
13 changes: 6 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,21 @@ import './init/alias';
import './init/initFile';

import { connectMysql, dbName } from '@/config/mysql';
import { connectRabbitMQ } from '@/config/rabbitmq';
import { connectRedis } from '@/config/redis';
import { createRedisPubSub } from '@/config/redis/pub';
import { startSchedule } from '@/config/schedule';
import { PROJECT_ENV, PROJECT_NAME, PROJECT_PORT } from '@/constant';
import { dockerRunRabbitMQ } from '@/init/docker/RabbitMQ';
import { dockerRunSRS } from '@/init/docker/SRS';
import { initDb } from '@/init/initDb';
import { initFFmpeg } from '@/init/initFFmpeg';
import { initSRS } from '@/init/initSRS';
import {
chalkERROR,
chalkINFO,
chalkSUCCESS,
chalkWARN,
} from '@/utils/chalkTip';

import { initRabbitMQConsumer } from './config/rabbitmq/consumer';

async function main() {
function adLog() {
console.log();
Expand All @@ -34,14 +32,15 @@ async function main() {
}
try {
await Promise.all([
connectRabbitMQ(), // 连接rabbitMQ
connectMysql(), // 连接mysql
connectRedis(), // 连接redis
createRedisPubSub(), // 创建redis的发布订阅
]);
initRabbitMQConsumer();
dockerRunRabbitMQ(); // docker运行RabbitMQ
// connectRabbitMQ(); // 连接rabbitMQ
// initRabbitMQConsumer();
await initDb('load');
initSRS(false); // 初始化srs
dockerRunSRS(false); // docker运行SRS
await initFFmpeg(false); // 初始化FFmpeg
startSchedule();
const port = +PROJECT_PORT;
Expand Down
49 changes: 49 additions & 0 deletions src/init/docker/RabbitMQ.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { execSync } from 'child_process';

import { RABBITMQ_CONFIG } from '@/config/secret';
import { dockerIsInstalled } from '@/utils';
import { chalkERROR, chalkSUCCESS } from '@/utils/chalkTip';

export const dockerRunRabbitMQ = (init = true) => {
if (!init) return;
const flag = dockerIsInstalled();
if (flag) {
console.log(chalkSUCCESS('docker已安装,开始运行RabbitMQ'));
} else {
console.log(chalkERROR('未安装docker!'));
return;
}
try {
// 停掉旧的容器
execSync(`docker stop ${RABBITMQ_CONFIG.dockerContainerName}`);
} catch (error) {
console.log(error);
}
try {
// 停掉旧的容器
execSync(`docker rm ${RABBITMQ_CONFIG.dockerContainerName}`);
} catch (error) {
console.log(error);
}
// 启动新的容器
try {
// 1 代表标准输出
// 2 代表标准错误
// 1>/dev/null 把标准输出导入到null设备,也就是消失不见,如果要重定向到某个文件,可以1>1.txt
// 2>&1 把标准错误也导入到标准输出同样的地方
// -d即后台运行,https://docs.docker.com/engine/reference/run/#detached--d
execSync(
// 官网命令是带-it参数的,但是使用execSync执行docker的话,不能带-it,否则报错:the input device is not a TTY
// `docker run -it -d --rm --name ${RABBITMQ_CONFIG.dockerContainerName} -p 5672:5672 -p 15672:15672 ${RABBITMQ_CONFIG.dockerImage}`
`docker run -d --rm --name ${RABBITMQ_CONFIG.dockerContainerName} -p 5672:5672 -p 15672:15672 ${RABBITMQ_CONFIG.dockerImage}`
);
console.log(
chalkSUCCESS(`${new Date().toLocaleString()},docker启动RabbitMQ成功!`)
);
} catch (error) {
console.log(
chalkERROR(`${new Date().toLocaleString()},docker启动RabbitMQ错误!`)
);
console.log(error);
}
};
23 changes: 8 additions & 15 deletions src/init/initSRS.ts → src/init/docker/SRS.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import { execSync, spawnSync } from 'child_process';
import { execSync } from 'child_process';

import { SRS_CONFIG } from '@/config/secret';
import { dockerIsInstalled } from '@/utils';
import { chalkERROR, chalkSUCCESS } from '@/utils/chalkTip';

function dockerIsInstalled() {
const res = spawnSync('docker', ['-v']);
if (res.status !== 0) {
return false;
}
return true;
}

export const initSRS = (init = true) => {
export const dockerRunSRS = (init = true) => {
if (!init) return;
const flag = dockerIsInstalled();
if (flag) {
console.log(chalkSUCCESS('docker已安装,开始运行docker-srs'));
console.log(chalkSUCCESS('docker已安装,开始运行SRS'));
} else {
console.log(chalkERROR('未安装docker!'));
return;
Expand All @@ -28,13 +21,13 @@ export const initSRS = (init = true) => {
console.log(error);
}
try {
// 删掉旧的容器
// 停掉旧的容器
execSync(`docker rm ${SRS_CONFIG.dockerContainerName}`);
} catch (error) {
console.log(error);
}
// 启动新的容器
execSync(`docker run -d --name ${SRS_CONFIG.dockerContainerName} --rm --env CANDIDATE=${SRS_CONFIG.CANDIDATE} \
execSync(`docker run -d --rm --name ${SRS_CONFIG.dockerContainerName} --env CANDIDATE=${SRS_CONFIG.CANDIDATE} \
-p 1935:1935 -p 5001:8080 -p 1985:1985 -p 8000:8000/udp \
registry.cn-hangzhou.aliyuncs.com/ossrs/srs:4 \
objs/srs -c conf/rtc2rtmp.conf`);
Expand All @@ -49,12 +42,12 @@ export const initSRS = (init = true) => {
// });
// child.on('exit', () => {
// console.log(
// chalkINFO(`${new Date().toLocaleString()},initSRS子进程退出了,${srsSh}`)
// chalkINFO(`${new Date().toLocaleString()},dockerStartSRS子进程退出了,${srsSh}`)
// );
// });
// child.on('error', () => {
// console.log(
// chalkERROR(`${new Date().toLocaleString()},initSRS子进程错误,${srsSh}`)
// chalkERROR(`${new Date().toLocaleString()},dockerStartSRS子进程错误,${srsSh}`)
// );
// });
} catch (error) {
Expand Down
9 changes: 9 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { spawnSync } from 'child_process';
import path from 'path';

import { ParameterizedContext } from 'koa';
Expand All @@ -6,6 +7,14 @@ import { Model, ModelStatic } from 'sequelize/types';
import sequelize from '@/config/mysql';
import { chalkERROR, chalkINFO, chalkSUCCESS } from '@/utils/chalkTip';

export function dockerIsInstalled() {
const res = spawnSync('docker', ['-v']);
if (res.status !== 0) {
return false;
}
return true;
}

export const filterObj = (obj: Record<string, any>, keyArr: string[]) => {
const res: Record<string, any> = {};
Object.keys(obj).forEach((item) => {
Expand Down

0 comments on commit 0c4d8df

Please sign in to comment.