@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
163 lines • 5.62 kB
JavaScript
"use strict";
/**
* 🔧 Simple Queue Adapter
*
* Adapter for basic sequential queue processing.
* Wraps the existing SimpleQueue implementation.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.SimpleQueueAdapter = void 0;
const queue_adapter_1 = require("../queue-adapter");
class SimpleQueueAdapter extends queue_adapter_1.QueueAdapter {
constructor(config, callbacks) {
super(config, callbacks);
this.processingQueue = [];
this.isPaused = false;
}
enqueue(data, options) {
const ids = [];
for (const item of data) {
const queueItem = this.createQueueItem(item, options?.priority);
this.processingQueue.push(queueItem);
ids.push(queueItem.id);
}
// Sort by priority (highest first) and timestamp (oldest first)
this.processingQueue.sort((a, b) => {
if (a.priority !== b.priority) {
return b.priority - a.priority;
}
return a.timestamp.getTime() - b.timestamp.getTime();
});
return ids;
}
async process(processor) {
if (this.isProcessing) {
throw new Error('Queue is already processing');
}
this.isProcessing = true;
this.startTime = new Date();
const completed = [];
const failed = [];
try {
while (this.processingQueue.length > 0 && !this.isPaused) {
const item = this.processingQueue.shift();
// Update item status to processing
this.updateItemStatus(item.id, 'processing');
let attempts = 0;
let success = false;
while (attempts < item.maxAttempts && !success && !this.isPaused) {
attempts++;
item.attempts = attempts;
try {
// Process the item
const result = await Promise.race([
processor(item.data),
this.createTimeoutPromise()
]);
// Success
this.updateItemStatus(item.id, 'completed', { result });
completed.push(item);
success = true;
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
if (attempts < item.maxAttempts) {
// Retry after delay
this.updateItemStatus(item.id, 'retrying', { error: errorMessage });
await this.delay(this.config.retryDelay || 1000);
}
else {
// Max attempts reached
this.updateItemStatus(item.id, 'failed', { error: errorMessage });
failed.push(item);
}
}
}
// Report progress
if (this.config.enableProgressReporting) {
this.callbacks?.onProgressUpdate?.(this.getStatistics());
}
}
// Queue empty or paused
if (this.processingQueue.length === 0) {
this.callbacks?.onQueueEmpty?.();
}
}
catch (error) {
this.callbacks?.onError?.(error instanceof Error ? error.message : String(error));
}
finally {
this.isProcessing = false;
this.endTime = new Date();
}
const statistics = this.getStatistics();
const duration = this.endTime && this.startTime
? this.endTime.getTime() - this.startTime.getTime()
: 0;
return {
completed,
failed,
statistics,
duration
};
}
getStatistics() {
const baseStats = this.getBaseStatistics();
// Add simple queue specific stats
return {
...baseStats,
pending: this.processingQueue.filter(item => item.status === 'pending').length,
processing: this.processingQueue.filter(item => item.status === 'processing').length
};
}
pause() {
this.isPaused = true;
}
resume() {
this.isPaused = false;
}
clear() {
this.processingQueue = [];
this.items.clear();
this.isPaused = false;
this.isProcessing = false;
this.startTime = undefined;
this.endTime = undefined;
}
/**
* Get pending items count
*/
getPendingCount() {
return this.processingQueue.filter(item => item.status === 'pending').length;
}
/**
* Get queue size
*/
size() {
return this.processingQueue.length;
}
/**
* Check if queue is empty
*/
isEmpty() {
return this.processingQueue.length === 0;
}
/**
* Create timeout promise for processor timeout
*/
createTimeoutPromise() {
return new Promise((_, reject) => {
setTimeout(() => {
reject(new Error(`Processing timeout after ${this.config.timeout}ms`));
}, this.config.timeout || 10000);
});
}
/**
* Simple delay utility
*/
delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
}
exports.SimpleQueueAdapter = SimpleQueueAdapter;
//# sourceMappingURL=simple-queue-adapter.js.map