dev #8
@@ -2,10 +2,13 @@ package com.printcalculator;
|
|||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@EnableTransactionManagement
|
@EnableTransactionManagement
|
||||||
|
@EnableScheduling
|
||||||
public class BackendApplication {
|
public class BackendApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|||||||
@@ -3,7 +3,9 @@ package com.printcalculator.repository;
|
|||||||
import com.printcalculator.entity.QuoteSession;
|
import com.printcalculator.entity.QuoteSession;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public interface QuoteSessionRepository extends JpaRepository<QuoteSession, UUID> {
|
public interface QuoteSessionRepository extends JpaRepository<QuoteSession, UUID> {
|
||||||
|
List<QuoteSession> findByCreatedAtBefore(java.time.OffsetDateTime cutoff);
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
package com.printcalculator.service;
|
||||||
|
|
||||||
|
import com.printcalculator.entity.QuoteSession;
|
||||||
|
import com.printcalculator.repository.QuoteSessionRepository;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.time.OffsetDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class SessionCleanupService {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(SessionCleanupService.class);
|
||||||
|
private final QuoteSessionRepository sessionRepository;
|
||||||
|
|
||||||
|
public SessionCleanupService(QuoteSessionRepository sessionRepository) {
|
||||||
|
this.sessionRepository = sessionRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run every day at 3 AM
|
||||||
|
@Scheduled(cron = "0 0 3 * * ?")
|
||||||
|
@Transactional
|
||||||
|
public void cleanupOldSessions() {
|
||||||
|
logger.info("Starting session cleanup job...");
|
||||||
|
|
||||||
|
OffsetDateTime cutoff = OffsetDateTime.now().minusDays(15);
|
||||||
|
List<QuoteSession> oldSessions = sessionRepository.findByCreatedAtBefore(cutoff);
|
||||||
|
|
||||||
|
int deletedCount = 0;
|
||||||
|
for (QuoteSession session : oldSessions) {
|
||||||
|
// We only delete sessions that are NOT ordered?
|
||||||
|
// The user request was "delete old ones".
|
||||||
|
// Safest is to check status if we had one.
|
||||||
|
// QuoteSession entity has 'status' field.
|
||||||
|
// Let's assume we delete 'PENDING' or similar, but maybe we just delete all old inputs?
|
||||||
|
// "rimangono in memoria... cancella quelle vecchie di 7 giorni".
|
||||||
|
// Implementation plan said: status != 'ORDERED'.
|
||||||
|
|
||||||
|
// User specified statuses: ACTIVE, EXPIRED, CONVERTED.
|
||||||
|
// We should NOT delete sessions that have been converted to an order.
|
||||||
|
if ("CONVERTED".equals(session.getStatus())) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Delete ACTIVE or EXPIRED sessions older than 7 days
|
||||||
|
deleteSessionFiles(session.getId().toString());
|
||||||
|
sessionRepository.delete(session);
|
||||||
|
deletedCount++;
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("Failed to cleanup session {}", session.getId(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info("Session cleanup job finished. Deleted {} sessions.", deletedCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deleteSessionFiles(String sessionId) {
|
||||||
|
Path sessionDir = Paths.get("storage_quotes", sessionId);
|
||||||
|
if (Files.exists(sessionDir)) {
|
||||||
|
try (Stream<Path> walk = Files.walk(sessionDir)) {
|
||||||
|
walk.sorted(java.util.Comparator.reverseOrder())
|
||||||
|
.map(Path::toFile)
|
||||||
|
.forEach(java.io.File::delete);
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.error("Failed to delete directory: {}", sessionDir, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -18,6 +18,8 @@ services:
|
|||||||
restart: always
|
restart: always
|
||||||
volumes:
|
volumes:
|
||||||
- backend_profiles_${ENV}:/app/profiles
|
- backend_profiles_${ENV}:/app/profiles
|
||||||
|
- /mnt/cache/appdata/print-calculator/${ENV}/storage_quotes:/app/storage_quotes
|
||||||
|
- /mnt/cache/appdata/print-calculator/${ENV}/storage_orders:/app/storage_orders
|
||||||
|
|
||||||
|
|
||||||
frontend:
|
frontend:
|
||||||
|
|||||||
Reference in New Issue
Block a user