feat(#115,#116): implement cron scheduler worker and WebSocket notifications

## Issues Addressed
- #115: Cron scheduler worker
- #116: Cron WebSocket notifications

## Changes

### CronSchedulerService (cron.scheduler.ts)
- Polls CronSchedule table every minute for due schedules
- Executes commands when schedules fire (placeholder for MoltBot integration)
- Updates lastRun/nextRun fields after execution
- Handles errors gracefully with logging
- Supports manual trigger for testing
- Start/stop lifecycle management

### WebSocket Integration
- Added emitCronExecuted() method to WebSocketGateway
- Emits workspace-scoped cron:executed events
- Payload includes: scheduleId, command, executedAt

### Tests
- cron.scheduler.spec.ts: 9 passing tests
- Tests cover: status, due schedule processing, manual trigger, scheduler lifecycle

## Technical Notes
- Placeholder triggerMoltBotCommand() needs actual implementation
- Uses setInterval for polling (could upgrade to cron-parser library)
- WebSocket rooms use workspace:{id} format (existing pattern)

## Files Changed
- apps/api/src/cron/cron.scheduler.ts (new)
- apps/api/src/cron/cron.scheduler.spec.ts (new)
- apps/api/src/cron/cron.module.ts (updated)
- apps/api/src/websocket/websocket.gateway.ts (updated)
This commit is contained in:
2026-01-29 23:05:39 -06:00
parent 2e6b7d4070
commit 5048d9eb01
4 changed files with 342 additions and 4 deletions

View File

@@ -0,0 +1,127 @@
import { describe, it, expect, beforeEach, vi } from "vitest";
// Mock WebSocketGateway before importing the service
vi.mock("../websocket/websocket.gateway", () => ({
WebSocketGateway: vi.fn().mockImplementation(() => ({
emitCronExecuted: vi.fn(),
})),
}));
// Mock PrismaService
const mockPrisma = {
cronSchedule: {
findMany: vi.fn(),
findUnique: vi.fn(),
update: vi.fn(),
},
};
vi.mock("../prisma/prisma.service", () => ({
PrismaService: vi.fn().mockImplementation(() => mockPrisma),
}));
// Now import the service
import { CronSchedulerService } from "./cron.scheduler";
describe("CronSchedulerService", () => {
let service: CronSchedulerService;
beforeEach(async () => {
vi.clearAllMocks();
// Create service with mocked dependencies
service = new CronSchedulerService(
mockPrisma as any,
{ emitCronExecuted: vi.fn() } as any
);
});
it("should be defined", () => {
expect(service).toBeDefined();
});
describe("getStatus", () => {
it("should return running status", () => {
const status = service.getStatus();
expect(status).toHaveProperty("running");
expect(status).toHaveProperty("checkIntervalMs");
});
});
describe("processDueSchedules", () => {
it("should find due schedules with null nextRun", async () => {
const now = new Date();
mockPrisma.cronSchedule.findMany.mockResolvedValue([]);
await service.processDueSchedules();
expect(mockPrisma.cronSchedule.findMany).toHaveBeenCalledWith({
where: {
enabled: true,
OR: [{ nextRun: null }, { nextRun: { lte: now } }],
},
});
});
it("should return empty array when no schedules are due", async () => {
mockPrisma.cronSchedule.findMany.mockResolvedValue([]);
const result = await service.processDueSchedules();
expect(result).toEqual([]);
});
it("should handle errors gracefully", async () => {
mockPrisma.cronSchedule.findMany.mockRejectedValue(new Error("DB error"));
const result = await service.processDueSchedules();
expect(result).toEqual([]);
});
});
describe("triggerManual", () => {
it("should return null for non-existent schedule", async () => {
mockPrisma.cronSchedule.findUnique.mockResolvedValue(null);
const result = await service.triggerManual("cron-999");
expect(result).toBeNull();
});
it("should return null for disabled schedule", async () => {
mockPrisma.cronSchedule.findUnique.mockResolvedValue({
id: "cron-1",
enabled: false,
command: "test",
workspaceId: "ws-123",
});
const result = await service.triggerManual("cron-1");
expect(result).toBeNull();
});
});
describe("startScheduler / stopScheduler", () => {
it("should start and stop the scheduler", () => {
expect(service.getStatus().running).toBe(false);
service.startScheduler();
expect(service.getStatus().running).toBe(true);
service.stopScheduler();
expect(service.getStatus().running).toBe(false);
});
it("should not start multiple schedulers", () => {
service.startScheduler();
const firstInterval = service.getStatus().checkIntervalMs;
service.startScheduler();
expect(service.getStatus().checkIntervalMs).toBe(firstInterval);
service.stopScheduler();
});
});
});