From cceba0b7248d2c08348eea5668cd97d0a3d962c7 Mon Sep 17 00:00:00 2001 From: Ilaria Date: Fri, 6 Dec 2024 10:56:36 +0100 Subject: [PATCH 1/3] RSJ-75 finito, stato dei job aggiornato in base a ora corrente --- .../unical/demacs/rdm/StiJobsApplication.java | 2 + .../repository/ScheduleRepository.java | 2 +- .../implementation/ScheduleServiceImpl.java | 43 ++++++++++++++++ .../utils/ScheduleStatusUpdaterService.java | 50 +++++++++++++++++++ 4 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 Backend/src/main/java/unical/demacs/rdm/utils/ScheduleStatusUpdaterService.java diff --git a/Backend/src/main/java/unical/demacs/rdm/StiJobsApplication.java b/Backend/src/main/java/unical/demacs/rdm/StiJobsApplication.java index 0685737..4022be7 100644 --- a/Backend/src/main/java/unical/demacs/rdm/StiJobsApplication.java +++ b/Backend/src/main/java/unical/demacs/rdm/StiJobsApplication.java @@ -2,11 +2,13 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableScheduling; import java.io.File; import java.nio.file.Paths; @SpringBootApplication +@EnableScheduling public class StiJobsApplication { diff --git a/Backend/src/main/java/unical/demacs/rdm/persistence/repository/ScheduleRepository.java b/Backend/src/main/java/unical/demacs/rdm/persistence/repository/ScheduleRepository.java index cd34d4a..2e8d155 100644 --- a/Backend/src/main/java/unical/demacs/rdm/persistence/repository/ScheduleRepository.java +++ b/Backend/src/main/java/unical/demacs/rdm/persistence/repository/ScheduleRepository.java @@ -21,7 +21,7 @@ public interface ScheduleRepository extends JpaRepository { List findByStartTimeBetween(LocalDateTime start, LocalDateTime end); List findByMachineType_IdAndStatus(long machineTypeId, ScheduleStatus scheduleStatus); - + List findByStatusNotInAndStartTimeBefore(List statuses, LocalDateTime dateTime); List findAll(); @Query(value = "SELECT MAX(start_time + (duration * INTERVAL '1 second')) " + diff --git a/Backend/src/main/java/unical/demacs/rdm/persistence/service/implementation/ScheduleServiceImpl.java b/Backend/src/main/java/unical/demacs/rdm/persistence/service/implementation/ScheduleServiceImpl.java index 65d732b..0277e08 100644 --- a/Backend/src/main/java/unical/demacs/rdm/persistence/service/implementation/ScheduleServiceImpl.java +++ b/Backend/src/main/java/unical/demacs/rdm/persistence/service/implementation/ScheduleServiceImpl.java @@ -43,6 +43,7 @@ public Schedule createSchedule(ScheduleDTO scheduleDTO) { logger.error("Job with id {} not found", scheduleDTO.getJobId()); throw new RuntimeException("Job not found"); } + Schedule schedule = Schedule.scheduleBuilder() .job(jobRepository.findById(scheduleDTO.getJobId()).orElse(null)) .machineType(machineTypeRepository.findById(scheduleDTO.getMachineTypeId()).orElse(null)) @@ -50,7 +51,20 @@ public Schedule createSchedule(ScheduleDTO scheduleDTO) { .startTime(scheduleDTO.getStartTime()) .duration(scheduleDTO.getDuration()) .build(); + + LocalDateTime now = LocalDateTime.now(); + LocalDateTime endTime = schedule.getStartTime().plusSeconds(schedule.getDuration()); + + if (now.isBefore(schedule.getStartTime())) { + schedule.setStatus(ScheduleStatus.SCHEDULED); + } else if (now.isAfter(schedule.getStartTime()) && now.isBefore(endTime)) { + schedule.setStatus(ScheduleStatus.IN_PROGRESS); + } else if (now.isAfter(endTime)) { + schedule.setStatus(ScheduleStatus.COMPLETED); + } + scheduleRepository.save(schedule); + updateScheduleStatuses(); logger.info("Schedule for job with id {} created successfully", scheduleDTO.getJobId()); return schedule; } catch (Exception e) { @@ -405,4 +419,33 @@ public List getSchedulesDueAfter(LocalDateTime date) { } } + + @Transactional + private void updateScheduleStatuses() { + LocalDateTime now = LocalDateTime.now(); + + List activeSchedules = scheduleRepository.findByStatusNotInAndStartTimeBefore( + List.of(ScheduleStatus.COMPLETED, ScheduleStatus.CANCELLED), + now + ); + + for (Schedule schedule : activeSchedules) { + updateScheduleStatus(schedule, now); + } + + scheduleRepository.saveAll(activeSchedules); + } + + private void updateScheduleStatus(Schedule schedule, LocalDateTime now) { + LocalDateTime endTime = schedule.getStartTime().plusSeconds(schedule.getDuration()); + + if (now.isBefore(schedule.getStartTime())) { + schedule.setStatus(ScheduleStatus.SCHEDULED); + } else if (now.isAfter(schedule.getStartTime()) && now.isBefore(endTime)) { + schedule.setStatus(ScheduleStatus.IN_PROGRESS); + } else if (now.isAfter(endTime)) { + schedule.setStatus(ScheduleStatus.COMPLETED); + } + } + } diff --git a/Backend/src/main/java/unical/demacs/rdm/utils/ScheduleStatusUpdaterService.java b/Backend/src/main/java/unical/demacs/rdm/utils/ScheduleStatusUpdaterService.java new file mode 100644 index 0000000..0848225 --- /dev/null +++ b/Backend/src/main/java/unical/demacs/rdm/utils/ScheduleStatusUpdaterService.java @@ -0,0 +1,50 @@ +package unical.demacs.rdm.utils; + +import lombok.RequiredArgsConstructor; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import unical.demacs.rdm.persistence.entities.Schedule; +import unical.demacs.rdm.persistence.enums.ScheduleStatus; +import unical.demacs.rdm.persistence.repository.ScheduleRepository; + +import java.time.LocalDateTime; +import java.util.List; + +@Service +@RequiredArgsConstructor +public class ScheduleStatusUpdaterService { + + private final ScheduleRepository scheduleRepository; + + @Scheduled(fixedRate = 60000) // every minute todo: ragionare su frequenza + @Transactional + public void updateScheduleStatuses() { + LocalDateTime now = LocalDateTime.now(); + + List activeSchedules = scheduleRepository.findByStatusNotInAndStartTimeBefore( + List.of(ScheduleStatus.COMPLETED, ScheduleStatus.CANCELLED), + now + ); + + for (Schedule schedule : activeSchedules) { + updateScheduleStatus(schedule, now); + } + + scheduleRepository.saveAll(activeSchedules); + } + + + + private void updateScheduleStatus(Schedule schedule, LocalDateTime now) { + LocalDateTime endTime = schedule.getStartTime().plusSeconds(schedule.getDuration()); + + if (now.isBefore(schedule.getStartTime())) { + schedule.setStatus(ScheduleStatus.SCHEDULED); + } else if (now.isAfter(schedule.getStartTime()) && now.isBefore(endTime)) { + schedule.setStatus(ScheduleStatus.IN_PROGRESS); + } else if (now.isAfter(endTime)) { + schedule.setStatus(ScheduleStatus.COMPLETED); + } + } +} \ No newline at end of file From 1fbf91c92eae7cd6afaee3bd96956ad04497c057 Mon Sep 17 00:00:00 2001 From: Ilaria Date: Fri, 6 Dec 2024 11:25:18 +0100 Subject: [PATCH 2/3] test --- .github/workflows/ci.yml | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e4654ad..041b350 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,16 +1,14 @@ name: Java CI - on: push: - branches: [main, dev] + branches: [main, dev, RSJ-75] pull_request: - branches: [main, dev] - - + branches: [main, dev, RSJ-75] + schedule: + - cron: '26 11 * * *' # Esegue alle 11:02 ogni giorno jobs: build: runs-on: ubuntu-latest - services: postgres: image: postgres:13 @@ -25,11 +23,9 @@ jobs: --health-interval 10s --health-timeout 5s --health-retries 5 - steps: - name: Checkout code uses: actions/checkout@v2 - - name: Set up JDK 17 uses: actions/setup-java@v2 with: @@ -42,9 +38,7 @@ jobs: key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} path: ~/.m2/repository restore-keys: '${{ runner.os }}-maven-' - - name: Install dependencies and run tests run: mvn -f Backend/pom.xml clean install - - name: Run tests - run: mvn -f Backend/pom.xml test \ No newline at end of file + run: mvn -f Backend/pom.xml test From b10a74610b3770f5b4e5ed207a985e4f8b7c738c Mon Sep 17 00:00:00 2001 From: Ilaria Date: Fri, 6 Dec 2024 11:30:18 +0100 Subject: [PATCH 3/3] Update ci.yml --- .github/workflows/ci.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 041b350..eb6ccae 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,11 +1,11 @@ name: Java CI on: push: - branches: [main, dev, RSJ-75] + branches: [main, dev] pull_request: - branches: [main, dev, RSJ-75] + branches: [main, dev] schedule: - - cron: '26 11 * * *' # Esegue alle 11:02 ogni giorno + - cron: '0 0 * * *' jobs: build: runs-on: ubuntu-latest