Skip to content

Commit

Permalink
FileSorted: Added dir watching and periodic scans of the 'inbox' path
Browse files Browse the repository at this point in the history
  • Loading branch information
LiberatorUSA committed Aug 10, 2024
1 parent 9181ba6 commit 993d0ff
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 2 deletions.
3 changes: 2 additions & 1 deletion tools/FileSorter/FileSorter.ini
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,10 @@

[FileSorter\FileSorterConfig]
"vfsInboxPath"="SortPath/autosort"
"vfsInboxPathScanIntervalInMs"=300000
"vfsSortSourceRootPath"="SortPath/unsorted"
"vfsSortedTargetRootPath"="SortPath/sorted"
"dirStructureUtcOffsetInMins"="-360"
"dirStructureUtcOffsetInMins"=-360
"useDateTimeFolderStructure"="true"
"yearFolderDecoration"="-== {year} ==-"
"useMonthFolder"="false"
Expand Down
3 changes: 2 additions & 1 deletion tools/FileSorter/FileSorter_d.ini
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,10 @@

[FileSorter\FileSorterConfig]
"vfsInboxPath"="SortPath/autosort"
"vfsInboxPathScanIntervalInMs"=300000
"vfsSortSourceRootPath"="SortPath/unsorted"
"vfsSortedTargetRootPath"="SortPath/sorted"
"dirStructureUtcOffsetInMins"="-360"
"dirStructureUtcOffsetInMins"=-360
"useDateTimeFolderStructure"="true"
"yearFolderDecoration"="-== {year} ==-"
"useMonthFolder"="false"
Expand Down
10 changes: 10 additions & 0 deletions tools/FileSorter/include/FileSorter.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ class FileSorterConfig : public CORE::CGloballyConfigurable

FileTypeConfig fileTypeConfig;
CORE::CString vfsInboxPath;
CORE::UInt32 vfsInboxPathScanIntervalInMs;
CORE::CString vfsSortSourceRootPath;
CORE::CString vfsSortedTargetRootPath;
bool useDateTimeFolderStructure;
Expand Down Expand Up @@ -275,12 +276,21 @@ class FileSorter : public CORE::CTSGNotifier
void OnVfsInitializationCompleted( CORE::CNotifier* notifier ,
const CORE::CEvent& eventId ,
CORE::CICloneable* eventData );

void OnVfsWatchedInboxDirChange( CORE::CNotifier* notifier ,
const CORE::CEvent& eventId ,
CORE::CICloneable* eventData );

void OnInboxWatchTimerCycle( CORE::CNotifier* notifier ,
const CORE::CEvent& eventId ,
CORE::CICloneable* eventData );

private:

WEB::CHTTPServer m_httpServer;
WEB::CDefaultHTTPServerRouter m_httpRouter;
WEB::CTaskManagerServerResource m_taskManagementRsc;
CORE::CTimer m_inboxWatchTimer;
CORE::CDataNode m_globalConfig;
CORE::CValueList m_appArgs;
FileSorterConfig m_appConfig;
Expand Down
69 changes: 69 additions & 0 deletions tools/FileSorter/src/FileSorter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ const CORE::CString FileSorterConfig::ClassTypeName = "FileSorterConfig";
FileSorterConfig::FileSorterConfig( void )
: CORE::CGloballyConfigurable()
, vfsInboxPath()
, vfsInboxPathScanIntervalInMs( 5 * 60 * 1000 ) // default is 5 minutes
, vfsSortSourceRootPath()
, vfsSortedTargetRootPath()
, useDateTimeFolderStructure( true )
Expand Down Expand Up @@ -431,6 +432,7 @@ FileSorterConfig::LoadConfig( const CORE::CDataNode& globalConfig )
return false;

vfsInboxPath = cfg->GetAttributeValueOrChildValueByName( "vfsInboxPath", vfsInboxPath ).AsString( vfsInboxPath, true );
vfsInboxPathScanIntervalInMs = cfg->GetAttributeValueOrChildValueByName( "vfsInboxPathScanIntervalInMs", vfsInboxPathScanIntervalInMs ).AsUInt32( vfsInboxPathScanIntervalInMs, true );
vfsSortSourceRootPath = cfg->GetAttributeValueOrChildValueByName( "vfsSortSourceRootPath", vfsSortSourceRootPath ).AsString( vfsSortSourceRootPath, true );
vfsSortedTargetRootPath = cfg->GetAttributeValueOrChildValueByName( "vfsSortedTargetRootPath", vfsSortedTargetRootPath ).AsString( vfsSortedTargetRootPath, true );
useDateTimeFolderStructure = cfg->GetAttributeValueOrChildValueByName( "useDateTimeFolderStructure", useDateTimeFolderStructure ).AsBool( useDateTimeFolderStructure, true );
Expand Down Expand Up @@ -585,6 +587,8 @@ FileSorter::SortFilesInVfsPath( const CORE::CString& vfsRootPath )
return true;

