-
Notifications
You must be signed in to change notification settings - Fork 0
Schedule 생성 api #216
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
Schedule 생성 api #216
Conversation
| public Schedule toEntity(Long workflowId, Long userId) { | ||
| return Schedule.builder() | ||
| .workflowId(workflowId) | ||
| .cronExpression(this.cronExpression) | ||
| .scheduleText(this.scheduleText) | ||
| .isActive(this.isActive != null ? this.isActive : true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
정적 팩토리 메서드로 변경해주시면 좋을 것 같습니다.
- 객체 책임 명확화
- DTO의 역할
- DTO 내부 상태를 변경하는 작업이 아니기 때문에 인스턴스 메서드가 아닌 클래스 레벨로 두는 것이 좋을 것 같습니다.
public static Schedule toEntity(ScheduleRequestDto dto, Long workflowId, Long userId) {
return Schedule.builder()
.workflowId(workflowId)
// 인스턴스가 아닌 정적 메서드의 인수로 받은 dto를 사용
.cronExpression(dto.cronExpression)
.scheduleText(dto.scheduleText)
.isActive(dto.isActive != null ? dto.isActive : true)
.parameters(dto.parameters)
.createdBy(userId)
.updatedBy(userId)
.build();
}| * @return 스케줄이 1개 이상 있으면 true | ||
| */ | ||
| public boolean hasSchedules() { | ||
| return schedules != null && !schedules.isEmpty(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
여담으로 이런 방법도 있긴 합니다.
import org.springframework.util.CollectionUtils;
public boolean hasSchedules() {
return !CollectionUtils.isEmpty(schedules);
}| // 4. 워크플로우 이름 중복 체크 | ||
| if (workflowMapper.existsByName(dto.getName())) { | ||
| throw new IllegalArgumentException("이미 존재하는 워크플로우 이름입니다 : " + dto.getName()); | ||
| throw new IllegalArgumentException("이미 존재하는 워크플로우 이름입니다: " + dto.getName()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
common/execption/DuplicateDataException.class 사용해주세요
| private void registerSchedules( | ||
| Long workflowId, List<ScheduleCreateDto> scheduleCreateDtos, Long userId) { | ||
| if (scheduleCreateDtos == null || scheduleCreateDtos.isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| log.info("스케줄 등록 시작: Workflow ID {} - {}개", workflowId, scheduleCreateDtos.size()); | ||
|
|
||
| int successCount = 0; | ||
| int failCount = 0; | ||
|
|
||
| for (ScheduleCreateDto dto : scheduleCreateDtos) { | ||
| try { | ||
| // 1. DTO → Model 변환 | ||
| Schedule schedule = dto.toEntity(workflowId, userId); | ||
|
|
||
| // 2. DB 중복 체크 (같은 워크플로우 + 같은 크론식) | ||
| if (scheduleMapper.existsByWorkflowIdAndCronExpression( | ||
| workflowId, schedule.getCronExpression())) { | ||
| throw new DuplicateDataException( | ||
| "이미 동일한 크론식의 스케줄이 존재합니다: " + schedule.getCronExpression()); | ||
| } | ||
|
|
||
| // 3. DB 저장 | ||
| int insertResult = scheduleMapper.insertSchedule(schedule); | ||
| if (insertResult != 1) { | ||
| log.error("스케줄 DB 저장 실패: Workflow ID {} - {}", workflowId, schedule.getCronExpression()); | ||
| failCount++; | ||
| continue; | ||
| } | ||
|
|
||
| // 4. Quartz 등록 (실시간 반영) | ||
| quartzScheduleService.addOrUpdateSchedule(schedule); | ||
|
|
||
| log.info( | ||
| "스케줄 등록 완료: Workflow ID {} - {} ({})", | ||
| workflowId, | ||
| schedule.getCronExpression(), | ||
| schedule.getScheduleText()); | ||
| successCount++; | ||
|
|
||
| } catch (DuplicateDataException e) { | ||
| log.warn("스케줄 중복으로 등록 건너뜀: Workflow ID {} - {}", workflowId, dto.getCronExpression()); | ||
| failCount++; | ||
| // 중복은 경고만 하고 계속 진행 | ||
| } catch (Exception e) { | ||
| log.error("스케줄 등록 실패: Workflow ID {} - {}", workflowId, dto.getCronExpression(), e); | ||
| failCount++; | ||
| // 스케줄 등록 실패해도 워크플로우는 유지 | ||
| } | ||
| } | ||
|
|
||
| log.info("스케줄 등록 완료: Workflow ID {} - 성공 {}개, 실패 {}개", workflowId, successCount, failCount); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 메서드에서 사용하는 로거는 execution log 테이블에 들어가야 하나요?
만약 그렇다면 아래와 같이 해주시면 됩니다.
private static final Logger workflowLogger = LoggerFactory.getLogger("WORKFLOW_HISTORY");
workflowLogger.info();- 사용법은 아래 클래스 확인 부탁드립니다.
site.icebang.domain.workflow.service.TaskExecutionService
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bwnfo3 확인해주시면 감사하겠습니다.
추가로 WorkflowController에서 아래 부분 수정 부탁드립니다.
@PostMapping("")
@ResponseStatus(HttpStatus.CREATED)
public ApiResponse<Void> createWorkflow(
@Valid @RequestBody WorkflowCreateDto workflowCreateDto,
@AuthenticationPrincipal AuthCredential authCredential) {
// 인증 체크
if (authCredential == null) {
throw new IllegalArgumentException("로그인이 필요합니다");
}참조: https://github.com/Kernel180-BE12/Final-4team-icebang/pull/211#discussion_r2379011039
📝 작업 내용
schedule 생성 api와 생성할 때 관련된 메서드들 구현입니다.
Quartz에 실시간으로 연동은 구현중이고 잡, 태스크 생성과 연결하기 위해서 1차 pr입니다.
🔗 관련 이슈
💬 추가 요청사항
✅ 체크리스트
코드 품질
테스트
배포 준비