CollectionWatcher: Abort collection scan on exit

This commit is contained in:
Jonas Kvinge
2022-01-06 23:14:10 +01:00
parent 2f3e8986ab
commit 7d96fe8b65
3 changed files with 18 additions and 14 deletions

View File

@@ -137,6 +137,7 @@ void SCollection::Exit() {
QObject::connect(backend_, &CollectionBackend::ExitFinished, this, &SCollection::ExitReceived); QObject::connect(backend_, &CollectionBackend::ExitFinished, this, &SCollection::ExitReceived);
QObject::connect(watcher_, &CollectionWatcher::ExitFinished, this, &SCollection::ExitReceived); QObject::connect(watcher_, &CollectionWatcher::ExitFinished, this, &SCollection::ExitReceived);
backend_->ExitAsync(); backend_->ExitAsync();
watcher_->Abort();
watcher_->ExitAsync(); watcher_->ExitAsync();
} }

View File

@@ -89,6 +89,7 @@ CollectionWatcher::CollectionWatcher(Song::Source source, QObject *parent)
expire_unavailable_songs_days_(60), expire_unavailable_songs_days_(60),
overwrite_rating_(false), overwrite_rating_(false),
stop_requested_(false), stop_requested_(false),
abort_requested_(false),
rescan_in_progress_(false), rescan_in_progress_(false),
rescan_timer_(new QTimer(this)), rescan_timer_(new QTimer(this)),
periodic_scan_timer_(new QTimer(this)), periodic_scan_timer_(new QTimer(this)),
@@ -212,7 +213,7 @@ CollectionWatcher::ScanTransaction::ScanTransaction(CollectionWatcher *watcher,
CollectionWatcher::ScanTransaction::~ScanTransaction() { CollectionWatcher::ScanTransaction::~ScanTransaction() {
// If we're stopping then don't commit the transaction // If we're stopping then don't commit the transaction
if (!watcher_->stop_requested_) { if (!watcher_->stop_requested_ && !watcher_->abort_requested_) {
CommitNewOrUpdatedSongs(); CommitNewOrUpdatedSongs();
} }
@@ -392,7 +393,7 @@ void CollectionWatcher::AddDirectory(const Directory &dir, const SubdirectoryLis
transaction.SetKnownSubdirs(subdirs); transaction.SetKnownSubdirs(subdirs);
transaction.AddToProgressMax(files_count); transaction.AddToProgressMax(files_count);
for (const Subdirectory &subdir : subdirs) { for (const Subdirectory &subdir : subdirs) {
if (stop_requested_) break; if (stop_requested_ || abort_requested_) break;
if (scan_on_startup_) ScanSubdirectory(subdir.path, subdir, subdir_files_count[subdir.path], &transaction); if (scan_on_startup_) ScanSubdirectory(subdir.path, subdir, subdir_files_count[subdir.path], &transaction);
@@ -457,7 +458,7 @@ void CollectionWatcher::ScanSubdirectory(const QString &path, const Subdirectory
QDirIterator it(path, QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot); QDirIterator it(path, QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot);
while (it.hasNext()) { while (it.hasNext()) {
if (stop_requested_) return; if (stop_requested_ || abort_requested_) return;
QString child(it.next()); QString child(it.next());
QFileInfo child_info(child); QFileInfo child_info(child);
@@ -489,7 +490,7 @@ void CollectionWatcher::ScanSubdirectory(const QString &path, const Subdirectory
} }
} }
if (stop_requested_) return; if (stop_requested_ || abort_requested_) return;
// Ask the database for a list of files in this directory // Ask the database for a list of files in this directory
SongList songs_in_db = t->FindSongsInSubdirectory(path); SongList songs_in_db = t->FindSongsInSubdirectory(path);
@@ -500,7 +501,7 @@ void CollectionWatcher::ScanSubdirectory(const QString &path, const Subdirectory
QStringList files_on_disk_copy = files_on_disk; QStringList files_on_disk_copy = files_on_disk;
for (const QString &file : files_on_disk_copy) { for (const QString &file : files_on_disk_copy) {
if (stop_requested_) return; if (stop_requested_ || abort_requested_) return;
// Associated CUE // Associated CUE
QString new_cue = CueParser::FindCueFilename(file); QString new_cue = CueParser::FindCueFilename(file);
@@ -693,7 +694,7 @@ void CollectionWatcher::ScanSubdirectory(const QString &path, const Subdirectory
// Recurse into the new subdirs that we found // Recurse into the new subdirs that we found
for (const Subdirectory &my_new_subdir : my_new_subdirs) { for (const Subdirectory &my_new_subdir : my_new_subdirs) {
if (stop_requested_) return; if (stop_requested_ || abort_requested_) return;
ScanSubdirectory(my_new_subdir.path, my_new_subdir, 0, t, true); ScanSubdirectory(my_new_subdir.path, my_new_subdir, 0, t, true);
} }
@@ -997,7 +998,7 @@ void CollectionWatcher::RescanPathsNow() {
QList<int> dirs = rescan_queue_.keys(); QList<int> dirs = rescan_queue_.keys();
for (const int dir : dirs) { for (const int dir : dirs) {
if (stop_requested_) break; if (stop_requested_ || abort_requested_) break;
ScanTransaction transaction(this, dir, false, false, mark_songs_unavailable_); ScanTransaction transaction(this, dir, false, false, mark_songs_unavailable_);
QMap<QString, quint64> subdir_files_count; QMap<QString, quint64> subdir_files_count;
@@ -1008,7 +1009,7 @@ void CollectionWatcher::RescanPathsNow() {
} }
for (const QString &path : rescan_queue_[dir]) { for (const QString &path : rescan_queue_[dir]) {
if (stop_requested_) break; if (stop_requested_ || abort_requested_) break;
Subdirectory subdir; Subdirectory subdir;
subdir.directory_id = dir; subdir.directory_id = dir;
subdir.mtime = 0; subdir.mtime = 0;
@@ -1053,7 +1054,7 @@ QString CollectionWatcher::PickBestImage(const QStringList &images) {
QString biggest_path; QString biggest_path;
for (const QString &path : filtered) { for (const QString &path : filtered) {
if (stop_requested_) break; if (stop_requested_ || abort_requested_) break;
QImage image(path); QImage image(path);
if (image.isNull()) continue; if (image.isNull()) continue;
@@ -1146,7 +1147,7 @@ void CollectionWatcher::RescanTracksNow() {
// Currently we are too stupid to rescan one file at a time, so we'll just scan the full directories // Currently we are too stupid to rescan one file at a time, so we'll just scan the full directories
QStringList scanned_dirs; // To avoid double scans QStringList scanned_dirs; // To avoid double scans
while (!song_rescan_queue_.isEmpty()) { while (!song_rescan_queue_.isEmpty()) {
if (stop_requested_) break; if (stop_requested_ || abort_requested_) break;
Song song = song_rescan_queue_.takeFirst(); Song song = song_rescan_queue_.takeFirst();
QString songdir = song.url().toLocalFile().section('/', 0, -2); QString songdir = song.url().toLocalFile().section('/', 0, -2);
if (!scanned_dirs.contains(songdir)) { if (!scanned_dirs.contains(songdir)) {
@@ -1172,7 +1173,7 @@ void CollectionWatcher::PerformScan(const bool incremental, const bool ignore_mt
for (const Directory &dir : std::as_const(watched_dirs_)) { for (const Directory &dir : std::as_const(watched_dirs_)) {
if (stop_requested_) break; if (stop_requested_ || abort_requested_) break;
ScanTransaction transaction(this, dir.id, incremental, ignore_mtimes, mark_songs_unavailable_); ScanTransaction transaction(this, dir.id, incremental, ignore_mtimes, mark_songs_unavailable_);
SubdirectoryList subdirs(transaction.GetAllSubdirs()); SubdirectoryList subdirs(transaction.GetAllSubdirs());
@@ -1190,7 +1191,7 @@ void CollectionWatcher::PerformScan(const bool incremental, const bool ignore_mt
transaction.AddToProgressMax(files_count); transaction.AddToProgressMax(files_count);
for (const Subdirectory &subdir : subdirs) { for (const Subdirectory &subdir : subdirs) {
if (stop_requested_) break; if (stop_requested_ || abort_requested_) break;
ScanSubdirectory(subdir.path, subdir, subdir_files_count[subdir.path], &transaction); ScanSubdirectory(subdir.path, subdir, subdir_files_count[subdir.path], &transaction);
} }
@@ -1208,7 +1209,7 @@ quint64 CollectionWatcher::FilesCountForPath(ScanTransaction *t, const QString &
QDirIterator it(path, QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot); QDirIterator it(path, QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot);
while (it.hasNext()) { while (it.hasNext()) {
if (stop_requested_) break; if (stop_requested_ || abort_requested_) break;
QString child = it.next(); QString child = it.next();
QFileInfo path_info(child); QFileInfo path_info(child);
@@ -1245,7 +1246,7 @@ quint64 CollectionWatcher::FilesCountForSubdirs(ScanTransaction *t, const Subdir
quint64 i = 0; quint64 i = 0;
for (const Subdirectory &subdir : subdirs) { for (const Subdirectory &subdir : subdirs) {
if (stop_requested_) break; if (stop_requested_ || abort_requested_) break;
const quint64 files_count = FilesCountForPath(t, subdir.path); const quint64 files_count = FilesCountForPath(t, subdir.path);
subdir_files_count[subdir.path] = files_count; subdir_files_count[subdir.path] = files_count;
i += files_count; i += files_count;

View File

@@ -62,6 +62,7 @@ class CollectionWatcher : public QObject {
void ReloadSettingsAsync(); void ReloadSettingsAsync();
void Stop() { stop_requested_ = true; } void Stop() { stop_requested_ = true; }
void Abort() { abort_requested_ = true; }
void ExitAsync(); void ExitAsync();
@@ -218,6 +219,7 @@ class CollectionWatcher : public QObject {
bool overwrite_rating_; bool overwrite_rating_;
bool stop_requested_; bool stop_requested_;
bool abort_requested_;
bool rescan_in_progress_; // True if RescanTracksNow() has been called and is working. bool rescan_in_progress_; // True if RescanTracksNow() has been called and is working.
QMap<int, Directory> watched_dirs_; QMap<int, Directory> watched_dirs_;