@casoon/auditmysite
Version:
Professional website analysis suite with robust accessibility testing, Core Web Vitals performance monitoring, SEO analysis, and content optimization insights. Features isolated browser contexts, retry mechanisms, and comprehensive API endpoints for profe
201 lines • 7.36 kB
JavaScript
;
/**
* 💾 Persistent Queue Adapter
*
* Queue adapter that provides persistence functionality for queue state.
* Can save and restore queue items across application restarts.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PersistentQueueAdapter = void 0;
const parallel_queue_adapter_1 = require("./parallel-queue-adapter");
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
class PersistentQueueAdapter extends parallel_queue_adapter_1.ParallelQueueAdapter {
constructor(config = {}, callbacks) {
super(config, callbacks);
this.persistencePath = config.persistencePath || path_1.default.join(process.cwd(), '.auditmysite-queue-state.json');
this.enableAutoSave = config.enableAutoSave !== false;
this.autoSaveInterval = config.autoSaveInterval || 30000; // 30 seconds
this.initializeAutoSave();
}
/**
* Initialize auto-save functionality
*/
initializeAutoSave() {
if (this.enableAutoSave) {
this.autoSaveTimer = setInterval(() => {
this.saveState().catch(error => {
console.warn('Auto-save failed:', error);
});
}, this.autoSaveInterval);
}
}
/**
* Save current queue state to disk
*/
async saveState() {
try {
const state = {
timestamp: new Date().toISOString(),
config: {
maxConcurrent: this.config.maxConcurrent,
maxRetries: this.config.maxRetries,
retryDelay: this.config.retryDelay,
timeout: this.config.timeout
},
items: Array.from(this.items.values()).map(item => ({
id: item.id,
data: item.data,
priority: item.priority,
status: item.status,
attempts: item.attempts,
error: item.error,
timestamp: item.timestamp.toISOString(),
startedAt: item.startedAt?.toISOString(),
completedAt: item.completedAt?.toISOString()
})),
statistics: this.getStatistics()
};
await fs_1.promises.writeFile(this.persistencePath, JSON.stringify(state, null, 2), 'utf-8');
// State saved successfully
}
catch (error) {
console.error('Failed to save queue state:', error);
throw new Error(`Queue state persistence failed: ${error.message}`);
}
}
/**
* Load queue state from disk
*/
async loadState() {
try {
const stateData = await fs_1.promises.readFile(this.persistencePath, 'utf-8');
const state = JSON.parse(stateData);
// Restore configuration
if (state.config) {
this.configure({
maxConcurrent: state.config.maxConcurrent,
maxRetries: state.config.maxRetries,
retryDelay: state.config.retryDelay,
timeout: state.config.timeout
});
}
// Restore queue items
if (state.items && Array.isArray(state.items)) {
this.items.clear();
for (const itemData of state.items) {
const item = {
id: itemData.id,
data: itemData.data,
priority: itemData.priority || 5,
status: itemData.status || 'pending',
attempts: itemData.attempts || 0,
maxAttempts: this.config.maxRetries || 3,
error: itemData.error,
timestamp: new Date(itemData.timestamp || new Date().toISOString()),
startedAt: itemData.startedAt ? new Date(itemData.startedAt) : undefined,
completedAt: itemData.completedAt ? new Date(itemData.completedAt) : undefined
};
this.items.set(item.id, item);
}
}
console.log(`📦 Restored ${this.items.size} queue items from persistence`);
// State loaded successfully
}
catch (error) {
if (error.code === 'ENOENT') {
// File doesn't exist, start with empty state
console.log('💾 No persistent state found, starting with empty queue');
return;
}
console.error('Failed to load queue state:', error);
throw new Error(`Queue state loading failed: ${error.message}`);
}
}
/**
* Clear persistent state file
*/
async clearPersistedState() {
try {
await fs_1.promises.unlink(this.persistencePath);
console.log('🗑️ Persistent state cleared');
// State cleared successfully
}
catch (error) {
if (error.code !== 'ENOENT') {
console.warn('Failed to clear persistent state:', error);
}
}
}
/**
* Process queue with persistence
*/
async process(processor) {
// Load state before processing
await this.loadState();
// Save state before starting processing
await this.saveState();
// Run the actual processing
const result = await super.process(processor);
// Save final state
await this.saveState();
return result;
}
/**
* Add items with persistence
*/
enqueue(data, options) {
const ids = super.enqueue(data, options);
// Trigger auto-save after adding items
if (this.enableAutoSave) {
this.saveState().catch(error => {
console.warn('Failed to save state after enqueue:', error);
});
}
return ids;
}
/**
* Clear queue with persistence
*/
clear() {
super.clear();
// Clear persistent state
this.clearPersistedState().catch(error => {
console.warn('Failed to clear persistent state:', error);
});
}
/**
* Get persistence status
*/
getPersistenceInfo() {
return {
path: this.persistencePath,
autoSave: this.enableAutoSave,
lastSave: undefined // Could track this if needed
};
}
/**
* Cleanup with persistence
*/
async cleanup() {
// Stop auto-save
if (this.autoSaveTimer) {
clearInterval(this.autoSaveTimer);
this.autoSaveTimer = undefined;
}
// Final save before cleanup
if (this.items.size > 0) {
await this.saveState();
}
else {
// Clear persistent state if queue is empty
await this.clearPersistedState();
}
console.log('💾 PersistentQueueAdapter cleanup completed');
}
}
exports.PersistentQueueAdapter = PersistentQueueAdapter;
//# sourceMappingURL=persistent-queue-adapter.js.map