Switch to using ScheduledExecutor for MediaScanner

master
Randomnicode 5 years ago
parent a9a7b08230
commit 8728566860
  1. 43
      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 javax.annotation.PostConstruct;
import java.io.File; import java.io.File;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*; 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. * Provides services for scanning the music library.
@ -49,7 +54,9 @@ public class MediaScannerService {
private MediaLibraryStatistics statistics; private MediaLibraryStatistics statistics;
private boolean scanning; private boolean scanning;
private Timer timer;
private ScheduledExecutorService scheduler;
@Autowired @Autowired
private SettingsService settingsService; private SettingsService settingsService;
@Autowired @Autowired
@ -82,17 +89,9 @@ public class MediaScannerService {
* Schedule background execution of media library scanning. * Schedule background execution of media library scanning.
*/ */
public synchronized void schedule() { public synchronized void schedule() {
if (timer != null) { if (scheduler != null) {
timer.cancel(); scheduler.shutdown();
} }
timer = new Timer(true);
TimerTask task = new TimerTask() {
@Override
public void run() {
scanLibrary();
}
};
long daysBetween = settingsService.getIndexCreationInterval(); long daysBetween = settingsService.getIndexCreationInterval();
int hour = settingsService.getIndexCreationHour(); int hour = settingsService.getIndexCreationHour();
@ -102,22 +101,18 @@ public class MediaScannerService {
return; return;
} }
Date now = new Date(); scheduler = Executors.newSingleThreadScheduledExecutor();
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)) { LocalDateTime now = LocalDateTime.now();
cal.add(Calendar.DATE, 1); 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);
Date firstTime = cal.getTime(); scheduler.scheduleAtFixedRate(() -> scanLibrary(), initialDelay, TimeUnit.DAYS.toMillis(daysBetween), TimeUnit.MILLISECONDS);
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); 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. // In addition, create index immediately if it doesn't exist on disk.
if (settingsService.getLastScanned() == null) { if (settingsService.getLastScanned() == null) {

Loading…
Cancel
Save