fase(19): scheduling module refactor
This commit is contained in:
2
dist/api/router.js
vendored
2
dist/api/router.js
vendored
@@ -10,6 +10,7 @@ const FindingsController_1 = require("../modules/findings/infrastructure/http/Fi
|
||||
const FuzzingController_1 = require("../modules/fuzzing/infrastructure/http/FuzzingController");
|
||||
const ReportingController_1 = require("../modules/reporting/infrastructure/http/ReportingController");
|
||||
const IntegrationsController_1 = require("../modules/integrations/infrastructure/http/IntegrationsController");
|
||||
const SchedulingController_1 = require("../modules/scheduling/infrastructure/http/SchedulingController");
|
||||
const LicensingController_1 = require("../modules/licensing/infrastructure/http/LicensingController");
|
||||
const FeatureGateMiddleware_1 = require("../modules/licensing/infrastructure/middleware/FeatureGateMiddleware");
|
||||
const AuthController_1 = require("../modules/auth/infrastructure/http/AuthController");
|
||||
@@ -27,6 +28,7 @@ function createRouter(deps) {
|
||||
router.use('/fuzz', (0, FuzzingController_1.createFuzzingRouter)(deps.fuzzingDeps));
|
||||
router.use('/reports', (0, FeatureGateMiddleware_1.requireFeature)(licenseService, 'reports:basic'), (0, ReportingController_1.createReportingRouter)(deps.reportingDeps));
|
||||
router.use('/integrations', (0, FeatureGateMiddleware_1.requireFeature)(licenseService, 'integrations:webhook'), (0, IntegrationsController_1.createIntegrationsRouter)(deps.integrationsDeps));
|
||||
router.use('/schedules', (0, SchedulingController_1.createSchedulingRouter)(deps.schedulingDeps));
|
||||
// Licensing routes (public-ish — only status and activate, no sensitive data)
|
||||
const licensingController = new LicensingController_1.LicensingController(licenseService);
|
||||
router.use('/license', licensingController.router);
|
||||
|
||||
20
dist/main.js
vendored
20
dist/main.js
vendored
@@ -60,6 +60,13 @@ const OnFindingCreated_1 = require("./modules/integrations/application/event-han
|
||||
// Licensing module
|
||||
const RSALicenseValidator_1 = require("./modules/licensing/infrastructure/validators/RSALicenseValidator");
|
||||
const LicenseService_1 = require("./modules/licensing/application/LicenseService");
|
||||
// Scheduling module
|
||||
const KyselyScheduleRepository_1 = require("./modules/scheduling/infrastructure/repositories/KyselyScheduleRepository");
|
||||
const CreateScheduleCommand_1 = require("./modules/scheduling/application/commands/CreateScheduleCommand");
|
||||
const ToggleScheduleCommand_1 = require("./modules/scheduling/application/commands/ToggleScheduleCommand");
|
||||
const DeleteScheduleCommand_1 = require("./modules/scheduling/application/commands/DeleteScheduleCommand");
|
||||
const ListSchedulesQuery_1 = require("./modules/scheduling/application/queries/ListSchedulesQuery");
|
||||
const SchedulingService_1 = require("./modules/scheduling/application/SchedulingService");
|
||||
// Job queue
|
||||
const SQLiteJobQueue_1 = require("./jobs/SQLiteJobQueue");
|
||||
const ExplorationWorker_1 = require("./jobs/workers/ExplorationWorker");
|
||||
@@ -125,7 +132,7 @@ async function bootstrap() {
|
||||
// 11b. Licensing
|
||||
const licenseValidator = new RSALicenseValidator_1.RSALicenseValidator();
|
||||
const licenseService = new LicenseService_1.LicenseService(licenseValidator);
|
||||
// 11c. Integrations
|
||||
// 11c. Integrations (moved from 11d)
|
||||
const integrationRepo = new KyselyIntegrationRepository_1.KyselyIntegrationRepository(db);
|
||||
const webhookRepo = new KyselyWebhookEndpointRepository_1.KyselyWebhookEndpointRepository(db);
|
||||
const webhookDispatcher = new WebhookDispatcher_1.WebhookDispatcher(webhookRepo, logger);
|
||||
@@ -136,6 +143,14 @@ async function bootstrap() {
|
||||
jobQueue.registerHandler(ExplorationWorker_1.EXPLORATION_JOB_TYPE, (0, ExplorationWorker_1.createExplorationJobHandler)({ sessionRepo, eventBus, logger }));
|
||||
jobQueue.registerHandler(ReportWorker_1.REPORT_JOB_TYPE, (0, ReportWorker_1.createReportJobHandler)({ logger, reportRepository: reportRepo, findingRepository: findingRepo }));
|
||||
jobQueue.start();
|
||||
// 12b. Scheduling module (after job queue, since it enqueues jobs)
|
||||
const scheduleRepo = new KyselyScheduleRepository_1.KyselyScheduleRepository(db);
|
||||
const createSchedule = new CreateScheduleCommand_1.CreateScheduleCommand(scheduleRepo, eventBus);
|
||||
const toggleSchedule = new ToggleScheduleCommand_1.ToggleScheduleCommand(scheduleRepo, eventBus);
|
||||
const deleteSchedule = new DeleteScheduleCommand_1.DeleteScheduleCommand(scheduleRepo, eventBus);
|
||||
const listSchedules = new ListSchedulesQuery_1.ListSchedulesQuery(scheduleRepo);
|
||||
const schedulingService = new SchedulingService_1.SchedulingService(scheduleRepo, jobQueue, eventBus, logger);
|
||||
await schedulingService.start();
|
||||
// 13. HTTP server
|
||||
const app = (0, server_1.createServer)({
|
||||
config,
|
||||
@@ -146,6 +161,7 @@ async function bootstrap() {
|
||||
fuzzingDeps: { runFuzz, repository: fuzzRepo },
|
||||
reportingDeps: { generateReport, reportRepository: reportRepo, jobQueue },
|
||||
integrationsDeps: { integrationRepo, webhookRepo },
|
||||
schedulingDeps: { createSchedule, toggleSchedule, deleteSchedule, listSchedules, schedulingService, scheduleRepo },
|
||||
licenseService,
|
||||
authDeps: {
|
||||
registerCommand,
|
||||
@@ -183,6 +199,8 @@ async function bootstrap() {
|
||||
httpServer.close();
|
||||
// Close socket.io
|
||||
io.close();
|
||||
// Stop scheduling service
|
||||
schedulingService.stop();
|
||||
// Stop job queue and wait for active jobs
|
||||
jobQueue.pause();
|
||||
await jobQueue.waitForActive(30000);
|
||||
|
||||
114
dist/modules/scheduling/application/SchedulingService.js
vendored
Normal file
114
dist/modules/scheduling/application/SchedulingService.js
vendored
Normal file
@@ -0,0 +1,114 @@
|
||||
"use strict";
|
||||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
var desc = Object.getOwnPropertyDescriptor(m, k);
|
||||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
||||
desc = { enumerable: true, get: function() { return m[k]; } };
|
||||
}
|
||||
Object.defineProperty(o, k2, desc);
|
||||
}) : (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
o[k2] = m[k];
|
||||
}));
|
||||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
||||
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
||||
}) : function(o, v) {
|
||||
o["default"] = v;
|
||||
});
|
||||
var __importStar = (this && this.__importStar) || (function () {
|
||||
var ownKeys = function(o) {
|
||||
ownKeys = Object.getOwnPropertyNames || function (o) {
|
||||
var ar = [];
|
||||
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
||||
return ar;
|
||||
};
|
||||
return ownKeys(o);
|
||||
};
|
||||
return function (mod) {
|
||||
if (mod && mod.__esModule) return mod;
|
||||
var result = {};
|
||||
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
||||
__setModuleDefault(result, mod);
|
||||
return result;
|
||||
};
|
||||
})();
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.SchedulingService = void 0;
|
||||
/**
|
||||
* SchedulingService — manages cron jobs for scheduled explorations.
|
||||
* On startup, loads all enabled schedules and registers cron tasks.
|
||||
* When a schedule fires, it enqueues an exploration job via IJobQueue.
|
||||
*/
|
||||
const cron = __importStar(require("node-cron"));
|
||||
const ExplorationWorker_1 = require("../../../jobs/workers/ExplorationWorker");
|
||||
const ScheduleFired_1 = require("../domain/events/ScheduleFired");
|
||||
const UniqueId_1 = require("../../../shared/domain/UniqueId");
|
||||
class SchedulingService {
|
||||
constructor(scheduleRepo, jobQueue, eventBus, logger) {
|
||||
this.scheduleRepo = scheduleRepo;
|
||||
this.jobQueue = jobQueue;
|
||||
this.eventBus = eventBus;
|
||||
this.logger = logger;
|
||||
this.jobs = new Map();
|
||||
}
|
||||
async start() {
|
||||
const schedules = await this.scheduleRepo.findAll(true);
|
||||
for (const schedule of schedules) {
|
||||
this.registerCron(schedule);
|
||||
}
|
||||
this.logger.info({ count: schedules.length }, 'SchedulingService started');
|
||||
}
|
||||
stop() {
|
||||
for (const [id, task] of this.jobs) {
|
||||
task.stop();
|
||||
this.logger.debug({ scheduleId: id }, 'Cron job stopped');
|
||||
}
|
||||
this.jobs.clear();
|
||||
this.logger.info('SchedulingService stopped');
|
||||
}
|
||||
registerCron(schedule) {
|
||||
this.unregisterCron(schedule.id.toString());
|
||||
if (!schedule.enabled)
|
||||
return;
|
||||
if (!cron.validate(schedule.cronExpression.value)) {
|
||||
this.logger.warn({ scheduleId: schedule.id.toString(), cron: schedule.cronExpression.value }, 'Invalid cron, skipping');
|
||||
return;
|
||||
}
|
||||
const task = cron.schedule(schedule.cronExpression.value, () => {
|
||||
void this.fire(schedule.id.toString());
|
||||
});
|
||||
this.jobs.set(schedule.id.toString(), task);
|
||||
this.logger.info({ scheduleId: schedule.id.toString(), cron: schedule.cronExpression.value }, 'Cron job registered');
|
||||
}
|
||||
unregisterCron(scheduleId) {
|
||||
const existing = this.jobs.get(scheduleId);
|
||||
if (existing) {
|
||||
existing.stop();
|
||||
this.jobs.delete(scheduleId);
|
||||
}
|
||||
}
|
||||
async fire(scheduleId) {
|
||||
const schedule = await this.scheduleRepo.findById(UniqueId_1.UniqueId.from(scheduleId));
|
||||
if (!schedule || !schedule.enabled)
|
||||
return;
|
||||
this.logger.info({ scheduleId, url: schedule.url }, 'Firing scheduled exploration');
|
||||
const payload = {
|
||||
sessionId: UniqueId_1.UniqueId.create().toString(),
|
||||
url: schedule.url,
|
||||
seed: Math.floor(Math.random() * 0x7fffffff),
|
||||
maxStates: schedule.config['maxStates'] ?? 50,
|
||||
config: schedule.config,
|
||||
};
|
||||
try {
|
||||
const jobId = await this.jobQueue.enqueue(ExplorationWorker_1.EXPLORATION_JOB_TYPE, payload);
|
||||
schedule.markFired(Date.now());
|
||||
await this.scheduleRepo.update(schedule);
|
||||
await this.eventBus.publish(new ScheduleFired_1.ScheduleFired(scheduleId, { scheduleId, url: schedule.url, jobId }));
|
||||
this.logger.info({ scheduleId, jobId, url: schedule.url }, 'Scheduled exploration enqueued');
|
||||
}
|
||||
catch (err) {
|
||||
this.logger.error({ scheduleId, err: err instanceof Error ? err.message : String(err) }, 'Failed to enqueue scheduled exploration');
|
||||
}
|
||||
}
|
||||
}
|
||||
exports.SchedulingService = SchedulingService;
|
||||
32
dist/modules/scheduling/application/commands/CreateScheduleCommand.js
vendored
Normal file
32
dist/modules/scheduling/application/commands/CreateScheduleCommand.js
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.CreateScheduleCommand = void 0;
|
||||
const Result_1 = require("../../../../shared/domain/Result");
|
||||
const Schedule_1 = require("../../domain/entities/Schedule");
|
||||
class CreateScheduleCommand {
|
||||
constructor(scheduleRepo, eventBus) {
|
||||
this.scheduleRepo = scheduleRepo;
|
||||
this.eventBus = eventBus;
|
||||
}
|
||||
async execute(req) {
|
||||
const result = Schedule_1.Schedule.create(req);
|
||||
if (!result.ok)
|
||||
return (0, Result_1.Err)(result.error);
|
||||
const schedule = result.value;
|
||||
await this.scheduleRepo.save(schedule);
|
||||
for (const event of schedule.domainEvents) {
|
||||
await this.eventBus.publish(event);
|
||||
}
|
||||
schedule.clearEvents();
|
||||
return (0, Result_1.Ok)({
|
||||
id: schedule.id.toString(),
|
||||
name: schedule.name,
|
||||
url: schedule.url,
|
||||
cronExpression: schedule.cronExpression.value,
|
||||
enabled: schedule.enabled,
|
||||
nextRunAt: schedule.nextRunAt,
|
||||
createdAt: schedule.createdAt,
|
||||
});
|
||||
}
|
||||
}
|
||||
exports.CreateScheduleCommand = CreateScheduleCommand;
|
||||
21
dist/modules/scheduling/application/commands/DeleteScheduleCommand.js
vendored
Normal file
21
dist/modules/scheduling/application/commands/DeleteScheduleCommand.js
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.DeleteScheduleCommand = void 0;
|
||||
const Result_1 = require("../../../../shared/domain/Result");
|
||||
const UniqueId_1 = require("../../../../shared/domain/UniqueId");
|
||||
class DeleteScheduleCommand {
|
||||
constructor(scheduleRepo, eventBus) {
|
||||
this.scheduleRepo = scheduleRepo;
|
||||
this.eventBus = eventBus;
|
||||
}
|
||||
async execute(req) {
|
||||
const id = UniqueId_1.UniqueId.from(req.id);
|
||||
const schedule = await this.scheduleRepo.findById(id);
|
||||
if (!schedule)
|
||||
return (0, Result_1.Err)('Schedule not found');
|
||||
await this.scheduleRepo.delete(id);
|
||||
void this.eventBus;
|
||||
return (0, Result_1.Ok)(undefined);
|
||||
}
|
||||
}
|
||||
exports.DeleteScheduleCommand = DeleteScheduleCommand;
|
||||
24
dist/modules/scheduling/application/commands/ToggleScheduleCommand.js
vendored
Normal file
24
dist/modules/scheduling/application/commands/ToggleScheduleCommand.js
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.ToggleScheduleCommand = void 0;
|
||||
const Result_1 = require("../../../../shared/domain/Result");
|
||||
const UniqueId_1 = require("../../../../shared/domain/UniqueId");
|
||||
class ToggleScheduleCommand {
|
||||
constructor(scheduleRepo, eventBus) {
|
||||
this.scheduleRepo = scheduleRepo;
|
||||
this.eventBus = eventBus;
|
||||
}
|
||||
async execute(req) {
|
||||
const schedule = await this.scheduleRepo.findById(UniqueId_1.UniqueId.from(req.id));
|
||||
if (!schedule)
|
||||
return (0, Result_1.Err)('Schedule not found');
|
||||
schedule.toggle(req.enabled);
|
||||
await this.scheduleRepo.update(schedule);
|
||||
for (const event of schedule.domainEvents) {
|
||||
await this.eventBus.publish(event);
|
||||
}
|
||||
schedule.clearEvents();
|
||||
return (0, Result_1.Ok)({ id: req.id, enabled: req.enabled });
|
||||
}
|
||||
}
|
||||
exports.ToggleScheduleCommand = ToggleScheduleCommand;
|
||||
24
dist/modules/scheduling/application/queries/ListSchedulesQuery.js
vendored
Normal file
24
dist/modules/scheduling/application/queries/ListSchedulesQuery.js
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.ListSchedulesQuery = void 0;
|
||||
const Result_1 = require("../../../../shared/domain/Result");
|
||||
class ListSchedulesQuery {
|
||||
constructor(scheduleRepo) {
|
||||
this.scheduleRepo = scheduleRepo;
|
||||
}
|
||||
async execute(req) {
|
||||
const schedules = await this.scheduleRepo.findAll(req.enabledOnly);
|
||||
return (0, Result_1.Ok)(schedules.map((s) => ({
|
||||
id: s.id.toString(),
|
||||
name: s.name,
|
||||
url: s.url,
|
||||
cronExpression: s.cronExpression.value,
|
||||
config: s.config,
|
||||
enabled: s.enabled,
|
||||
lastRunAt: s.lastRunAt,
|
||||
nextRunAt: s.nextRunAt,
|
||||
createdAt: s.createdAt,
|
||||
})));
|
||||
}
|
||||
}
|
||||
exports.ListSchedulesQuery = ListSchedulesQuery;
|
||||
96
dist/modules/scheduling/domain/entities/Schedule.js
vendored
Normal file
96
dist/modules/scheduling/domain/entities/Schedule.js
vendored
Normal file
@@ -0,0 +1,96 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.Schedule = exports.CreateScheduleSchema = void 0;
|
||||
const AggregateRoot_1 = require("../../../../shared/domain/AggregateRoot");
|
||||
const UniqueId_1 = require("../../../../shared/domain/UniqueId");
|
||||
const Result_1 = require("../../../../shared/domain/Result");
|
||||
const CronExpression_1 = require("../value-objects/CronExpression");
|
||||
const ScheduleCreated_1 = require("../events/ScheduleCreated");
|
||||
const ScheduleToggled_1 = require("../events/ScheduleToggled");
|
||||
const zod_1 = require("zod");
|
||||
exports.CreateScheduleSchema = zod_1.z.object({
|
||||
name: zod_1.z.string().min(1).max(100),
|
||||
url: zod_1.z.string().url(),
|
||||
cronExpression: zod_1.z.string().min(1),
|
||||
config: zod_1.z.record(zod_1.z.string(), zod_1.z.unknown()).optional().default({}),
|
||||
enabled: zod_1.z.boolean().optional().default(true),
|
||||
});
|
||||
class Schedule extends AggregateRoot_1.AggregateRoot {
|
||||
get name() { return this.props.name; }
|
||||
get url() { return this.props.url; }
|
||||
get cronExpression() { return this.props.cronExpression; }
|
||||
get config() { return this.props.config; }
|
||||
get enabled() { return this.props.enabled; }
|
||||
get lastRunAt() { return this.props.lastRunAt; }
|
||||
get nextRunAt() { return this.props.nextRunAt; }
|
||||
get createdAt() { return this.props.createdAt; }
|
||||
static create(input) {
|
||||
const parsed = exports.CreateScheduleSchema.safeParse(input);
|
||||
if (!parsed.success) {
|
||||
return (0, Result_1.Err)(parsed.error.issues.map((e) => e.message).join(', '));
|
||||
}
|
||||
const cronResult = CronExpression_1.CronExpression.create(parsed.data.cronExpression);
|
||||
if (!cronResult.ok) {
|
||||
return (0, Result_1.Err)(cronResult.error);
|
||||
}
|
||||
const id = UniqueId_1.UniqueId.create();
|
||||
const now = Date.now();
|
||||
const schedule = new Schedule({
|
||||
name: parsed.data.name,
|
||||
url: parsed.data.url,
|
||||
cronExpression: cronResult.value,
|
||||
config: parsed.data.config,
|
||||
enabled: parsed.data.enabled,
|
||||
lastRunAt: null,
|
||||
nextRunAt: now + 60000, // approximate next run
|
||||
createdAt: now,
|
||||
}, id);
|
||||
schedule.addDomainEvent(new ScheduleCreated_1.ScheduleCreated(id.toString(), {
|
||||
name: parsed.data.name,
|
||||
url: parsed.data.url,
|
||||
cronExpression: parsed.data.cronExpression,
|
||||
}));
|
||||
return (0, Result_1.Ok)(schedule);
|
||||
}
|
||||
static reconstitute(id, props) {
|
||||
const cronResult = CronExpression_1.CronExpression.create(props.cronExpression);
|
||||
// If stored cron is invalid, store raw value — shouldn't happen in practice
|
||||
const cronExpr = (0, Result_1.isOk)(cronResult)
|
||||
? cronResult.value
|
||||
: { props: { value: props.cronExpression }, value: props.cronExpression };
|
||||
return new Schedule({
|
||||
name: props.name,
|
||||
url: props.url,
|
||||
cronExpression: cronExpr,
|
||||
config: props.config,
|
||||
enabled: props.enabled,
|
||||
lastRunAt: props.lastRunAt,
|
||||
nextRunAt: props.nextRunAt,
|
||||
createdAt: props.createdAt,
|
||||
}, UniqueId_1.UniqueId.from(id));
|
||||
}
|
||||
toggle(enabled) {
|
||||
this.props.enabled = enabled;
|
||||
this.addDomainEvent(new ScheduleToggled_1.ScheduleToggled(this.id.toString(), { enabled }));
|
||||
}
|
||||
markFired(now) {
|
||||
this.props.lastRunAt = now;
|
||||
this.props.nextRunAt = now + 60000; // approximate
|
||||
}
|
||||
update(fields) {
|
||||
if (fields.cronExpression !== undefined) {
|
||||
const cronResult = CronExpression_1.CronExpression.create(fields.cronExpression);
|
||||
if (!cronResult.ok)
|
||||
return (0, Result_1.Err)(cronResult.error);
|
||||
this.props.cronExpression = cronResult.value;
|
||||
}
|
||||
if (fields.name !== undefined)
|
||||
this.props.name = fields.name;
|
||||
if (fields.url !== undefined)
|
||||
this.props.url = fields.url;
|
||||
if (fields.config !== undefined)
|
||||
this.props.config = fields.config;
|
||||
return (0, Result_1.Ok)(undefined);
|
||||
}
|
||||
}
|
||||
exports.Schedule = Schedule;
|
||||
14
dist/modules/scheduling/domain/events/ScheduleCreated.js
vendored
Normal file
14
dist/modules/scheduling/domain/events/ScheduleCreated.js
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.ScheduleCreated = void 0;
|
||||
const crypto_1 = require("crypto");
|
||||
class ScheduleCreated {
|
||||
constructor(aggregateId, payload) {
|
||||
this.aggregateId = aggregateId;
|
||||
this.payload = payload;
|
||||
this.eventId = (0, crypto_1.randomUUID)();
|
||||
this.eventName = 'scheduling.schedule_created';
|
||||
this.occurredOn = new Date();
|
||||
}
|
||||
}
|
||||
exports.ScheduleCreated = ScheduleCreated;
|
||||
14
dist/modules/scheduling/domain/events/ScheduleFired.js
vendored
Normal file
14
dist/modules/scheduling/domain/events/ScheduleFired.js
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.ScheduleFired = void 0;
|
||||
const crypto_1 = require("crypto");
|
||||
class ScheduleFired {
|
||||
constructor(aggregateId, payload) {
|
||||
this.aggregateId = aggregateId;
|
||||
this.payload = payload;
|
||||
this.eventId = (0, crypto_1.randomUUID)();
|
||||
this.eventName = 'scheduling.schedule_fired';
|
||||
this.occurredOn = new Date();
|
||||
}
|
||||
}
|
||||
exports.ScheduleFired = ScheduleFired;
|
||||
14
dist/modules/scheduling/domain/events/ScheduleToggled.js
vendored
Normal file
14
dist/modules/scheduling/domain/events/ScheduleToggled.js
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.ScheduleToggled = void 0;
|
||||
const crypto_1 = require("crypto");
|
||||
class ScheduleToggled {
|
||||
constructor(aggregateId, payload) {
|
||||
this.aggregateId = aggregateId;
|
||||
this.payload = payload;
|
||||
this.eventId = (0, crypto_1.randomUUID)();
|
||||
this.eventName = 'scheduling.schedule_toggled';
|
||||
this.occurredOn = new Date();
|
||||
}
|
||||
}
|
||||
exports.ScheduleToggled = ScheduleToggled;
|
||||
2
dist/modules/scheduling/domain/ports/IScheduleRepository.js
vendored
Normal file
2
dist/modules/scheduling/domain/ports/IScheduleRepository.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
54
dist/modules/scheduling/domain/value-objects/CronExpression.js
vendored
Normal file
54
dist/modules/scheduling/domain/value-objects/CronExpression.js
vendored
Normal file
@@ -0,0 +1,54 @@
|
||||
"use strict";
|
||||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
var desc = Object.getOwnPropertyDescriptor(m, k);
|
||||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
||||
desc = { enumerable: true, get: function() { return m[k]; } };
|
||||
}
|
||||
Object.defineProperty(o, k2, desc);
|
||||
}) : (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
o[k2] = m[k];
|
||||
}));
|
||||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
||||
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
||||
}) : function(o, v) {
|
||||
o["default"] = v;
|
||||
});
|
||||
var __importStar = (this && this.__importStar) || (function () {
|
||||
var ownKeys = function(o) {
|
||||
ownKeys = Object.getOwnPropertyNames || function (o) {
|
||||
var ar = [];
|
||||
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
||||
return ar;
|
||||
};
|
||||
return ownKeys(o);
|
||||
};
|
||||
return function (mod) {
|
||||
if (mod && mod.__esModule) return mod;
|
||||
var result = {};
|
||||
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
||||
__setModuleDefault(result, mod);
|
||||
return result;
|
||||
};
|
||||
})();
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.CronExpression = void 0;
|
||||
const ValueObject_1 = require("../../../../shared/domain/ValueObject");
|
||||
const Result_1 = require("../../../../shared/domain/Result");
|
||||
const cron = __importStar(require("node-cron"));
|
||||
class CronExpression extends ValueObject_1.ValueObject {
|
||||
get value() {
|
||||
return this.props.value;
|
||||
}
|
||||
static create(expression) {
|
||||
if (!expression || expression.trim().length === 0) {
|
||||
return (0, Result_1.Err)('Cron expression cannot be empty');
|
||||
}
|
||||
if (!cron.validate(expression)) {
|
||||
return (0, Result_1.Err)(`Invalid cron expression: "${expression}"`);
|
||||
}
|
||||
return (0, Result_1.Ok)(new CronExpression({ value: expression }));
|
||||
}
|
||||
}
|
||||
exports.CronExpression = CronExpression;
|
||||
22
dist/modules/scheduling/index.js
vendored
Normal file
22
dist/modules/scheduling/index.js
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.createSchedulingRouter = exports.KyselyScheduleRepository = exports.SchedulingService = exports.ListSchedulesQuery = exports.DeleteScheduleCommand = exports.ToggleScheduleCommand = exports.CreateScheduleCommand = exports.CronExpression = exports.Schedule = void 0;
|
||||
// Scheduling module public API
|
||||
var Schedule_1 = require("./domain/entities/Schedule");
|
||||
Object.defineProperty(exports, "Schedule", { enumerable: true, get: function () { return Schedule_1.Schedule; } });
|
||||
var CronExpression_1 = require("./domain/value-objects/CronExpression");
|
||||
Object.defineProperty(exports, "CronExpression", { enumerable: true, get: function () { return CronExpression_1.CronExpression; } });
|
||||
var CreateScheduleCommand_1 = require("./application/commands/CreateScheduleCommand");
|
||||
Object.defineProperty(exports, "CreateScheduleCommand", { enumerable: true, get: function () { return CreateScheduleCommand_1.CreateScheduleCommand; } });
|
||||
var ToggleScheduleCommand_1 = require("./application/commands/ToggleScheduleCommand");
|
||||
Object.defineProperty(exports, "ToggleScheduleCommand", { enumerable: true, get: function () { return ToggleScheduleCommand_1.ToggleScheduleCommand; } });
|
||||
var DeleteScheduleCommand_1 = require("./application/commands/DeleteScheduleCommand");
|
||||
Object.defineProperty(exports, "DeleteScheduleCommand", { enumerable: true, get: function () { return DeleteScheduleCommand_1.DeleteScheduleCommand; } });
|
||||
var ListSchedulesQuery_1 = require("./application/queries/ListSchedulesQuery");
|
||||
Object.defineProperty(exports, "ListSchedulesQuery", { enumerable: true, get: function () { return ListSchedulesQuery_1.ListSchedulesQuery; } });
|
||||
var SchedulingService_1 = require("./application/SchedulingService");
|
||||
Object.defineProperty(exports, "SchedulingService", { enumerable: true, get: function () { return SchedulingService_1.SchedulingService; } });
|
||||
var KyselyScheduleRepository_1 = require("./infrastructure/repositories/KyselyScheduleRepository");
|
||||
Object.defineProperty(exports, "KyselyScheduleRepository", { enumerable: true, get: function () { return KyselyScheduleRepository_1.KyselyScheduleRepository; } });
|
||||
var SchedulingController_1 = require("./infrastructure/http/SchedulingController");
|
||||
Object.defineProperty(exports, "createSchedulingRouter", { enumerable: true, get: function () { return SchedulingController_1.createSchedulingRouter; } });
|
||||
76
dist/modules/scheduling/infrastructure/http/SchedulingController.js
vendored
Normal file
76
dist/modules/scheduling/infrastructure/http/SchedulingController.js
vendored
Normal file
@@ -0,0 +1,76 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.createSchedulingRouter = createSchedulingRouter;
|
||||
const express_1 = require("express");
|
||||
const UniqueId_1 = require("../../../../shared/domain/UniqueId");
|
||||
function createSchedulingRouter(deps) {
|
||||
const router = (0, express_1.Router)();
|
||||
const { createSchedule, toggleSchedule, deleteSchedule, listSchedules, schedulingService, scheduleRepo } = deps;
|
||||
// GET /api/schedules
|
||||
router.get('/', async (_req, res) => {
|
||||
const result = await listSchedules.execute({});
|
||||
if (!result.ok) {
|
||||
res.status(500).json({ error: result.error });
|
||||
return;
|
||||
}
|
||||
res.json(result.value);
|
||||
});
|
||||
// POST /api/schedules
|
||||
router.post('/', async (req, res) => {
|
||||
const body = req.body;
|
||||
const result = await createSchedule.execute({
|
||||
name: body.name ?? '',
|
||||
url: body.url ?? '',
|
||||
cronExpression: body.cronExpression ?? '',
|
||||
config: body.config ?? {},
|
||||
enabled: body.enabled !== false,
|
||||
});
|
||||
if (!result.ok) {
|
||||
res.status(400).json({ error: result.error });
|
||||
return;
|
||||
}
|
||||
// Register cron after creation
|
||||
const schedule = await scheduleRepo.findById(UniqueId_1.UniqueId.from(result.value.id));
|
||||
if (schedule) {
|
||||
schedulingService.registerCron(schedule);
|
||||
}
|
||||
res.status(201).json(result.value);
|
||||
});
|
||||
// PATCH /api/schedules/:id/toggle
|
||||
router.patch('/:id/toggle', async (req, res) => {
|
||||
const id = String(req.params['id']);
|
||||
const { enabled } = req.body;
|
||||
if (enabled === undefined) {
|
||||
res.status(400).json({ error: 'enabled is required' });
|
||||
return;
|
||||
}
|
||||
const result = await toggleSchedule.execute({ id, enabled });
|
||||
if (!result.ok) {
|
||||
res.status(result.error === 'Schedule not found' ? 404 : 400).json({ error: result.error });
|
||||
return;
|
||||
}
|
||||
// Update cron registration
|
||||
const schedule = await scheduleRepo.findById(UniqueId_1.UniqueId.from(id));
|
||||
if (schedule) {
|
||||
if (enabled) {
|
||||
schedulingService.registerCron(schedule);
|
||||
}
|
||||
else {
|
||||
schedulingService.unregisterCron(id);
|
||||
}
|
||||
}
|
||||
res.json(result.value);
|
||||
});
|
||||
// DELETE /api/schedules/:id
|
||||
router.delete('/:id', async (req, res) => {
|
||||
const id = String(req.params['id']);
|
||||
schedulingService.unregisterCron(id);
|
||||
const result = await deleteSchedule.execute({ id });
|
||||
if (!result.ok) {
|
||||
res.status(result.error === 'Schedule not found' ? 404 : 400).json({ error: result.error });
|
||||
return;
|
||||
}
|
||||
res.status(204).send();
|
||||
});
|
||||
return router;
|
||||
}
|
||||
74
dist/modules/scheduling/infrastructure/repositories/KyselyScheduleRepository.js
vendored
Normal file
74
dist/modules/scheduling/infrastructure/repositories/KyselyScheduleRepository.js
vendored
Normal file
@@ -0,0 +1,74 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.KyselyScheduleRepository = void 0;
|
||||
const Schedule_1 = require("../../domain/entities/Schedule");
|
||||
class KyselyScheduleRepository {
|
||||
constructor(db) {
|
||||
this.db = db;
|
||||
}
|
||||
async save(schedule) {
|
||||
await this.db
|
||||
.insertInto('schedules')
|
||||
.values({
|
||||
id: schedule.id.toString(),
|
||||
name: schedule.name,
|
||||
url: schedule.url,
|
||||
config_json: JSON.stringify(schedule.config),
|
||||
cron_expression: schedule.cronExpression.value,
|
||||
enabled: schedule.enabled ? 1 : 0,
|
||||
last_run_at: schedule.lastRunAt,
|
||||
next_run_at: schedule.nextRunAt,
|
||||
created_at: schedule.createdAt,
|
||||
})
|
||||
.execute();
|
||||
}
|
||||
async findById(id) {
|
||||
const row = await this.db
|
||||
.selectFrom('schedules')
|
||||
.selectAll()
|
||||
.where('id', '=', id.toString())
|
||||
.executeTakeFirst();
|
||||
if (!row)
|
||||
return null;
|
||||
return this.toEntity(row);
|
||||
}
|
||||
async findAll(enabledOnly = false) {
|
||||
let query = this.db.selectFrom('schedules').selectAll().orderBy('created_at', 'desc');
|
||||
if (enabledOnly) {
|
||||
query = query.where('enabled', '=', 1);
|
||||
}
|
||||
const rows = await query.execute();
|
||||
return rows.map((r) => this.toEntity(r));
|
||||
}
|
||||
async update(schedule) {
|
||||
await this.db
|
||||
.updateTable('schedules')
|
||||
.set({
|
||||
name: schedule.name,
|
||||
url: schedule.url,
|
||||
config_json: JSON.stringify(schedule.config),
|
||||
cron_expression: schedule.cronExpression.value,
|
||||
enabled: schedule.enabled ? 1 : 0,
|
||||
last_run_at: schedule.lastRunAt,
|
||||
next_run_at: schedule.nextRunAt,
|
||||
})
|
||||
.where('id', '=', schedule.id.toString())
|
||||
.execute();
|
||||
}
|
||||
async delete(id) {
|
||||
await this.db.deleteFrom('schedules').where('id', '=', id.toString()).execute();
|
||||
}
|
||||
toEntity(row) {
|
||||
return Schedule_1.Schedule.reconstitute(row.id, {
|
||||
name: row.name,
|
||||
url: row.url,
|
||||
cronExpression: row.cron_expression,
|
||||
config: JSON.parse(row.config_json),
|
||||
enabled: row.enabled === 1,
|
||||
lastRunAt: row.last_run_at,
|
||||
nextRunAt: row.next_run_at,
|
||||
createdAt: row.created_at,
|
||||
});
|
||||
}
|
||||
}
|
||||
exports.KyselyScheduleRepository = KyselyScheduleRepository;
|
||||
Reference in New Issue
Block a user