From 7b73dd2d62333725f3974e9ec041e936e37a19c9 Mon Sep 17 00:00:00 2001
From: syuilo <Syuilotan@yahoo.co.jp>
Date: Mon, 23 Jan 2023 20:07:48 +0900
Subject: [PATCH] =?UTF-8?q?enhance(server):=20onlyServer=20/=20onlyQueue?=
 =?UTF-8?q?=20=E3=82=AA=E3=83=97=E3=82=B7=E3=83=A7=E3=83=B3=E3=82=92?=
 =?UTF-8?q?=E5=BE=A9=E6=B4=BB?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 packages/backend/src/boot/common.ts | 35 +++++++++++++++++++++++++++++
 packages/backend/src/boot/master.ts | 33 ++++++++-------------------
 packages/backend/src/boot/worker.ts | 23 ++++++++-----------
 3 files changed, 53 insertions(+), 38 deletions(-)
 create mode 100644 packages/backend/src/boot/common.ts

diff --git a/packages/backend/src/boot/common.ts b/packages/backend/src/boot/common.ts
new file mode 100644
index 0000000000..04aa26e652
--- /dev/null
+++ b/packages/backend/src/boot/common.ts
@@ -0,0 +1,35 @@
+import { NestFactory } from '@nestjs/core';
+import { ChartManagementService } from '@/core/chart/ChartManagementService.js';
+import { QueueProcessorService } from '@/queue/QueueProcessorService.js';
+import { NestLogger } from '@/NestLogger.js';
+import { QueueProcessorModule } from '@/queue/QueueProcessorModule.js';
+import { JanitorService } from '@/daemons/JanitorService.js';
+import { QueueStatsService } from '@/daemons/QueueStatsService.js';
+import { ServerStatsService } from '@/daemons/ServerStatsService.js';
+import { ServerService } from '@/server/ServerService.js';
+import { MainModule } from '@/MainModule.js';
+
+export async function server() {
+	const app = await NestFactory.createApplicationContext(MainModule, {
+		logger: new NestLogger(),
+	});
+	app.enableShutdownHooks();
+
+	const serverService = app.get(ServerService);
+	serverService.launch();
+
+	app.get(ChartManagementService).start();
+	app.get(JanitorService).start();
+	app.get(QueueStatsService).start();
+	app.get(ServerStatsService).start();
+}
+
+export async function jobQueue() {
+	const jobQueue = await NestFactory.createApplicationContext(QueueProcessorModule, {
+		logger: new NestLogger(),
+	});
+	jobQueue.enableShutdownHooks();
+
+	jobQueue.get(QueueProcessorService).start();
+	jobQueue.get(ChartManagementService).start();
+}
diff --git a/packages/backend/src/boot/master.ts b/packages/backend/src/boot/master.ts
index 93cb3131ba..f5d936fadf 100644
--- a/packages/backend/src/boot/master.ts
+++ b/packages/backend/src/boot/master.ts
@@ -6,21 +6,12 @@ import cluster from 'node:cluster';
 import chalk from 'chalk';
 import chalkTemplate from 'chalk-template';
 import semver from 'semver';
-import { NestFactory } from '@nestjs/core';
 import Logger from '@/logger.js';
 import { loadConfig } from '@/config.js';
 import type { Config } from '@/config.js';
-import { lessThan } from '@/misc/prelude/array.js';
 import { showMachineInfo } from '@/misc/show-machine-info.js';
-import { DaemonModule } from '@/daemons/DaemonModule.js';
-import { JanitorService } from '@/daemons/JanitorService.js';
-import { QueueStatsService } from '@/daemons/QueueStatsService.js';
-import { ServerStatsService } from '@/daemons/ServerStatsService.js';
-import { NestLogger } from '@/NestLogger.js';
-import { ChartManagementService } from '@/core/chart/ChartManagementService.js';
-import { ServerService } from '@/server/ServerService.js';
-import { MainModule } from '@/MainModule.js';
-import { envOption } from '../env.js';
+import { envOption } from '@/env.js';
+import { jobQueue, server } from './common.js';
 
 const _filename = fileURLToPath(import.meta.url);
 const _dirname = dirname(_filename);
@@ -73,14 +64,13 @@ export async function masterMain() {
 		process.exit(1);
 	}
 
-	const app = await NestFactory.createApplicationContext(MainModule, {
-		logger: new NestLogger(),
-	});
-	app.enableShutdownHooks();
-
-	// start server
-	const serverService = app.get(ServerService);
-	serverService.launch();
+	if (envOption.onlyServer) {
+		await server();
+	} else if (envOption.onlyQueue) {
+		await jobQueue();
+	} else {
+		await server();
+	}
 
 	bootLogger.succ('Misskey initialized');
 
@@ -89,11 +79,6 @@ export async function masterMain() {
 	}
 
 	bootLogger.succ(`Now listening on port ${config.port} on ${config.url}`, null, true);
-
-	app.get(ChartManagementService).start();
-	app.get(JanitorService).start();
-	app.get(QueueStatsService).start();
-	app.get(ServerStatsService).start();
 }
 
 function showEnvironment(): void {
diff --git a/packages/backend/src/boot/worker.ts b/packages/backend/src/boot/worker.ts
index e0574643b7..ab75aaa572 100644
--- a/packages/backend/src/boot/worker.ts
+++ b/packages/backend/src/boot/worker.ts
@@ -1,23 +1,18 @@
 import cluster from 'node:cluster';
-import { NestFactory } from '@nestjs/core';
-import { ChartManagementService } from '@/core/chart/ChartManagementService.js';
-import { QueueProcessorService } from '@/queue/QueueProcessorService.js';
-import { NestLogger } from '@/NestLogger.js';
-import { QueueProcessorModule } from '@/queue/QueueProcessorModule.js';
+import { envOption } from '@/env.js';
+import { jobQueue, server } from './common.js';
 
 /**
  * Init worker process
  */
 export async function workerMain() {
-	const jobQueue = await NestFactory.createApplicationContext(QueueProcessorModule, {
-		logger: new NestLogger(),
-	});
-	jobQueue.enableShutdownHooks();
-
-	// start job queue
-	jobQueue.get(QueueProcessorService).start();
-
-	jobQueue.get(ChartManagementService).start();
+	if (envOption.onlyServer) {
+		await server();
+	} else if (envOption.onlyQueue) {
+		await jobQueue();
+	} else {
+		await jobQueue();
+	}
 
 	if (cluster.isWorker) {
 		// Send a 'ready' message to parent process