Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redis/scheduler #82

Merged
merged 6 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/CICDOnRedis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: CI/CD on Redis
run-name: ${{ github.actor }} is testing out GitHub Actions 🚀
on:
push:
branches:
- main
jobs:
deploy-client:
runs-on: ubuntu-latest
steps:
- name: deploy redis scheduler
uses: appleboy/[email protected]
with:
host: ${{ secrets.REDIS }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
port: ${{ secrets.REDIS_PORT }}
script: |
export NVM_DIR=~/.nvm
source ~/.nvm/nvm.sh
npm --help
cd /home/web28-Boostform
git pull
cd /root/boostForm/redis
npm i
pm2 reload all
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions redis/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.env
6 changes: 6 additions & 0 deletions redis/Form/Form.Model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import mongoose from "mongoose";
import { FormSchema } from "./Form.Schema.js";

const Form = mongoose.model("Form", FormSchema);

export default Form;
78 changes: 78 additions & 0 deletions redis/Form/Form.Schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import mongoose from "mongoose";

const QuestionSchema = new mongoose.Schema({
question_id: {
type: Number,
required: true,
},
page: {
type: Number,
},
type: {
type: String,
required: true,
enum: ["checkbox", "multiple", "paragraph"],
},
title: {
type: String,
default: "제목 없음",
},
option: {
type: [String],
},
essential: {
type: Boolean,
default: false,
},
etc_added: {
type: Boolean,
default: false,
},
});

const FormSchema = new mongoose.Schema(
{
user_id: {
type: Number,
required: true,
},
title: {
type: String,
default: "제목 없음",
},
description: {
type: String,
},
category: {
type: String,
},
question_list: {
type: [QuestionSchema],
},
accept_response: {
type: Boolean,
default: false,
},
on_board: {
type: Boolean,
default: false,
},
login_required: {
type: Boolean,
default: false,
},
response_modifiable: {
type: Boolean,
default: true,
},
response_count: {
type: Number,
default: 0,
},
},
{
timestamps: true,
}
);

export { QuestionSchema, FormSchema };
6 changes: 6 additions & 0 deletions redis/Response/Response.Model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import mongoose from "mongoose";
import { ResponseSchema } from "./Response.Schema.js";

const FormResponse = mongoose.model("Response", ResponseSchema);

export default FormResponse;
25 changes: 25 additions & 0 deletions redis/Response/Response.Schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import mongoose from "mongoose";

const AnswerSchema = new mongoose.Schema({
question_id: {
type: Number,
},
answer: {
type: [String],
},
});

const ResponseSchema = new mongoose.Schema({
user_id: {
type: Number,
},
form_id: {
type: String,
index: true,
},
answer_list: {
type: [AnswerSchema],
},
});

export { AnswerSchema, ResponseSchema };
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* eslint-disable no-async-promise-executor */
import schedule from "node-schedule";
import Scheduler from "./Scheduler";
import { redisCli } from "../../app";
import Form from "../../Form/Form.Model";
import Scheduler from "./Scheduler.js";
import { redisCli } from "../connect.js";
import Form from "../Form/Form.Model.js";

class CountIncreaseScheduler extends Scheduler {
static isWorking = false;
Expand All @@ -20,7 +19,10 @@ class CountIncreaseScheduler extends Scheduler {
return new Promise((res, rej) => {
const count = Number(countList[formId]);

Form.findOneAndUpdate({ _id: formId }, { $inc: { response_count: count } })
Form.findOneAndUpdate(
{ _id: formId },
{ $inc: { response_count: count } }
)
.exec()
.then(redisCli.hIncrBy("count", formId, -count))
.then(async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/* eslint-disable no-async-promise-executor */
import schedule from "node-schedule";
import * as fs from "fs";
import Scheduler from "./Scheduler";
import { redisCli } from "../../app";
import FormResponse from "../Response.Model";
import Scheduler from "./Scheduler.js";
import { redisCli } from "../connect.js";
import FormResponse from "../Response/Response.Model.js";

class ResponseSaveScheduler extends Scheduler {
static isWorking = false;
Expand All @@ -22,9 +20,10 @@ class ResponseSaveScheduler extends Scheduler {
const responseObj = JSON.parse(responseSaveList[responseId]);
const response = new FormResponse(responseObj);

response.save().then(redisCli.hDel("response", responseId)).then(res);
}).catch((err) => {
fs.writeFileSync(`./log/${new Date().getTime()}`, String(err));
response
.save()
.then(redisCli.hDel("response", responseId))
.then(res);
});
})
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* eslint-disable no-async-promise-executor */
import schedule from "node-schedule";
import Scheduler from "./Scheduler";
import { redisCli } from "../../app";
import FormResponse from "../Response.Model";
import Scheduler from "./Scheduler.js";
import { redisCli } from "../connect.js";
import FormResponse from "../Response/Response.Model.js";

class ResponseUpdateScheduler extends Scheduler {
static isWorking = false;
Expand All @@ -22,17 +21,32 @@ class ResponseUpdateScheduler extends Scheduler {

FormResponse.exists({ _id: responseId }).then(async (result) => {
if (result) {
// 응답지가 DB에 존재하는 경우
// findOneAndUpdate 진행
const answerList = JSON.parse(answerListString);
await FormResponse.findOneAndUpdate(
{ _id: responseId },
{ answer_list: answerList.answer_list }
).exec();

if (answerListString === (await redisCli.hGet("response_update", responseId))) {
if (
answerListString ===
(await redisCli.hGet("response_update", responseId))
) {
// 해당 responseId로 새로운 수정 사항이 생겼는지 확인
// 만약 새로운 수정 사항이 없다면 redis에서 삭제
// 만약 새로운 수정 사항이 있다면 다음 작업에서 처리하도록 남겨둠
await redisCli.hDel("response_update", responseId);
}
} else {
await redisCli.hSet("response_update", responseId, answerListString);
// 응답지가 아직 DB에 존재하지 않는 경우
// findOneAndUpdate 메소드를 적용할 수 없으므로
// 이번 작업에서는 처리하지 않고, 다음 작업으로 미룸
await redisCli.hSet(
"response_update",
responseId,
answerListString
);
}
res(true);
});
Expand Down
File renamed without changes.
38 changes: 38 additions & 0 deletions redis/connect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as dotenv from "dotenv";
import mongoose from "mongoose";
import * as redis from "redis";

dotenv.config();

// MongoDB 연결
function connectDB() {
mongoose.connect(
`mongodb+srv://${process.env.MONGODB_ID}:${process.env.MONGODB_PASSWORD}@cluster0.a7vmgdw.mongodb.net/database0?`,
(err) => {
if (err) {
console.log(err);
} else {
console.log("mongoDB is connected...");
}
}
);
}

connectDB();

// redis 연결
const redisClient = redis.createClient({
url: `redis://@${process.env.REDIS_HOST}:${process.env.REDIS_PORT}/0`,
legacyMode: true,
});

redisClient.on("connect", () => {
console.info("Redis connected!");
});
redisClient.on("error", (err) => {
console.error("Redis Client Error", err);
});

redisClient.connect();

export const redisCli = redisClient.v4;
11 changes: 11 additions & 0 deletions redis/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import ResponseSaveScheduler from "./Scheduler/ResponseSaveScheduler.js";
import CountIncreaseScheduler from "./Scheduler/CountIncreaseScheduler.js";
import ResponseUpdateScheduler from "./Scheduler/ResponseUpdateScheduler.js";

try {
ResponseSaveScheduler.init();
CountIncreaseScheduler.init();
ResponseUpdateScheduler.init();
} catch (err) {
console.log(err);
}
Loading