blazerjob
Version:
TypeScript library for scheduling, executing, and managing asynchronous tasks (custom, HTTP) with a SQLite backend.
390 lines (389 loc) • 16.2 kB
JavaScript
"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;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BlazeJob = void 0;
exports.startServer = startServer;
exports.stopServer = stopServer;
const dotenv = __importStar(require("dotenv"));
dotenv.config();
const fastify_1 = __importDefault(require("fastify"));
// @ts-ignore
const Database = require('better-sqlite3');
const node_fetch_1 = __importDefault(require("node-fetch")); // si node <18
const crypto = __importStar(require("crypto"));
const queries_1 = require("./http/queries");
const ALGORITHM = 'aes-256-gcm';
function getEncryptionKey(optionsKey) {
const key = optionsKey || process.env.BLAZERJOB_ENCRYPTION_KEY || 'default_blazerjob_secret_do_not_use_in_prod';
return crypto.scryptSync(key, 'salt', 32);
}
function encryptConfig(configStr, key) {
if (!configStr)
return configStr;
const iv = crypto.randomBytes(12);
const cipher = crypto.createCipheriv(ALGORITHM, key, iv);
let encrypted = cipher.update(configStr, 'utf8', 'hex');
encrypted += cipher.final('hex');
const authTag = cipher.getAuthTag().toString('hex');
return `enc:v1:${iv.toString('hex')}:${authTag}:${encrypted}`;
}
function decryptConfig(encryptedStr, key) {
if (!encryptedStr || !encryptedStr.startsWith('enc:v1:')) {
return encryptedStr;
}
const parts = encryptedStr.split(':');
if (parts.length !== 5)
return encryptedStr;
const iv = Buffer.from(parts[2], 'hex');
const authTag = Buffer.from(parts[3], 'hex');
const encryptedText = parts[4];
const decipher = crypto.createDecipheriv(ALGORITHM, key, iv);
decipher.setAuthTag(authTag);
let decrypted = decipher.update(encryptedText, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
class BlazeJob {
onAllTasksEnded(cb) {
this.onAllTasksEndedCb = cb;
}
constructor(options) {
// Map: taskId -> { runCount, startedAt, maxRuns, maxDurationMs }
this.taskRunStats = new Map();
this.taskErrorCount = new Map();
this.taskCount = 0;
this.activeTasksCount = 0;
// Map: taskId -> taskFn (en mémoire)
this.taskFns = new Map();
this.encryptionKey = getEncryptionKey(options.encryptionKey);
const useMemoryStorage = options.storage !== 'sqlite';
const dbPath = useMemoryStorage ? ':memory:' : (options.dbPath || 'blazerjob.db');
this.db = new Database(dbPath);
if (!useMemoryStorage) {
this.db.pragma('journal_mode = WAL');
}
this.autoExit = !!options.autoExit;
this.concurrency = options.concurrency || 1;
this.debug = !!options.debug;
this.db.prepare(`
CREATE TABLE IF NOT EXISTS tasks (
id INTEGER PRIMARY KEY AUTOINCREMENT,
runAt TEXT,
interval INTEGER,
priority INTEGER,
retriesLeft INTEGER,
type TEXT,
config TEXT,
webhookUrl TEXT,
status TEXT DEFAULT 'pending',
executed_at TEXT,
created_at TEXT DEFAULT CURRENT_TIMESTAMP,
lastError TEXT
)
`).run();
try {
this.db.prepare('ALTER TABLE tasks ADD COLUMN lastError TEXT').run();
}
catch (e) {
// Ignore si déjà présent
}
try {
this.db.prepare('ALTER TABLE tasks ADD COLUMN webhookUrl TEXT').run();
}
catch (e) {
// Ignore si déjà présent
}
}
async start() {
if (!this.timer) {
this.timer = setInterval(() => this.tick(), 50);
}
}
stop() {
if (this.timer) {
clearInterval(this.timer);
this.timer = undefined;
}
}
async tick() {
if (this.debug) {
console.log('[BlazeJob][DEBUG] tick() called. taskCount:', this.taskCount, 'taskRunStats:', Array.from(this.taskRunStats.keys()));
console.log('[BlazeJob] tick');
}
const now = new Date().toISOString();
const availableSlots = this.concurrency - this.activeTasksCount;
if (availableSlots <= 0)
return;
const selectStmt = this.db.prepare(`
SELECT * FROM tasks
WHERE runAt <= AND status = 'pending'
ORDER BY priority DESC, runAt ASC
LIMIT ${availableSlots}
`);
const dueTasks = selectStmt.all({ now });
if (dueTasks.length === 0)
return;
for (const task of dueTasks) {
this.db.prepare(`UPDATE tasks SET status = 'running' WHERE id = ?`).run(task.id);
this.activeTasksCount++;
(async () => {
const stat = this.taskRunStats.get(task.id);
try {
let taskFn = async () => { };
// Decrypt config before execution if needed
const decryptedConfig = decryptConfig(task.config, this.encryptionKey);
// If the task has a custom JS function (stored in memory), use it
if (this.taskFns.has(task.id)) {
taskFn = this.taskFns.get(task.id);
}
else if (task.type === 'http' && decryptedConfig) {
let config = decryptedConfig;
// Parse multiple times if needed
for (let i = 0; i < 2; i++) {
if (typeof config === 'string') {
try {
config = JSON.parse(config);
}
catch (e) {
console.error('[BlazeJob][HTTP] Config parsing error:', e, config);
break;
}
}
}
taskFn = (0, queries_1.makeHttpTaskFn)(config);
}
else {
// Do not reassign taskFn here to let custom function execute
}
await taskFn();
// Après exécution de la tâche (succès ou erreur)
if (stat) {
stat.runCount++;
// Vérifie si on a atteint maxRuns ou maxDuration
if ((stat.maxRuns && stat.runCount >= stat.maxRuns) || (stat.maxDurationMs && Date.now() - stat.startedAt > stat.maxDurationMs)) {
// On ne replanifiera pas (voir condition plus bas)
}
}
const isOverMaxRuns = stat?.maxRuns && stat.runCount >= stat.maxRuns;
const isOverMaxDuration = stat?.maxDurationMs && Date.now() - stat.startedAt > (stat.maxDurationMs || Infinity);
if (typeof task.interval === 'number' && task.interval > 0 && !isOverMaxRuns && !isOverMaxDuration) {
// Replanifier la tâche périodique
const nextRunAt = new Date(Date.now() + task.interval).toISOString();
this.db.prepare(`UPDATE tasks SET status = 'pending', runAt = ? WHERE id = ?`).run(nextRunAt, task.id);
}
else {
// Tâche terminée (succès ou fin de retry)
this.db.prepare(`UPDATE tasks SET status = 'success', executed_at = ? WHERE id = ?`).run(new Date().toISOString(), task.id);
if (stat && stat.onEnd)
stat.onEnd({ runCount: stat.runCount, errorCount: stat.errorCount || 0 });
this.taskRunStats.delete(task.id);
this.taskCount--;
// console.log('[BlazeJob][DEBUG] Après suppression, taskCount:', this.taskCount, 'taskRunStats:', Array.from(this.taskRunStats.keys()));
if (this.taskCount === 0 && this.onAllTasksEndedCb)
this.onAllTasksEndedCb();
if (this.taskCount === 0 && this.autoExit) {
// console.log('[BlazeJob][DEBUG] Condition autoExit atteinte, arrêt dans 200ms');
setTimeout(() => {
try {
this.stop();
}
catch (e) {
console.error('[BlazeJob] Erreur lors de l\'arrêt du scheduler', e);
}
try {
if (this.db && typeof this.db.close === 'function')
this.db.close();
}
catch (e) {
console.error('[BlazeJob] Erreur lors de la fermeture de la base', e);
}
console.log('[BlazeJob] Toutes les tâches périodiques sont terminées. Arrêt automatique du process.');
process.exit(0);
}, 200);
}
}
}
catch (err) {
console.error('[BlazeJob] Erreur lors de l\'exécution de la tâche', task.id, err);
}
finally {
this.activeTasksCount--;
}
})();
}
// Drain immédiatement si d'autres tâches sont prêtes et qu'il reste de la capacité,
// sans attendre le prochain intervalle.
if (dueTasks.length === availableSlots) {
setImmediate(() => this.tick());
}
}
static async sendWebhook(url, payload) {
try {
await (0, node_fetch_1.default)(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
});
}
catch (e) {
// Optionnel : log ou ignorer
}
}
getTasks() {
const tasks = this.db.prepare('SELECT * FROM tasks').all();
return tasks.map((task) => {
if (task.config) {
task.config = decryptConfig(task.config, this.encryptionKey);
}
return task;
});
}
deleteTask(taskId) {
this.db.prepare('DELETE FROM tasks WHERE id = ?').run(taskId);
// Clean up memory maps
this.taskFns.delete(taskId);
this.taskRunStats.delete(taskId);
this.taskErrorCount.delete(taskId);
}
/**
* Schedule a new task and store its function in the taskMap.
* @param taskFn The function to execute for this task
* @param opts Task options: runAt, interval, priority, retriesLeft, type, config, webhookUrl
* @returns The inserted task ID
*/
schedule(taskFn, options) {
const { runAt, interval, priority, retriesLeft, type, config, webhookUrl, maxRuns, maxDurationMs, onEnd } = options;
const stmt = this.db.prepare(`
INSERT INTO tasks (runAt, interval, priority, retriesLeft, type, config, webhookUrl)
VALUES (?, ?, ?, ?, ?, ?, ?)
`);
const result = stmt.run(runAt instanceof Date ? runAt.toISOString() : runAt, interval, priority, retriesLeft, type, config ? encryptConfig(JSON.stringify(config), this.encryptionKey) : null, webhookUrl);
const taskId = result.lastInsertRowid;
// Stocke la fonction JS en mémoire pour ce taskId (si fournie)
if (taskFn) {
this.taskFns.set(taskId, taskFn);
}
this.taskRunStats.set(taskId, {
runCount: 0,
startedAt: Date.now(),
maxRuns,
maxDurationMs,
onEnd,
errorCount: 0
});
this.taskCount++;
return taskId;
}
}
exports.BlazeJob = BlazeJob;
// Variables globales pour le serveur autonome
let app = null;
let db = null;
let jobs = null;
async function startServer(port = 9000) {
// Initialize Fastify server
app = (0, fastify_1.default)({
logger: true
});
// Register form body parser for x-www-form-urlencoded (before declaring routes)
// eslint-disable-next-line @typescript-eslint/no-var-requires
app.register(require('@fastify/formbody'));
// Initialize scheduler (RAM storage by default)
jobs = new BlazeJob({ storage: 'memory' });
db = jobs['db'];
// GET /tasks: return all scheduled tasks
app.get('/tasks', async (request, reply) => {
const tasks = jobs.getTasks();
reply.send(tasks);
});
// POST /task: schedule a new task
app.post('/task', async (request, reply) => {
const { runAt, interval, priority, retriesLeft, type, config, webhookUrl, maxRuns, maxDurationMs, onEnd } = request.body ?? {};
let taskFn = async () => { };
if (type === 'http' && config) {
const cfg = JSON.parse(config);
taskFn = (0, queries_1.makeHttpTaskFn)(cfg);
}
else {
// Default: dummy task
taskFn = async () => {
console.log('Task executed:', { type, config });
};
}
const taskId = jobs.schedule(taskFn, { runAt, interval, priority, retriesLeft, type, config, webhookUrl, maxRuns, maxDurationMs, onEnd });
reply.code(201).send({ id: taskId });
});
// DELETE /task/:id: delete a task by id
app.delete('/task/:id', async (request, reply) => {
const { id } = request.params;
const taskId = parseInt(id, 10);
db.prepare('DELETE FROM tasks WHERE id = ?').run(id);
// Clean up memory maps
jobs['taskFns'].delete(taskId);
jobs['taskRunStats'].delete(taskId);
reply.code(204).send();
});
await jobs.start();
await app.listen({ port });
console.log(`Fastify server running on http://localhost:${port}`);
}
// Méthode utilitaire pour arrêter proprement le serveur et le scheduler
async function stopServer() {
if (app)
await app.close();
if (jobs)
jobs.stop();
if (jobs && jobs['db'])
jobs['db'].close();
console.log('Serveur et scheduler arrêtés proprement.');
}
// Optionnel : gestion du signal SIGTERM/SIGINT
process.on('SIGTERM', async () => {
await stopServer();
process.exit(0);
});
process.on('SIGINT', async () => {
await stopServer();
process.exit(0);
});
if (require.main === module) {
startServer(9000).catch(err => {
console.error(err);
process.exit(1);
});
}