Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 | 1x 1x 1x 1x 1x 1x | import { EventEmitter } from "eventemitter3";
import {
TaskPriority,
WorkerEvents,
WorkerStatus,
Task,
} from "../types/index.js";
import { IWorker } from "../types/interfaces.js";
import { StreamMessageType } from "../types/stream.js";
// 웹 환경 여부 확인
const isWeb = typeof window !== "undefined" && typeof Worker !== "undefined";
/**
* 우선순위 메시지 인터페이스
*/
interface PriorityMessage {
message: any;
priority: TaskPriority;
timestamp: number;
}
/**
* 워커 초기화 옵션
*/
export interface WorkerOptions {
/** 워커 ID */
id: string;
/** 워커 URL (웹 환경) */
url?: string;
/** 워커 파일 경로 (Node.js 환경) */
file?: string;
/** 워커 초기화 데이터 */
workerData?: any;
/** 워커 메시지 큐 크기 제한 */
maxQueueSize?: number;
}
/**
* 워커 어댑터 클래스
* 웹 워커와 Node.js 워커를 일관된 인터페이스로 제공
*/
export class WorkerAdapter extends EventEmitter implements IWorker {
/** 워커 ID */
readonly id: string;
/** 웹 워커 인스턴스 */
private webWorker?: Worker;
/** Node.js 워커 인스턴스 */
private nodeWorker?: any;
/** 워커 상태 */
private _status: WorkerStatus = WorkerStatus.STARTING;
/** 메시지 처리 중 여부 */
private processing: boolean = false;
/** 종료 여부 */
private terminated: boolean = false;
/** 메시지 우선순위 큐 */
private messageQueue: PriorityMessage[] = [];
/** 최대 큐 크기 */
private maxQueueSize: number;
/** 워커 정보 */
info: {
/** 상태 */
status: WorkerStatus;
/** 태스크 수 */
tasks: number;
/** 성능 정보 */
performance: {
/** 평균 태스크 처리 시간 */
averageTaskTime: number;
/** 완료한 태스크 수 */
completedTasks: number;
/** 오류 발생 횟수 */
errors: number;
};
};
/** 워커 유형 */
workerType: string = "default";
/** 워커 생성 시간 */
createdAt: number = Date.now();
/** 마지막 활동 시간 */
lastActiveAt: number = Date.now();
/**
* 워커 상태 속성
*/
get state(): WorkerStatus {
return this._status;
}
/**
* 워커 어댑터 생성자
* @param options 워커 옵션
*/
constructor(options: WorkerOptions) {
super();
this.id = options.id;
this.maxQueueSize = options.maxQueueSize || 100;
// 워커 정보 초기화
this.info = {
status: WorkerStatus.STARTING,
tasks: 0,
performance: {
averageTaskTime: 0,
completedTasks: 0,
errors: 0,
},
};
// 웹 또는 Node.js 환경에 맞게 워커 초기화
if (isWeb && options.url) {
this.initWebWorker(options.url, options.workerData);
} else if (!isWeb && options.file) {
this.initNodeWorker(options.file, options.workerData);
} else {
throw new Error(
"워커 생성에 필요한 URL 또는 파일 경로가 지정되지 않았습니다"
);
}
}
/**
* 웹 워커 초기화
* @param url 워커 URL
* @param workerData 초기화 데이터
*/
private initWebWorker(url: string, workerData?: any): void {
try {
// 웹 워커 생성
this.webWorker = new Worker(url, { type: "module" });
// 메시지 핸들러 등록
this.webWorker.onmessage = (event) => {
this.emit("message", event.data);
};
// 오류 핸들러 등록
this.webWorker.onerror = (error) => {
this.info.performance.errors++;
this.info.status = WorkerStatus.ERROR;
this.emit(
"error",
error instanceof Error ? error : new Error(String(error))
);
};
// 상태 업데이트
this._status = WorkerStatus.IDLE;
this.info.status = WorkerStatus.IDLE;
// 초기화 데이터 전송 (있는 경우)
Iif (workerData) {
this.postMessage({
__workerInit: true,
id: this.id,
data: workerData,
});
}
} catch (error) {
this._status = WorkerStatus.ERROR;
this.info.status = WorkerStatus.ERROR;
this.emit(
"error",
error instanceof Error ? error : new Error(String(error))
);
}
}
/**
* Node.js 워커 초기화
* @param file 워커 파일 경로
* @param workerData 초기화 데이터
*/
private initNodeWorker(file: string, workerData?: any): void {
try {
// Worker Threads 모듈 동적 임포트
const { Worker } = require("worker_threads");
// Node.js 워커 생성
this.nodeWorker = new Worker(file, {
workerData: {
id: this.id,
...workerData,
},
});
// 메시지 핸들러 등록
this.nodeWorker.on("message", (data: any) => {
this.emit("message", data);
});
// 오류 핸들러 등록
this.nodeWorker.on("error", (error: Error) => {
this.info.performance.errors++;
this.info.status = WorkerStatus.ERROR;
this.emit("error", error);
});
// 종료 핸들러 등록
this.nodeWorker.on("exit", (code: number) => {
this.emit("exit", code);
this.terminated = true;
});
// 상태 업데이트
this._status = WorkerStatus.IDLE;
this.info.status = WorkerStatus.IDLE;
} catch (error) {
this._status = WorkerStatus.ERROR;
this.info.status = WorkerStatus.ERROR;
this.emit(
"error",
error instanceof Error ? error : new Error(String(error))
);
}
}
/**
* 워커에 메시지 전송
* @param message 메시지
*/
postMessage(message: any): void {
Iif (this.terminated) {
throw new Error("종료된 워커에 메시지를 전송할 수 없습니다");
}
try {
if (this.webWorker) {
this.webWorker.postMessage(message);
} else Iif (this.nodeWorker) {
this.nodeWorker.postMessage(message);
}
} catch (error) {
this.emit(
"error",
error instanceof Error ? error : new Error(String(error))
);
}
}
/**
* 우선순위 메시지 전송
* @param message 메시지
* @param priority 우선순위
*/
postPrioritizedMessage(
message: any,
priority: TaskPriority = TaskPriority.NORMAL
): void {
// 큐가 가득 찼는지 확인
Iif (this.messageQueue.length >= this.maxQueueSize) {
throw new Error("메시지 큐가 가득 찼습니다");
}
// 메시지를 큐에 추가
this.messageQueue.push({
message,
priority,
timestamp: Date.now(),
});
// 메시지 처리 시작
Iif (!this.processing) {
this.processMessageQueue();
}
}
/**
* 메시지 큐 처리
*/
private async processMessageQueue(): Promise<void> {
Iif (this.processing || this.messageQueue.length === 0 || this.terminated) {
return;
}
this.processing = true;
try {
// 큐 정렬 (우선순위 높은 순, 같은 우선순위면 먼저 들어온 순)
this.messageQueue.sort((a, b) => {
Iif (a.priority !== b.priority) {
return a.priority - b.priority;
}
return a.timestamp - b.timestamp;
});
// 첫 번째 메시지 처리
const nextMessage = this.messageQueue.shift();
Iif (nextMessage) {
this.postMessage(nextMessage.message);
}
} catch (error) {
this.emit(
"error",
error instanceof Error ? error : new Error(String(error))
);
} finally {
this.processing = false;
// 큐에 메시지가 남아있으면 계속 처리
Iif (this.messageQueue.length > 0) {
// 비동기로 다음 처리 예약
setTimeout(() => this.processMessageQueue(), 0);
}
}
}
/**
* 워커 종료
* @param force 강제 종료 여부
*/
async terminate(force: boolean = false): Promise<void> {
Iif (this.terminated) {
return;
}
this._status = WorkerStatus.TERMINATING;
this.info.status = WorkerStatus.TERMINATING;
this.terminated = true;
// 실행 중인 태스크 종료 처리
const terminationError = new Error(
"Worker terminated while processing task"
);
const messageListeners = this.listeners("message");
// 메시지 리스너 제거 (주로 태스크 처리 관련 리스너들)
for (const listener of messageListeners) {
this.off("message", listener);
}
// 모든 대기 중인 태스크에 대한 오류 이벤트 발생
Iif (messageListeners.length > 0) {
this.emit("error", terminationError);
}
if (this.webWorker) {
this.webWorker.terminate();
} else Iif (this.nodeWorker) {
if (force) {
this.nodeWorker.terminate();
} else {
this.nodeWorker.postMessage({ type: "terminate" });
}
}
// 리소스 정리
this.messageQueue = [];
this.webWorker = undefined;
this.nodeWorker = undefined;
// 종료 이벤트 발생
this.emit("exit", 0);
}
/**
* 태스크 시작
* @param task 시작할 태스크
*/
async startTask<T, R>(task: Task<T, R>): Promise<R> {
this._status = WorkerStatus.BUSY;
this.info.status = WorkerStatus.BUSY;
this.lastActiveAt = Date.now();
return new Promise<R>((resolve, reject) => {
const messageHandler = (response: any) => {
try {
Iif (response && response.taskId === task.id) {
this.off("message", messageHandler);
if (response.type === "taskCompleted") {
this._status = WorkerStatus.IDLE;
this.info.status = WorkerStatus.IDLE;
resolve(response.result);
} else Iif (response.type === "taskFailed") {
this._status = WorkerStatus.IDLE;
this.info.status = WorkerStatus.IDLE;
reject(response.error);
}
}
} catch (error) {
this._status = WorkerStatus.ERROR;
this.info.status = WorkerStatus.ERROR;
this.off("message", messageHandler);
reject(error instanceof Error ? error : new Error(String(error)));
this.emit(
"error",
error instanceof Error ? error : new Error(String(error))
);
}
};
this.on("message", messageHandler);
try {
this.postMessage({
type: "startTask",
taskId: task.id,
data: task.data,
});
} catch (error) {
this.off("message", messageHandler);
this._status = WorkerStatus.IDLE;
this.info.status = WorkerStatus.IDLE;
reject(error instanceof Error ? error : new Error(String(error)));
}
});
}
/**
* 워커가 유휴 상태인지 확인
*/
isIdle(): boolean {
return this._status === WorkerStatus.IDLE;
}
/**
* 워커가 바쁜 상태인지 확인
*/
isBusy(): boolean {
return this._status === WorkerStatus.BUSY;
}
/**
* 워커가 사용 가능한지 확인
*/
isAvailable(): boolean {
return (
!this.terminated &&
this._status !== WorkerStatus.ERROR &&
this._status !== WorkerStatus.TERMINATING
);
}
}
/**
* 워커 스크립트에 추가할 이벤트 스트림 처리 코드
*/
export const streamHandlerCode = `
// 활성 스트림 관리
const activeStreams = new Set();
// 스트림 처리 핸들러
function handleStreamMessage(message) {
const { type, streamId, data } = message;
if (!type || !streamId) return;
switch (type) {
case "${StreamMessageType.INIT}":
// 스트림 초기화
activeStreams.add(streamId);
// 준비 완료 메시지 응답
self.postMessage({
type: "${StreamMessageType.READY}",
streamId,
timestamp: Date.now()
});
break;
case "${StreamMessageType.MESSAGE}":
// 스트림 활성 확인
if (activeStreams.has(streamId)) {
// 데이터 처리 및 응답
// 실제 구현에서는 데이터 처리 로직 추가
self.postMessage({
type: "${StreamMessageType.MESSAGE}",
streamId,
data: data, // 처리된 데이터 반환
timestamp: Date.now()
});
}
break;
case "${StreamMessageType.PAUSE}":
// 스트림 일시 중지 처리
if (activeStreams.has(streamId)) {
// 일시 중지 확인 응답
self.postMessage({
type: "${StreamMessageType.PAUSE}",
streamId,
timestamp: Date.now()
});
}
break;
case "${StreamMessageType.RESUME}":
// 스트림 재개 처리
if (activeStreams.has(streamId)) {
// 재개 확인 응답
self.postMessage({
type: "${StreamMessageType.RESUME}",
streamId,
timestamp: Date.now()
});
}
break;
case "${StreamMessageType.CLOSE}":
// 스트림 종료
activeStreams.delete(streamId);
break;
}
}
// 기존 메시지 핸들러 확장
const originalMessageHandler = self.onmessage;
self.onmessage = function(event) {
const message = event.data;
// 스트림 메시지 처리
if (
message &&
typeof message === 'object' &&
message.type &&
message.type.startsWith('STREAM_')
) {
handleStreamMessage(message);
return;
}
// 기존 메시지 핸들러 호출
if (originalMessageHandler) {
originalMessageHandler.call(self, event);
}
};
`;
|