Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/RSJ-75' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
PierpaoloSpadafora committed Dec 6, 2024
2 parents 346658f + b10a746 commit b5ae173
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 10 deletions.
12 changes: 3 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
name: Java CI

on:
push:
branches: [main, dev]
pull_request:
branches: [main, dev]


schedule:
- cron: '0 0 * * *'
jobs:
build:
runs-on: ubuntu-latest

services:
postgres:
image: postgres:13
Expand All @@ -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:
Expand All @@ -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
run: mvn -f Backend/pom.xml test
Original file line number Diff line number Diff line change
Expand Up @@ -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 {


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public interface ScheduleRepository extends JpaRepository<Schedule, Long> {
List<Schedule> findByStartTimeBetween(LocalDateTime start, LocalDateTime end);

List<Schedule> findByMachineType_IdAndStatus(long machineTypeId, ScheduleStatus scheduleStatus);

List<Schedule> findByStatusNotInAndStartTimeBefore(List<ScheduleStatus> statuses, LocalDateTime dateTime);
List<Schedule> findAll();

@Query(value = "SELECT MAX(start_time + (duration * INTERVAL '1 second')) " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,28 @@ 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))
.dueDate(scheduleDTO.getDueDate())
.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) {
Expand Down Expand Up @@ -405,4 +419,33 @@ public List<Schedule> getSchedulesDueAfter(LocalDateTime date) {
}
}


@Transactional
private void updateScheduleStatuses() {
LocalDateTime now = LocalDateTime.now();

List<Schedule> 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);
}
}

}
Original file line number Diff line number Diff line change
@@ -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<Schedule> 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);
}
}
}

0 comments on commit b5ae173

Please sign in to comment.