overall v1

This commit is contained in:
andrei-mihnea-cerbu
2024-11-12 14:00:30 +02:00
parent a5e4fc030d
commit 5ea32b2a3a
34 changed files with 1131 additions and 781 deletions
+44 -14
View File
@@ -10,8 +10,9 @@ export class DirectoryWatcher {
private applicationInfo: JsonManager;
private memoryManager: MemoryManager;
private readonly sourceKey: string;
private directoryWatcher: FSWatcher | null; // To store the watcher reference
private totalSize: number; // To store total directory size
private directoryWatcher: FSWatcher | null;
private totalSize: number;
private isBusy: boolean;
constructor(memoryManagerPath: string, applicationInfoPath: string, sourceKey: string) {
this.sourceKey = sourceKey;
@@ -19,14 +20,28 @@ export class DirectoryWatcher {
this.memoryManager = new MemoryManager(memoryManagerPath);
this.directoryMemoryId = '';
this.directoryPath = '';
this.directoryWatcher = null; // Initialize with no watcher
this.totalSize = 0; // Initialize size with zero
this.directoryWatcher = null;
this.totalSize = 0;
this.isBusy = false;
}
// Start the watcher with a busy flag to prevent overlapping operations
async start(): Promise<void> {
setInterval(async () => {
if (!this.isBusy) {
this.isBusy = true;
const initialized = await this.initialize();
if (initialized) this.log('Directory watcher started successfully.');
this.isBusy = false;
}
}, 10000); // 10-second interval for testing
}
// Method to initialize and validate the backup directory
async initialize(): Promise<boolean> {
const directoryData = await this.applicationInfo.readValue(this.sourceKey);
if (!directoryData) {
this.log('Directory data not found in application info.', 'error');
return false;
}
@@ -34,7 +49,7 @@ export class DirectoryWatcher {
this.directoryMemoryId = directoryData.id;
if (!this.directoryMemoryId || !this.directoryPath) {
console.error('Components of entry in \'DirectoryWatcher\' not found.');
this.log('Components of entry in \'DirectoryWatcher\' not found.', 'error');
await this.applicationInfo.removeValue(this.sourceKey);
return false;
}
@@ -65,7 +80,7 @@ export class DirectoryWatcher {
for (const item of items) {
const fullPath = path.join(dirPath, item.name);
const stats = await fs.stat(fullPath); // Get stats for each item
const stats = await fs.stat(fullPath);
if (item.isDirectory()) {
// If it's a directory, recursively build its structure and accumulate size
@@ -75,7 +90,7 @@ export class DirectoryWatcher {
} else if (item.isFile()) {
// If it's a file, store its full path and accumulate size
directoryScheme[item.name] = fullPath;
totalSize += stats.size; // Add file size
totalSize += stats.size;
}
}
@@ -85,8 +100,8 @@ export class DirectoryWatcher {
// Restart the directory watcher, ensuring any previous watcher is closed
private restartWatcher(): void {
if (this.directoryWatcher) {
console.log('Stopping existing watcher...');
this.directoryWatcher.close(); // Stop the existing watcher
this.log('Stopping existing watcher...');
this.directoryWatcher.close();
}
this.startDirectoryWatcher();
@@ -100,7 +115,7 @@ export class DirectoryWatcher {
this.directoryWatcher = watch(this.directoryPath, { recursive: true }, async (eventType, filename) => {
if (filename) {
console.log(`File change detected: ${eventType} - ${filename}`);
this.log(`File change detected: ${eventType} - ${filename}`);
// Rebuild the directory scheme and update memory
const result = await this.buildDirectoryScheme(this.directoryPath);
this.directoryScheme = result.structure;
@@ -111,19 +126,34 @@ export class DirectoryWatcher {
totalSize: this.totalSize,
});
console.log('Directory structure and size updated in memory.');
this.log('Directory structure and size updated in memory.');
}
});
console.log(`Watching for changes in: ${this.directoryPath}`);
this.log(`Watching for changes in: ${this.directoryPath}`);
}
// Close the directory watcher
public closeWatcher(): void {
if (this.directoryWatcher) {
console.log(`Stopping watcher for ${this.directoryPath}`);
this.log(`Stopping watcher for ${this.directoryPath}`);
this.directoryWatcher.close();
this.directoryWatcher = null; // Clear the reference after closing
this.directoryWatcher = null;
}
}
// Unified logging function
private log(message: string, level: 'log' | 'error' = 'log'): void {
const sourcePrefix = `[DirectoryWatcher] {${this.capitalize(this.sourceKey)}}`;
if (level === 'error') {
console.error(`${sourcePrefix} ${message}`);
} else {
console.log(`${sourcePrefix} ${message}`);
}
}
// Capitalize the first letter of the sourceKey
private capitalize(str: string): string {
return str.charAt(0).toUpperCase() + str.slice(1);
}
}