Skip to content
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
15 changes: 15 additions & 0 deletions src/main/java/umc/codeplay/controller/TaskController.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package umc.codeplay.controller;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -71,4 +74,16 @@ public ApiResponse<MemberResponseDTO.TaskProgressDTO> getTask(
Task task = taskService.findById(request.getTaskId());
return ApiResponse.onSuccess(MemberConverter.toTaskProgressDTO(task));
}

@Operation(
summary = "작업 진행 상황 조회",
description = "작업 ID를 받아 완료될 때까지 기다린 후 결과를 반환합니다. 기본 대기시간 5분, 10초마다 작업 상태확인.")
@GetMapping("/wait/{taskId}")
public ApiResponse<MemberResponseDTO.TaskProgressDTO> waitTask(
@PathVariable Long taskId,
@RequestParam(defaultValue = "300000") long timeoutMillis // 기본 5분 대기시간 + 10초마다 작업상태 체크
) {
Task task = taskService.waitTask(taskId, timeoutMillis);
return ApiResponse.onSuccess(MemberConverter.toTaskProgressDTO(task));
}
}
26 changes: 26 additions & 0 deletions src/main/java/umc/codeplay/service/TaskService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package umc.codeplay.service;

import java.time.Duration;
import java.time.Instant;

import org.springframework.stereotype.Service;

import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -73,4 +76,27 @@ public Task addTask(Remix newRemix) {

return taskRepository.save(task);
}

public Task waitTask(Long id, long timeoutMillis) {
Instant startTime = Instant.now();
Task task = findById(id);

while (Duration.between(startTime, Instant.now()).toMillis() < timeoutMillis) {

// complete 면 응답
if (task.getStatus().equals(ProcessStatus.COMPLETED)) {
return task;
}

// 3초 대기이후 다시 작업 체크
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new GeneralException(ErrorStatus._INTERNAL_SERVER_ERROR);
}
}

return findById(id);
}
}