From 87285668606fa3ee6be23aecd7be012e59aeea8d Mon Sep 17 00:00:00 2001 From: Randomnicode Date: Sat, 26 Oct 2019 04:25:54 -0700 Subject: [PATCH] Switch to using ScheduledExecutor for MediaScanner --- .../player/service/MediaScannerService.java | 53 +++++++++---------- 1 file changed, 24 insertions(+), 29 deletions(-) diff --git a/airsonic-main/src/main/java/org/airsonic/player/service/MediaScannerService.java b/airsonic-main/src/main/java/org/airsonic/player/service/MediaScannerService.java index e0b94806..be8e2973 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/service/MediaScannerService.java +++ b/airsonic-main/src/main/java/org/airsonic/player/service/MediaScannerService.java @@ -34,7 +34,12 @@ import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import java.io.File; +import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; import java.util.*; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; /** * Provides services for scanning the music library. @@ -49,7 +54,9 @@ public class MediaScannerService { private MediaLibraryStatistics statistics; private boolean scanning; - private Timer timer; + + private ScheduledExecutorService scheduler; + @Autowired private SettingsService settingsService; @Autowired @@ -82,18 +89,10 @@ public class MediaScannerService { * Schedule background execution of media library scanning. */ public synchronized void schedule() { - if (timer != null) { - timer.cancel(); + if (scheduler != null) { + scheduler.shutdown(); } - timer = new Timer(true); - - TimerTask task = new TimerTask() { - @Override - public void run() { - scanLibrary(); - } - }; - + long daysBetween = settingsService.getIndexCreationInterval(); int hour = settingsService.getIndexCreationHour(); @@ -101,23 +100,19 @@ public class MediaScannerService { LOG.info("Automatic media scanning disabled."); return; } - - Date now = new Date(); - Calendar cal = Calendar.getInstance(); - cal.setTime(now); - cal.set(Calendar.HOUR_OF_DAY, hour); - cal.set(Calendar.MINUTE, 0); - cal.set(Calendar.SECOND, 0); - - if (cal.getTime().before(now)) { - cal.add(Calendar.DATE, 1); - } - - Date firstTime = cal.getTime(); - long period = daysBetween * 24L * 3600L * 1000L; - timer.schedule(task, firstTime, period); - - LOG.info("Automatic media library scanning scheduled to run every " + daysBetween + " day(s), starting at " + firstTime); + + scheduler = Executors.newSingleThreadScheduledExecutor(); + + LocalDateTime now = LocalDateTime.now(); + LocalDateTime nextRun = now.withHour(hour).withMinute(0).withSecond(0); + if (now.compareTo(nextRun) > 0) + nextRun = nextRun.plusDays(1); + + long initialDelay = ChronoUnit.MILLIS.between(now, nextRun); + + scheduler.scheduleAtFixedRate(() -> scanLibrary(), initialDelay, TimeUnit.DAYS.toMillis(daysBetween), TimeUnit.MILLISECONDS); + + LOG.info("Automatic media library scanning scheduled to run every {} day(s), starting at {}", daysBetween, nextRun); // In addition, create index immediately if it doesn't exist on disk. if (settingsService.getLastScanned() == null) {