VFS::CVFS& vfs = VFS::CVfsGlobal::Instance()->GetVfs();
if ( !vfs.IsInitialized() )
return false;

CORE::CString::StringVector filesToSort;
if ( vfs.GetFileList( filesToSort, vfsRootPath, true, true, CORE::CString::Empty ) )
Expand Down Expand Up @@ -620,7 +624,12 @@ FileSorter::OnAppStarted( CORE::CNotifier* notifier ,

VFS::CVFS& vfs = VFS::CVfsGlobal::Instance()->GetVfs();
if ( vfs.IsInitialized() )
{
SortInitialRootPaths();

m_inboxWatchTimer.SetInterval( m_appConfig.vfsInboxPathScanIntervalInMs );
m_inboxWatchTimer.SetEnabled( true );
}
}

/*-------------------------------------------------------------------------*/
Expand All @@ -632,6 +641,49 @@ FileSorter::OnVfsInitializationCompleted( CORE::CNotifier* notifier ,
{GUCEF_TRACE;

SortInitialRootPaths();

VFS::CVFS& vfs = VFS::CVfsGlobal::Instance()->GetVfs();

if ( vfs.AddDirToWatch(m_appConfig.vfsInboxPath, true ) )
{
GUCEF_LOG( CORE::LOGLEVEL_NORMAL, "FileSorter: Added VFS path \"" + m_appConfig.vfsInboxPath + "\" to watch list" );
}
else
{
GUCEF_ERROR_LOG( CORE::LOGLEVEL_IMPORTANT, "FileSorter: Failed to add VFS path \"" + m_appConfig.vfsInboxPath + "\" to watch list" );
}

m_inboxWatchTimer.SetInterval( m_appConfig.vfsInboxPathScanIntervalInMs );
m_inboxWatchTimer.SetEnabled( true );
}

/*-------------------------------------------------------------------------*/

void
FileSorter::OnVfsWatchedInboxDirChange( CORE::CNotifier* notifier ,
const CORE::CEvent& eventId ,
CORE::CICloneable* eventData )
{GUCEF_TRACE;

// we dont even care what the change is or to what file, we just sort everything in the inbox
GUCEF_LOG( CORE::LOGLEVEL_NORMAL, "FileSorter: Detected changes in 'inbox' VFS path \"" + m_appConfig.vfsInboxPath + "\"" );
SortFilesInVfsPath( m_appConfig.vfsInboxPath );
}

/*-------------------------------------------------------------------------*/

void
FileSorter::OnInboxWatchTimerCycle( CORE::CNotifier* notifier ,
const CORE::CEvent& eventId ,
CORE::CICloneable* eventData )
{GUCEF_TRACE;

m_inboxWatchTimer.SetEnabled( false ); // stop the timer while we process just in case it takes a while

GUCEF_LOG( CORE::LOGLEVEL_NORMAL, "FileSorter: Performing periodic scan of VFS path \"" + m_appConfig.vfsInboxPath + "\"" );
SortFilesInVfsPath( m_appConfig.vfsInboxPath );

m_inboxWatchTimer.SetEnabled( true );
}

/*-------------------------------------------------------------------------*/
Expand All @@ -652,7 +704,23 @@ FileSorter::RegisterEventHandlers( void )
SubscribeTo( &vfs ,
VFS::CVFS::VfsInitializationCompletedEvent ,
callback2 );
TEventCallback callback3( this, &FileSorter::OnVfsWatchedInboxDirChange );
SubscribeTo( &vfs ,
VFS::CVFS::FileCreatedEvent ,
callback3 );
TEventCallback callback4( this, &FileSorter::OnVfsWatchedInboxDirChange );
SubscribeTo( &vfs ,
VFS::CVFS::FileModifiedEvent ,
callback4 );
TEventCallback callback5( this, &FileSorter::OnVfsWatchedInboxDirChange );
SubscribeTo( &vfs ,
VFS::CVFS::FileRenamedEvent ,
callback5 );

TEventCallback callback6( this, &FileSorter::OnInboxWatchTimerCycle );
SubscribeTo( &m_inboxWatchTimer ,
CORE::CTimer::TimerUpdateEvent ,
callback6 );
}

/*-------------------------------------------------------------------------*/
Expand All @@ -662,6 +730,7 @@ FileSorter::FileSorter( void )
, m_httpServer()
, m_httpRouter()
, m_taskManagementRsc()
, m_inboxWatchTimer()
, m_globalConfig()
, m_appArgs()
, m_appConfig()
Expand Down

0 comments on commit 993d0ff

Please sign in to comment.