feat: implement orchestrator module with feed source management

Add FeedOrchestrator that coordinates fetch→parse→dedup→store pipeline:
- FeedSource type for managing RSS/Atom feed configurations
- Feed source CRUD operations in IStorage interface
- Database schema migration for feed_sources table
- Exponential backoff retry with configurable delays
- Per-feed poll intervals with health tracking
- Concurrency-limited parallel feed processing
- ProcessResult and FeedHealth interfaces for status monitoring

Files added:
- orchestrator/orchestrator.ts - main orchestrator class
- orchestrator/scheduler.ts - backoff calculation utilities
- orchestrator/index.ts - module exports
- orchestrator/orchestrator.test.ts - comprehensive test suite

Files modified:
- interfaces/feed.types.ts - add FeedSource type
- interfaces/storage.interface.ts - extend with feed source methods
- infrastructure/db/database.ts - add FeedSourceTable interface
- infrastructure/db/schema.ts - add feed_sources table migration
- modules/storage/storage.ts - implement feed source CRUD
- modules/storage/storage.test.ts - add feed source tests
This commit is contained in:
Edo Limburg
2026-05-05 22:17:16 +02:00
parent 40ccbbad1a
commit 78a2b27f6d
15 changed files with 1610 additions and 4 deletions
+173 -1
View File
@@ -4,7 +4,7 @@ import { Kysely, SqliteDialect } from 'kysely';
import { SqlStorage } from './storage.js';
import { migrate, reset } from '../../infrastructure/db/schema.js';
import type { Database } from '../../infrastructure/db/database.js';
import type { FeedItem } from '../../interfaces/feed.types.js';
import type { FeedItem, FeedSource } from '../../interfaces/feed.types.js';
describe('SqlStorage', () => {
let sqliteDb: BetterSqlite3.Database;
@@ -427,4 +427,176 @@ describe('SqlStorage', () => {
expect(recent[0].content).toBeUndefined();
});
});
describe('Feed Source Management', () => {
function createTestFeedSource(overrides: Partial<FeedSource> = {}): FeedSource {
const now = new Date();
return {
id: 'test-source-1',
url: 'https://example.com/feed.xml',
name: 'Test Feed',
format: 'rss',
pollIntervalMs: 60000,
isActive: true,
lastFetchedAt: null,
lastSuccessAt: null,
consecutiveFailures: 0,
createdAt: now,
updatedAt: now,
...overrides,
};
}
describe('saveFeedSource', () => {
it('saves a new feed source', async () => {
const source = createTestFeedSource();
await storage.saveFeedSource(source);
const retrieved = await storage.getFeedSourceById(source.id);
expect(retrieved).toBeTruthy();
expect(retrieved!.id).toBe(source.id);
expect(retrieved!.url).toBe(source.url);
expect(retrieved!.name).toBe(source.name);
expect(retrieved!.format).toBe(source.format);
expect(retrieved!.pollIntervalMs).toBe(source.pollIntervalMs);
expect(retrieved!.isActive).toBe(source.isActive);
});
it('updates existing feed source on duplicate id (upsert)', async () => {
const source = createTestFeedSource({ id: 'same-id', name: 'Original Name' });
await storage.saveFeedSource(source);
const updated = createTestFeedSource({
id: 'same-id',
name: 'Updated Name',
pollIntervalMs: 120000,
});
await storage.saveFeedSource(updated);
const retrieved = await storage.getFeedSourceById('same-id');
expect(retrieved!.name).toBe('Updated Name');
expect(retrieved!.pollIntervalMs).toBe(120000);
});
});
describe('getFeedSources', () => {
it('returns all feed sources', async () => {
const source1 = createTestFeedSource({ id: 'source-1', url: 'https://example1.com/feed.xml' });
const source2 = createTestFeedSource({ id: 'source-2', url: 'https://example2.com/feed.xml' });
await storage.saveFeedSource(source1);
await storage.saveFeedSource(source2);
const sources = await storage.getFeedSources();
expect(sources).toHaveLength(2);
expect(sources.map(s => s.id)).toContain('source-1');
expect(sources.map(s => s.id)).toContain('source-2');
});
it('returns only active sources when activeOnly is true', async () => {
const active = createTestFeedSource({ id: 'active', url: 'https://active.com/feed.xml', isActive: true });
const inactive = createTestFeedSource({ id: 'inactive', url: 'https://inactive.com/feed.xml', isActive: false });
await storage.saveFeedSource(active);
await storage.saveFeedSource(inactive);
const sources = await storage.getFeedSources(true);
expect(sources).toHaveLength(1);
expect(sources[0]!.id).toBe('active');
});
});
describe('getFeedSourceById', () => {
it('returns feed source by id', async () => {
const source = createTestFeedSource({ id: 'specific-id' });
await storage.saveFeedSource(source);
const retrieved = await storage.getFeedSourceById('specific-id');
expect(retrieved).toBeTruthy();
expect(retrieved!.id).toBe('specific-id');
});
it('returns null for non-existent id', async () => {
const retrieved = await storage.getFeedSourceById('non-existent');
expect(retrieved).toBeNull();
});
});
describe('updateFeedSourceStatus', () => {
it('updates lastFetchedAt', async () => {
const source = createTestFeedSource();
await storage.saveFeedSource(source);
const fetchTime = new Date('2024-09-06T10:00:00Z');
await storage.updateFeedSourceStatus(source.id, { lastFetchedAt: fetchTime });
const retrieved = await storage.getFeedSourceById(source.id);
expect(retrieved!.lastFetchedAt).toEqual(fetchTime);
});
it('updates lastSuccessAt', async () => {
const source = createTestFeedSource();
await storage.saveFeedSource(source);
const successTime = new Date('2024-09-06T10:00:00Z');
await storage.updateFeedSourceStatus(source.id, { lastSuccessAt: successTime });
const retrieved = await storage.getFeedSourceById(source.id);
expect(retrieved!.lastSuccessAt).toEqual(successTime);
});
it('updates consecutiveFailures', async () => {
const source = createTestFeedSource();
await storage.saveFeedSource(source);
await storage.updateFeedSourceStatus(source.id, { consecutiveFailures: 5 });
const retrieved = await storage.getFeedSourceById(source.id);
expect(retrieved!.consecutiveFailures).toBe(5);
});
it('updates isActive', async () => {
const source = createTestFeedSource({ isActive: true });
await storage.saveFeedSource(source);
await storage.updateFeedSourceStatus(source.id, { isActive: false });
const retrieved = await storage.getFeedSourceById(source.id);
expect(retrieved!.isActive).toBe(false);
});
it('updates multiple fields at once', async () => {
const source = createTestFeedSource();
await storage.saveFeedSource(source);
const now = new Date();
await storage.updateFeedSourceStatus(source.id, {
lastFetchedAt: now,
lastSuccessAt: now,
consecutiveFailures: 0,
});
const retrieved = await storage.getFeedSourceById(source.id);
expect(retrieved!.lastFetchedAt).toEqual(now);
expect(retrieved!.lastSuccessAt).toEqual(now);
expect(retrieved!.consecutiveFailures).toBe(0);
});
});
describe('deleteFeedSource', () => {
it('deletes feed source by id', async () => {
const source = createTestFeedSource({ id: 'to-delete' });
await storage.saveFeedSource(source);
await storage.deleteFeedSource('to-delete');
const retrieved = await storage.getFeedSourceById('to-delete');
expect(retrieved).toBeNull();
});
it('does not throw when deleting non-existent id', async () => {
await expect(storage.deleteFeedSource('non-existent')).resolves.not.toThrow();
});
});
});
});
+113 -2
View File
@@ -4,9 +4,9 @@
*/
import type { Kysely } from 'kysely';
import type { FeedItem } from '../../interfaces/feed.types.js';
import type { FeedItem, FeedSource } from '../../interfaces/feed.types.js';
import type { IStorage } from '../../interfaces/storage.interface.js';
import type { Database, FeedItemTable } from '../../infrastructure/db/database.js';
import type { Database, FeedItemTable, FeedSourceTable } from '../../infrastructure/db/database.js';
export class SqlStorage implements IStorage {
private readonly db: Kysely<Database>;
@@ -98,4 +98,115 @@ export class SqlStorage implements IStorage {
summary: row.summary ?? undefined,
};
}
// Feed source management methods
async getFeedSources(activeOnly?: boolean): Promise<FeedSource[]> {
let query = this.db.selectFrom('feed_sources').selectAll();
if (activeOnly) {
query = query.where('is_active', '=', 1); // Use 1 for SQLite boolean
}
const rows = await query.orderBy('created_at', 'asc').execute();
return rows.map(this.rowToFeedSource);
}
async getFeedSourceById(id: string): Promise<FeedSource | null> {
const row = await this.db
.selectFrom('feed_sources')
.selectAll()
.where('id', '=', id)
.executeTakeFirst();
return row ? this.rowToFeedSource(row) : null;
}
async saveFeedSource(source: FeedSource): Promise<void> {
const now = new Date().toISOString();
const row: FeedSourceTable = {
id: source.id,
url: source.url,
name: source.name,
format: source.format,
poll_interval_ms: source.pollIntervalMs,
is_active: source.isActive ? 1 : 0, // Convert boolean to integer for SQLite
last_fetched_at: source.lastFetchedAt?.toISOString() ?? null,
last_success_at: source.lastSuccessAt?.toISOString() ?? null,
consecutive_failures: source.consecutiveFailures,
created_at: source.createdAt.toISOString(),
updated_at: now,
};
// Upsert: insert or update on conflict
await this.db
.insertInto('feed_sources')
.values(row)
.onConflict((oc) =>
oc.column('id').doUpdateSet({
name: (eb) => eb.ref('excluded.name'),
format: (eb) => eb.ref('excluded.format'),
poll_interval_ms: (eb) => eb.ref('excluded.poll_interval_ms'),
is_active: (eb) => eb.ref('excluded.is_active'),
last_fetched_at: (eb) => eb.ref('excluded.last_fetched_at'),
last_success_at: (eb) => eb.ref('excluded.last_success_at'),
consecutive_failures: (eb) => eb.ref('excluded.consecutive_failures'),
updated_at: (eb) => eb.ref('excluded.updated_at'),
})
)
.execute();
}
async updateFeedSourceStatus(
id: string,
updates: {
lastFetchedAt?: Date;
lastSuccessAt?: Date;
consecutiveFailures?: number;
isActive?: boolean;
}
): Promise<void> {
const setClause: Partial<FeedSourceTable> = {
updated_at: new Date().toISOString(),
};
if (updates.lastFetchedAt !== undefined) {
setClause.last_fetched_at = updates.lastFetchedAt.toISOString();
}
if (updates.lastSuccessAt !== undefined) {
setClause.last_success_at = updates.lastSuccessAt?.toISOString() ?? null;
}
if (updates.consecutiveFailures !== undefined) {
setClause.consecutive_failures = updates.consecutiveFailures;
}
if (updates.isActive !== undefined) {
setClause.is_active = updates.isActive ? 1 : 0; // Convert boolean to integer for SQLite
}
await this.db
.updateTable('feed_sources')
.set(setClause)
.where('id', '=', id)
.execute();
}
async deleteFeedSource(id: string): Promise<void> {
await this.db.deleteFrom('feed_sources').where('id', '=', id).execute();
}
private rowToFeedSource(row: FeedSourceTable): FeedSource {
return {
id: row.id,
url: row.url,
name: row.name,
format: row.format,
pollIntervalMs: row.poll_interval_ms,
isActive: Boolean(row.is_active), // Convert integer to boolean
lastFetchedAt: row.last_fetched_at ? new Date(row.last_fetched_at) : null,
lastSuccessAt: row.last_success_at ? new Date(row.last_success_at) : null,
consecutiveFailures: row.consecutive_failures,
createdAt: new Date(row.created_at),
updatedAt: new Date(row.updated_at),
};
}
}