Skip to content

Commit

Permalink
[ MERGE ] 버전 1.2.2 배포
Browse files Browse the repository at this point in the history
*Release/v1.2.2->master
  • Loading branch information
aereeeee committed Dec 17, 2019
2 parents 53387ce + 053a7e4 commit 7113db0
Show file tree
Hide file tree
Showing 58 changed files with 507 additions and 450 deletions.
1 change: 1 addition & 0 deletions server-converter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"test": "jest --passWithNoTests"
},
"dependencies": {
"@types/connect-timeout": "^0.0.34",
"aws-sdk": "^2.574.0",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
Expand Down
2 changes: 1 addition & 1 deletion server-converter/src/@types/converter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type OutputNaming = (page: number) => string;
export interface ConverterEngine extends EventEmitter {
init: () => Promise<void>,
convert: () => Promise<SlideInfo[]>
stop: (clearOutput?: boolean) => void
stop: (clearOutput?: boolean) => Promise<void>
clear: () => Promise<void>
clearInput: () => Promise<void>
clearOutput: () => Promise<void>
Expand Down
1 change: 1 addition & 0 deletions server-converter/src/@types/express.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Converter from '../core/Converter';
declare global {
namespace Express {
interface Request {
stage: {stage:string, next:boolean, path?:string};
topic: string;
slides: SlideInfo[];
fileUrl: string;
Expand Down
4 changes: 3 additions & 1 deletion server-converter/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as express from 'express';
import * as cors from 'cors';
import router from './router';
import { TIMEOUT } from './constants';

const app = express();

Expand All @@ -24,7 +25,8 @@ const start = () => {
app.use(express.json());
app.use(router);
app.use(handleError);
app.listen('3000', () => {
const server = app.listen('3000', () => {
server.setTimeout(TIMEOUT);
console.log('🔗 welcome dropy converter!');
});
};
Expand Down
1 change: 1 addition & 0 deletions server-converter/src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './main';
export * from './progress';
5 changes: 5 additions & 0 deletions server-converter/src/constants/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const TIMEOUT = 20 * 60 * 1000;
export const CONVERT_TIMEOUT = 10 * 60 * 1000;
export const TIMEOUT_MESSAGE = '사용량이 많습니다. 잠시 후 다시 시도해 주세요';
export const ERROR_MESSAGE = '잘못된 접근입니다. 다시 시도해 주세요';
export const CLEAR_TIME = 1000;
2 changes: 2 additions & 0 deletions server-converter/src/core/Converter/Converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Converter {
private outputPath: string;
private outputNaming: OutputNaming;
public engine: ConverterEngine;
public isStop: Boolean;

constructor(
inputPath: string,
Expand All @@ -20,6 +21,7 @@ class Converter {
this.inputPath = inputPath;
this.outputPath = outputPath;
this.outputNaming = outputNaming;
this.isStop = false;
}

async init() {
Expand Down
19 changes: 13 additions & 6 deletions server-converter/src/core/Converter/PdfConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class PdfConverter extends EventEmitter implements ConverterEngine {
private slides: SlideInfo[];
private pageLength: number;
private done: boolean;
private convertChain: Promise<SlideInfo[]>;

constructor(
inputPath: string,
Expand All @@ -31,6 +32,7 @@ class PdfConverter extends EventEmitter implements ConverterEngine {
this.slides = [];
this.pageLength = 0;
this.done = false;
this.convertChain = null;
}

async init(): Promise<void> {
Expand Down Expand Up @@ -58,7 +60,8 @@ class PdfConverter extends EventEmitter implements ConverterEngine {
this.end();
return this.slides;
};
const convertChain: Promise<SlideInfo[]> = this.sgms.reduce(

this.convertChain = this.sgms.reduce(
(chain, sgm) => chain
.then(convertStopped)
.then(() => sgm.write().then(convertDone)),
Expand All @@ -67,7 +70,7 @@ class PdfConverter extends EventEmitter implements ConverterEngine {
.then(convertEnd)
.catch(convertEnd);

const slides: SlideInfo[] = await convertChain;
const slides: SlideInfo[] = await this.convertChain;

return slides;
}
Expand All @@ -77,14 +80,18 @@ class PdfConverter extends EventEmitter implements ConverterEngine {
this.done = true;
}

async stop(clearOutput): Promise<void> {
async stop(clear): Promise<void> {
this.end();
if (clearOutput) await this.clearOutput();
await this.convertChain;
if (clear) {
await this.clear();
}
}

async clear(): Promise<void> {
await this.clearOutput();
await this.clearInput();
await this.convertChain;
this.clearInput();
this.clearOutput();
}

async clearInput(): Promise<void> {
Expand Down
1 change: 0 additions & 1 deletion server-converter/src/core/Converter/SimpleGm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class SimpleGm {
private state: gm.State;
private page: number;


constructor(inputPath: string, outputPath: string) {
this.inputPath = inputPath;
this.outputPath = outputPath;
Expand Down
125 changes: 75 additions & 50 deletions server-converter/src/core/queue.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { EventEmitter } from 'events';
import * as osu from 'node-os-utils';
import * as fs from 'fs';
import Job from './requestJob';
import { CLEAR_TIME } from '../constants';

const { cpu, mem } = osu;

Expand Down Expand Up @@ -32,101 +34,124 @@ class Queue extends EventEmitter {

async checkCPU() {
const cpuUsage = await cpu.usage();

return (cpuUsage < this.config.cpuUsage);
}

async setActiveLimit() {
const cpuUsage = await cpu.usage();

if (this.activeLimit > 0) {
if (cpuUsage < this.config.cpuUsage) this.activeLimit += 1;
}
}

async setQueueLimit() {
async checkMem(job) {
const { freeMemMb } = await mem.free();

if (freeMemMb < 0) {
this.queueLimit = 0;
}
if (freeMemMb <= 0) job.setState(true, 'reject');
}

createJob(data) {
this.setQueueLimit();
const job = new Job(this, data);

process.nextTick(() => {
if (!this.canQueue()) return job.setState(true, 'reject');
if (this.canStart() && this.queue.length === 0) return this.startJob(job);
return this.enqueueJob(job);
});
// await this.checkMem(job);

// process.nextTick(() => {
if (!this.canQueue()) return job.setState(true, 'reject');
if (this.canStart() && this.queue.length === 0) return this.startJob(job);
this.enqueueJob(job);
// });

process.nextTick(this.checkQueue.bind(this));

return job;
}

startJob(job) {
this.activeCount += 1;
this.active.push(job);
job.setState(true, 'process', (state) => {
this.completeJob(job, state);
job.setState(true, 'process', () => {
this.completeJob(job);
});

return job;
}

dequeueJob(job = null) {
if (job) {
const index = this.queue.indexOf(job);

if (index < 0) {
throw new Error('request not found in queue');
} else {
this.queue.splice(index, 1);
}
} else {
job = this.queue.shift();
}
job.setState(false, 'dequeue');
completeJob(job) {
this.dequeueActive();
job.setState(false, 'complete');
process.nextTick(this.checkQueue.bind(this));
}

return job;
stopHandler(job) {
const { req, next } = job.data;

return {
next: () => next(),
save: () => this.removeSavedFile(job),
convert: () => req.converter && req.converter.stop(true),
};
}

completeJob(job, state) {
stopJob(job) {
const { req, res } = job.data;
const stage = req.stage.next ? 'next' : req.stage.stage;
const stopHandler = this.stopHandler(job);

if (req.converter) req.isStop = true;

this.dequeueActive();
job.setState(false, 'stop');

if (!stopHandler[stage]) res.end();
else stopHandler[stage]();

process.nextTick(this.checkQueue.bind(this));
}

dequeueActive() {
if (this.active.length < 0) {
throw new Error('request complete but not found');
}
this.active.pop();
if (this.activeCount !== 0) this.activeCount -= 1;
}

dequeueJob(job = null) {
const dequeueJob = job ? this.checkJobinQueue(job) : this.queue.shift();
dequeueJob.setState(false, 'dequeue');

return dequeueJob;
}

checkJobinQueue(job) {
const index = this.queue.indexOf(job);

if (index < 0) throw new Error('request not found in queue');
else this.queue.splice(index, 1);

job.setState(false, state);
process.nextTick(this.checkQueue.bind(this, job));
return job;
}

enqueueJob = (job) => {
this.queue.push(job);
job.setState(true, 'queue', () => {
this.cancelJob(job);
});
job.setState(false, 'queue');

return job;
};

checkQueue() {
if (this.canStart() && this.queue.length > 0) {
const nextjob = this.dequeueJob();
this.startJob(nextjob);
}
}

cancelJob(job) {
this.dequeueJob(job);
job.setState(false, 'cancel-dequeue');
job.setState(false, 'cancle-queue');

return job;
}

checkQueue(job = null) {
if (job && job.state === 'cancel') {
job.data.next();
} else if (this.canStart() && this.queue.length > 0) {
const nextjob = this.dequeueJob();
this.startJob(nextjob);
}
removeSavedFile(job) {
setTimeout(() => {
fs.unlink(job.data.req.stage.path, (err) => {
if (err) this.removeSavedFile(job);
});
job.data.res.end();
}, CLEAR_TIME);
}

canQueue() {
Expand Down
9 changes: 0 additions & 9 deletions server-converter/src/middlewares/checkSave.ts

This file was deleted.

26 changes: 13 additions & 13 deletions server-converter/src/middlewares/convert.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import * as path from 'path';
import { Converter } from '../core';
import { RequestHandler, OutputNaming } from '../@types';
import { RequestHandler } from '../@types';
import { noitfyProgress } from './progress';
import { PROGRESS_CONVERTING } from '../constants';
import handleResponse from './stop';
import { PROGRESS_CONVERTING, CONVERT_TIMEOUT } from '../constants';

const convertMiddleware: RequestHandler = (req, _, next) => {
req.isConverted = true;
const convertMiddleware: RequestHandler = (req, res, next) => {
res.setTimeout(CONVERT_TIMEOUT, () => { res.emit('close'); });
req.stage = { stage: 'convert', next: false };
const { channelId } = req.params;
const naming: OutputNaming = (page: number) => `${channelId}_${page}`;
const inputPath = req.file.path;
const outputPath = path.resolve(__dirname, '../../tmpFiles');
const converter = new Converter(inputPath, outputPath, naming);
const response = handleResponse(req, res, next);
const convertDone = (slides) => {
req.slides = slides;
req.slideRatioList = slides.map((slide) => slide.ratio);
next();
response();
};
const { converter } = req;

req.converter = converter;
converter.init().then(() => {
converter.engine.on('progress', ({ page, length }) => noitfyProgress(channelId, {
status: 'convert',
message: `${PROGRESS_CONVERTING}: ${page}/${length} 완료`,
}));
converter.convert().then(convertDone).catch((err) => next(err));
converter.convert().then(convertDone).catch((err) => {
response(err);
});
});
};

Expand Down
18 changes: 18 additions & 0 deletions server-converter/src/middlewares/createConverter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as path from 'path';
import { Converter } from '../core';
import handleResponse from './stop';
import { RequestHandler, OutputNaming } from '../@types';

const creaveConverter: RequestHandler = (req:any, res, next) => {
const { channelId } = req.params;
const naming: OutputNaming = (page: number) => `${channelId}_${page}`;
const inputPath = req.file.path;
const outputPath = path.resolve(__dirname, '../../tmpFiles');
const converter = new Converter(inputPath, outputPath, naming);
const response = handleResponse(req, res, next);

req.converter = converter;
response();
};

export default creaveConverter;
Loading

0 comments on commit 7113db0

Please sign in to comment.