WIP. Using WIOService to handle Services

This commit is contained in:
emeric
2014-08-05 13:11:31 +02:00
parent f26e6cad7c
commit 9fdc9f41dd
12 changed files with 148 additions and 65 deletions
+101 -25
View File
@@ -2,6 +2,7 @@
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
#include <boost/thread.hpp>
#include <boost/asio/placeholders.hpp>
#include "database/MediaDirectory.hpp"
#include "database/AudioTypes.hpp"
@@ -14,46 +15,114 @@ namespace DatabaseUpdater {
using namespace Database;
Updater::Updater(boost::filesystem::path dbPath, MetaData::Parser& parser)
: _db(dbPath),
_metadataParser(parser)
: _running(false),
_scheduleTimer(_ioService),
_db(dbPath),
_metadataParser(parser)
{
_ioService.setThreadCount(1);
}
void
Updater::process(void)
Updater::start(void)
{
removeMissingAudioFiles(_result.audioStats);
// TODO video files
_running = true;
// post some jobs in the io_service
processNextJob();
_ioService.start();
}
void
Updater::stop(void)
{
_running = false;
// TODO cancel all jobs (timer, ...)
_scheduleTimer.cancel();
_ioService.stop();
}
void
Updater::processNextJob(void)
{
Wt::Dbo::Transaction transaction(_db.getSession());
std::vector<MediaDirectory::pointer> mediaDirectories = MediaDirectory::getAll(_db.getSession());
MediaDirectorySettings::pointer settings = MediaDirectorySettings::get(_db.getSession());
BOOST_FOREACH( MediaDirectory::pointer directory, mediaDirectories)
if (settings->getManualScanRequested())
{
switch (directory->getType()) {
case MediaDirectory::Audio:
refreshAudioDirectory(directory->getPath(), _result.audioStats);
break;
case MediaDirectory::Video:
refreshVideoDirectory(directory->getPath());
break;
}
settings.modify()->setManualScanRequested(false);
// Schedule immediate scan
scheduleScan( boost::posix_time::seconds(0) );
}
else
{
// boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
std::cout << "Audio changes = " << _result.audioStats.nbChanges() << std::endl;
std::cout << "Video changes = " << _result.videoStats.nbChanges() << std::endl;
// TODO
}
}
Database::MediaDirectorySettings::pointer settings = Database::MediaDirectorySettings::get(_db.getSession());
boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
void
Updater::scheduleScan( boost::posix_time::time_duration duration)
{
std::cout << "Scheduling next scan in " << duration << std::endl;
_scheduleTimer.expires_from_now(duration);
_scheduleTimer.async_wait( boost::bind( &Updater::process, this, boost::asio::placeholders::error) );
}
if (_result.audioStats.nbChanges() + _result.videoStats.nbChanges() > 0)
settings.modify()->setLastUpdate(now);
void
Updater::scheduleScan( boost::posix_time::ptime time)
{
_scheduleTimer.expires_at(time);
_scheduleTimer.async_wait( boost::bind( &Updater::process, this, boost::asio::placeholders::error) );
}
settings.modify()->setLastScan(now);
void
Updater::process(boost::system::error_code err)
{
if (!err)
{
removeMissingAudioFiles(_result.audioStats);
// TODO video files
transaction.commit();
std::vector<boost::filesystem::path> pathes;
{
Wt::Dbo::Transaction transaction(_db.getSession());
std::vector<MediaDirectory::pointer> mediaDirectories = MediaDirectory::getAll(_db.getSession());
BOOST_FOREACH(MediaDirectory::pointer directory, mediaDirectories)
pathes.push_back(directory->getPath());
}
BOOST_FOREACH( boost::filesystem::path p, pathes)
refreshAudioDirectory(p, _result.audioStats);
std::cout << "Audio changes = " << _result.audioStats.nbChanges() << std::endl;
std::cout << "Video changes = " << _result.videoStats.nbChanges() << std::endl;
// Update database stats only if it has not been interrupted
if (_running)
{
boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
{
Wt::Dbo::Transaction transaction(_db.getSession());
Database::MediaDirectorySettings::pointer settings = Database::MediaDirectorySettings::get(_db.getSession());
if (_result.audioStats.nbChanges() + _result.videoStats.nbChanges() > 0)
settings.modify()->setLastUpdate(now);
settings.modify()->setLastScan(now);
}
processNextJob();
}
}
}
void
@@ -62,7 +131,7 @@ Updater::processAudioFile( const boost::filesystem::path& file, Stats& stats)
try {
// Check last update time
boost::posix_time::ptime lastWriteTime (boost::posix_time::from_time_t( boost::filesystem::last_write_time( file ) ) );
boost::posix_time::ptime lastWriteTime (boost::posix_time::from_time_t( boost::filesystem::last_write_time( file ) ) );
Wt::Dbo::Transaction transaction(_db.getSession());
@@ -232,6 +301,12 @@ Updater::processAudioFile( const boost::filesystem::path& file, Stats& stats)
void
Updater::refreshAudioDirectory( const boost::filesystem::path& p, Stats& stats)
{
if (!_running)
{
std::cerr << "Not running! Stopping scan" << std::endl;
return;
}
if (boost::filesystem::exists(p) && boost::filesystem::is_directory(p)) {
typedef std::vector<boost::filesystem::path> Paths; // store paths,
@@ -247,6 +322,7 @@ Updater::refreshAudioDirectory( const boost::filesystem::path& p, Stats& stats)
if (boost::filesystem::is_directory(file)) {
refreshAudioDirectory( file, stats );
}
else if (boost::filesystem::is_regular(file)) {
processAudioFile( file, stats );
}
+18 -2
View File
@@ -1,6 +1,8 @@
#ifndef DB_UPDATER_UPDATER_HPP
#define DB_UPDATER_UPDATER_HPP
#include <boost/asio/deadline_timer.hpp>
#include <Wt/WIOService>
#include "metadata/MetaData.hpp"
#include "database/DatabaseHandler.hpp"
@@ -15,8 +17,9 @@ class Updater
public:
Updater(boost::filesystem::path db, MetaData::Parser& parser);
// Update database
void process();
void start();
void stop();
private:
@@ -36,6 +39,14 @@ class Updater
Stats videoStats;
};
// Job handling
void processNextJob();
void scheduleScan(boost::posix_time::time_duration duration);
void scheduleScan(boost::posix_time::ptime time);
// Update database
void process(boost::system::error_code ec);
// Video
void refreshVideoDirectory( const boost::filesystem::path& directory );
void processVideoFile( const boost::filesystem::path& file);
@@ -47,6 +58,11 @@ class Updater
Database::Path::pointer getAddPath(const boost::filesystem::path& path);
bool _running;
Wt::WIOService _ioService;
boost::asio::deadline_timer _scheduleTimer;
Database::Handler _db;
MetaData::Parser& _metadataParser;