@pulzar/core
Version:
Next-generation Node.js framework for ultra-fast web applications with zero-reflection DI, GraphQL, WebSockets, events, and edge runtime support
229 lines • 6.33 kB
JavaScript
export class MemoryTaskAdapter {
tasks = new Map();
stats = {
total: 0,
pending: 0,
running: 0,
completed: 0,
failed: 0,
cancelled: 0,
averageDuration: 0,
throughput: 0,
errorRate: 0,
};
/**
* Add a new task
*/
async add(task) {
this.tasks.set(task.id, task);
this.stats.total++;
this.stats.pending++;
this.updateStats();
}
/**
* Get a task by ID
*/
async get(id) {
return this.tasks.get(id) || null;
}
/**
* List all tasks with optional filter
*/
async list(filter) {
let tasks = Array.from(this.tasks.values());
if (filter) {
tasks = tasks.filter((task) => {
if (filter.status && !filter.status.includes(task.status)) {
return false;
}
if (filter.name && !filter.name.includes(task.name)) {
return false;
}
if (filter.createdAfter && task.createdAt < filter.createdAfter) {
return false;
}
if (filter.createdBefore && task.createdAt > filter.createdBefore) {
return false;
}
return true;
});
}
return tasks;
}
/**
* Update a task
*/
async update(id, updates) {
const task = this.tasks.get(id);
if (task) {
const oldStatus = task.status;
Object.assign(task, updates);
// Update stats based on status change
if (updates.status && updates.status !== oldStatus) {
this.stats[oldStatus]--;
this.stats[updates.status]++;
}
this.updateStats();
}
}
/**
* Remove a task
*/
async remove(id) {
const task = this.tasks.get(id);
if (task) {
this.stats[task.status]--;
this.stats.total--;
this.tasks.delete(id);
this.updateStats();
}
}
/**
* Get tasks that are due to run
*/
async getDueTasks() {
const now = new Date();
return Array.from(this.tasks.values()).filter((task) => task.status === "pending" && task.scheduledFor <= now);
}
/**
* Mark a task as completed
*/
async complete(id, result) {
const task = this.tasks.get(id);
if (task) {
const oldStatus = task.status;
task.status = "completed";
task.result = result.result;
task.duration = result.duration;
task.completedAt = result.completedAt || new Date();
task.attempts = result.attempts || task.attempts;
this.stats[oldStatus]--;
this.stats.completed++;
this.updateStats();
}
}
/**
* Mark a task as failed
*/
async fail(id, error) {
const task = this.tasks.get(id);
if (task) {
const oldStatus = task.status;
task.status = "failed";
task.error = error.error;
task.attempts = error.attempts;
task.failedAt = error.failedAt;
this.stats[oldStatus]--;
this.stats.failed++;
this.updateStats();
}
}
/**
* Get task statistics
*/
async getStats() {
return { ...this.stats };
}
/**
* Start the adapter
*/
async start() {
// Nothing to do for memory adapter
}
/**
* Stop the adapter
*/
async stop() {
// Nothing to do for memory adapter
}
/**
* Clear all tasks
*/
async clear() {
this.tasks.clear();
this.stats = {
total: 0,
pending: 0,
running: 0,
completed: 0,
failed: 0,
cancelled: 0,
averageDuration: 0,
throughput: 0,
errorRate: 0,
};
}
/**
* Update statistics
*/
updateStats() {
const tasks = Array.from(this.tasks.values());
// Calculate average duration
const completedTasks = tasks.filter((t) => t.status === "completed" && t.duration);
if (completedTasks.length > 0) {
const totalDuration = completedTasks.reduce((sum, task) => sum + (task.duration || 0), 0);
this.stats.averageDuration = totalDuration / completedTasks.length;
}
// Calculate throughput (tasks per minute)
const now = Date.now();
const oneMinuteAgo = new Date(now - 60000);
const recentTasks = tasks.filter((t) => t.completedAt && t.completedAt > oneMinuteAgo);
this.stats.throughput = recentTasks.length;
// Calculate error rate
if (this.stats.total > 0) {
this.stats.errorRate = (this.stats.failed / this.stats.total) * 100;
}
}
/**
* Get tasks by status
*/
getTasksByStatus(status) {
return Array.from(this.tasks.values()).filter((task) => task.status === status);
}
/**
* Get tasks by name
*/
getTasksByName(name) {
return Array.from(this.tasks.values()).filter((task) => task.name === name);
}
/**
* Get tasks created in a time range
*/
getTasksInTimeRange(start, end) {
return Array.from(this.tasks.values()).filter((task) => task.createdAt >= start && task.createdAt <= end);
}
/**
* Get overdue tasks
*/
getOverdueTasks() {
const now = new Date();
return Array.from(this.tasks.values()).filter((task) => task.status === "pending" && task.scheduledFor < now);
}
/**
* Get running tasks
*/
getRunningTasks() {
return this.getTasksByStatus("running");
}
/**
* Get completed tasks
*/
getCompletedTasks() {
return this.getTasksByStatus("completed");
}
/**
* Get failed tasks
*/
getFailedTasks() {
return this.getTasksByStatus("failed");
}
/**
* Reset statistics
*/
resetStats() {
this.updateStats();
}
}
// Export as default for compatibility
export default MemoryTaskAdapter;
//# sourceMappingURL=memory.js.map