Migrated scanner stuff

This commit is contained in:
emeric
2021-10-20 20:00:14 +02:00
parent 7386c89ae7
commit 398925588f
13 changed files with 173 additions and 174 deletions
-122
View File
@@ -1,122 +0,0 @@
/*
* Copyright (C) 2013 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <chrono>
#include <shared_mutex>
#include <optional>
#include <unordered_set>
#include <Wt/WDateTime.h>
#include <Wt/WIOService.h>
#include <Wt/WSignal.h>
#include <boost/asio/system_timer.hpp>
#include "database/Types.hpp"
#include "database/ScanSettings.hpp"
#include "database/Session.hpp"
#include "metadata/IParser.hpp"
#include "scanner/IScanner.hpp"
#include "utils/Path.hpp"
class UUID;
namespace Recommendation
{
class IRecommendationService;
}
namespace Scanner {
class Scanner : public IScanner
{
public:
Scanner(Database::Db& db, Recommendation::IRecommendationService& recommendationService);
~Scanner();
Scanner(const Scanner&) = delete;
Scanner(Scanner&&) = delete;
Scanner& operator=(const Scanner&) = delete;
Scanner& operator=(Scanner&&) = delete;
void requestReload() override;
void requestImmediateScan(bool force) override;
Status getStatus() const override;
Events& getEvents() override { return _events; }
private:
void start();
void stop();
// Job handling
void scheduleNextScan();
void scheduleScan(bool force, const Wt::WDateTime& dateTime = {});
void abortScan();
// Update database (scheduled callback)
void scan(bool force);
void scanMediaDirectory( const std::filesystem::path& mediaDirectory, bool forceScan, ScanStats& stats);
bool fetchTrackFeatures(Database::TrackId trackId, const UUID& MBID);
void fetchTrackFeatures(ScanStats& stats);
// Helpers
void refreshScanSettings();
void countAllFiles(ScanStats& stats);
void removeMissingTracks(ScanStats& stats);
void removeOrphanEntries();
void checkDuplicatedAudioFiles(ScanStats& stats);
void scanAudioFile(const std::filesystem::path& file, bool forceScan, ScanStats& stats);
void notifyInProgressIfNeeded(const ScanStepStats& stats);
void notifyInProgress(const ScanStepStats& stats);
void reloadSimilarityEngine(ScanStats& stats);
Recommendation::IRecommendationService& _recommendationService;
std::mutex _controlMutex;
std::atomic<bool> _abortScan {};
Wt::WIOService _ioService;
boost::asio::system_timer _scheduleTimer {_ioService};
Events _events;
std::chrono::system_clock::time_point _lastScanInProgressEmit {};
Database::Session _dbSession;
std::unique_ptr<MetaData::IParser> _metadataParser;
mutable std::shared_mutex _statusMutex;
State _curState {State::NotScheduled};
std::optional<ScanStats> _lastCompleteScanStats;
std::optional<ScanStepStats> _currentScanStepStats;
Wt::WDateTime _nextScheduledScan;
// Current scan settings
std::size_t _scanVersion {};
Wt::WTime _startTime;
Database::ScanSettings::UpdatePeriod _updatePeriod {Database::ScanSettings::UpdatePeriod::Never};
std::unordered_set<std::filesystem::path> _fileExtensions;
std::filesystem::path _mediaDirectory;
Database::ScanSettings::RecommendationEngineType _recommendationServiceType;
};
} // Scanner
@@ -17,7 +17,7 @@
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Scanner.hpp"
#include "ScannerService.hpp"
#include <ctime>
#include <boost/asio/placeholders.hpp>
@@ -248,13 +248,13 @@ getOrCreateClusters(Session& session, const MetaData::Clusters& clustersNames)
namespace Scanner {
std::unique_ptr<IScanner>
createScanner(Database::Db& db, Recommendation::IRecommendationService& recommendationService)
std::unique_ptr<IScannerService>
createScannerService(Database::Db& db, Recommendation::IRecommendationService& recommendationService)
{
return std::make_unique<Scanner>(db, recommendationService);
return std::make_unique<ScannerService>(db, recommendationService);
}
Scanner::Scanner(Database::Db& db, Recommendation::IRecommendationService& recommendationService)
ScannerService::ScannerService(Database::Db& db, Recommendation::IRecommendationService& recommendationService)
: _recommendationService {recommendationService}
, _dbSession {db}
{
@@ -268,14 +268,14 @@ Scanner::Scanner(Database::Db& db, Recommendation::IRecommendationService& recom
start();
}
Scanner::~Scanner()
ScannerService::~ScannerService()
{
LMS_LOG(DBUPDATER, INFO) << "Shutting down Scanner...";
stop();
}
void
Scanner::start()
ScannerService::start()
{
std::scoped_lock lock {_controlMutex};
@@ -296,7 +296,7 @@ Scanner::start()
}
void
Scanner::stop()
ScannerService::stop()
{
std::scoped_lock lock {_controlMutex};
@@ -307,7 +307,7 @@ Scanner::stop()
}
void
Scanner::abortScan()
ScannerService::abortScan()
{
LMS_LOG(DBUPDATER, DEBUG) << "Aborting scan...";
std::scoped_lock lock {_controlMutex};
@@ -325,7 +325,7 @@ Scanner::abortScan()
}
void
Scanner::requestImmediateScan(bool force)
ScannerService::requestImmediateScan(bool force)
{
abortScan();
_ioService.post([=]()
@@ -338,7 +338,7 @@ Scanner::requestImmediateScan(bool force)
}
void
Scanner::requestReload()
ScannerService::requestReload()
{
abortScan();
_ioService.post([=]()
@@ -350,8 +350,8 @@ Scanner::requestReload()
});
}
Scanner::Status
Scanner::getStatus() const
ScannerService::Status
ScannerService::getStatus() const
{
Status res;
@@ -366,7 +366,7 @@ Scanner::getStatus() const
}
void
Scanner::scheduleNextScan()
ScannerService::scheduleNextScan()
{
LMS_LOG(DBUPDATER, INFO) << "Scheduling next scan";
@@ -420,7 +420,7 @@ Scanner::scheduleNextScan()
}
void
Scanner::countAllFiles(ScanStats& stats)
ScannerService::countAllFiles(ScanStats& stats)
{
ScanStepStats stepStats{stats.startTime, ScanProgressStep::DiscoveringFiles};
@@ -445,7 +445,7 @@ Scanner::countAllFiles(ScanStats& stats)
}
void
Scanner::scheduleScan(bool force, const Wt::WDateTime& dateTime)
ScannerService::scheduleScan(bool force, const Wt::WDateTime& dateTime)
{
auto cb {[=](boost::system::error_code ec)
{
@@ -474,7 +474,7 @@ Scanner::scheduleScan(bool force, const Wt::WDateTime& dateTime)
}
void
Scanner::scan(bool forceScan)
ScannerService::scan(bool forceScan)
{
_events.scanStarted.emit();
@@ -543,7 +543,7 @@ Scanner::scan(bool forceScan)
}
bool
Scanner::fetchTrackFeatures(Database::TrackId trackId, const UUID& recordingMBID)
ScannerService::fetchTrackFeatures(Database::TrackId trackId, const UUID& recordingMBID)
{
std::map<std::string, double> features;
@@ -569,7 +569,7 @@ Scanner::fetchTrackFeatures(Database::TrackId trackId, const UUID& recordingMBID
}
void
Scanner::fetchTrackFeatures(ScanStats& stats)
ScannerService::fetchTrackFeatures(ScanStats& stats)
{
if (_recommendationServiceType != ScanSettings::RecommendationEngineType::Features)
return;
@@ -619,7 +619,7 @@ Scanner::fetchTrackFeatures(ScanStats& stats)
}
void
Scanner::refreshScanSettings()
ScannerService::refreshScanSettings()
{
auto transaction {_dbSession.createSharedTransaction()};
@@ -651,7 +651,7 @@ Scanner::refreshScanSettings()
}
void
Scanner::notifyInProgress(const ScanStepStats& stepStats)
ScannerService::notifyInProgress(const ScanStepStats& stepStats)
{
{
std::unique_lock lock {_statusMutex};
@@ -664,7 +664,7 @@ Scanner::notifyInProgress(const ScanStepStats& stepStats)
}
void
Scanner::notifyInProgressIfNeeded(const ScanStepStats& stepStats)
ScannerService::notifyInProgressIfNeeded(const ScanStepStats& stepStats)
{
std::chrono::system_clock::time_point now {std::chrono::system_clock::now()};
@@ -673,7 +673,7 @@ Scanner::notifyInProgressIfNeeded(const ScanStepStats& stepStats)
}
void
Scanner::scanAudioFile(const std::filesystem::path& file, bool forceScan, ScanStats& stats)
ScannerService::scanAudioFile(const std::filesystem::path& file, bool forceScan, ScanStats& stats)
{
Wt::WDateTime lastWriteTime;
try
@@ -836,7 +836,7 @@ Scanner::scanAudioFile(const std::filesystem::path& file, bool forceScan, ScanSt
}
void
Scanner::scanMediaDirectory(const std::filesystem::path& mediaDirectory, bool forceScan, ScanStats& stats)
ScannerService::scanMediaDirectory(const std::filesystem::path& mediaDirectory, bool forceScan, ScanStats& stats)
{
ScanStepStats stepStats{stats.startTime, ScanProgressStep::ScanningFiles};
stepStats.totalElems = stats.filesScanned;
@@ -903,7 +903,7 @@ checkFile(const std::filesystem::path& p, const std::filesystem::path& mediaDire
}
void
Scanner::removeMissingTracks(ScanStats& stats)
ScannerService::removeMissingTracks(ScanStats& stats)
{
static constexpr std::size_t batchSize {50};
@@ -970,7 +970,7 @@ Scanner::removeMissingTracks(ScanStats& stats)
}
void
Scanner::removeOrphanEntries()
ScannerService::removeOrphanEntries()
{
LMS_LOG(DBUPDATER, DEBUG) << "Checking orphan clusters...";
{
@@ -1013,7 +1013,7 @@ Scanner::removeOrphanEntries()
}
void
Scanner::checkDuplicatedAudioFiles(ScanStats& stats)
ScannerService::checkDuplicatedAudioFiles(ScanStats& stats)
{
LMS_LOG(DBUPDATER, INFO) << "Checking duplicated audio files";
@@ -1033,7 +1033,7 @@ Scanner::checkDuplicatedAudioFiles(ScanStats& stats)
}
void
Scanner::reloadSimilarityEngine(ScanStats& stats)
ScannerService::reloadSimilarityEngine(ScanStats& stats)
{
ScanStepStats stepStats {stats.startTime, ScanProgressStep::ReloadingSimilarityEngine};
@@ -0,0 +1,121 @@
/*
* Copyright (C) 2013 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <chrono>
#include <shared_mutex>
#include <optional>
#include <unordered_set>
#include <Wt/WDateTime.h>
#include <Wt/WIOService.h>
#include <Wt/WSignal.h>
#include <boost/asio/system_timer.hpp>
#include "database/Types.hpp"
#include "database/ScanSettings.hpp"
#include "database/Session.hpp"
#include "metadata/IParser.hpp"
#include "services/scanner/IScannerService.hpp"
#include "utils/Path.hpp"
class UUID;
namespace Recommendation
{
class IRecommendationService;
}
namespace Scanner
{
class ScannerService : public IScannerService
{
public:
ScannerService(Database::Db& db, Recommendation::IRecommendationService& recommendationService);
~ScannerService();
ScannerService(const ScannerService&) = delete;
ScannerService(ScannerService&&) = delete;
ScannerService& operator=(const ScannerService&) = delete;
ScannerService& operator=(ScannerService&&) = delete;
void requestReload() override;
void requestImmediateScan(bool force) override;
Status getStatus() const override;
Events& getEvents() override { return _events; }
private:
void start();
void stop();
// Job handling
void scheduleNextScan();
void scheduleScan(bool force, const Wt::WDateTime& dateTime = {});
void abortScan();
// Update database (scheduled callback)
void scan(bool force);
void scanMediaDirectory( const std::filesystem::path& mediaDirectory, bool forceScan, ScanStats& stats);
bool fetchTrackFeatures(Database::TrackId trackId, const UUID& MBID);
void fetchTrackFeatures(ScanStats& stats);
// Helpers
void refreshScanSettings();
void countAllFiles(ScanStats& stats);
void removeMissingTracks(ScanStats& stats);
void removeOrphanEntries();
void checkDuplicatedAudioFiles(ScanStats& stats);
void scanAudioFile(const std::filesystem::path& file, bool forceScan, ScanStats& stats);
void notifyInProgressIfNeeded(const ScanStepStats& stats);
void notifyInProgress(const ScanStepStats& stats);
void reloadSimilarityEngine(ScanStats& stats);
Recommendation::IRecommendationService& _recommendationService;
std::mutex _controlMutex;
std::atomic<bool> _abortScan {};
Wt::WIOService _ioService;
boost::asio::system_timer _scheduleTimer {_ioService};
Events _events;
std::chrono::system_clock::time_point _lastScanInProgressEmit {};
Database::Session _dbSession;
std::unique_ptr<MetaData::IParser> _metadataParser;
mutable std::shared_mutex _statusMutex;
State _curState {State::NotScheduled};
std::optional<ScanStats> _lastCompleteScanStats;
std::optional<ScanStepStats> _currentScanStepStats;
Wt::WDateTime _nextScheduledScan;
// Current scan settings
std::size_t _scanVersion {};
Wt::WTime _startTime;
Database::ScanSettings::UpdatePeriod _updatePeriod {Database::ScanSettings::UpdatePeriod::Never};
std::unordered_set<std::filesystem::path> _fileExtensions;
std::filesystem::path _mediaDirectory;
Database::ScanSettings::RecommendationEngineType _recommendationServiceType;
};
} // Scanner
@@ -17,7 +17,7 @@
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "scanner/ScannerStats.hpp"
#include "services/scanner/ScannerStats.hpp"
namespace Scanner {