UNPKG

blazerjob

Version:

TypeScript library for scheduling, executing, and managing asynchronous tasks (custom, HTTP, Cosmos) with a SQLite backend.

115 lines (114 loc) 4.85 kB
#!/usr/bin/env node "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 }); const index_1 = require("../index"); const path = __importStar(require("path")); const fs = __importStar(require("fs")); // Cherche tous les fichiers .db dans le répertoire courant const dbFiles = fs.readdirSync('.').filter(file => file.endsWith('.db')); // Affiche toutes les tâches de toutes les bases de données async function listAllTasks() { for (const dbFile of dbFiles) { console.log(`\n=== Base de données : ${dbFile} ===`); try { const jobs = new index_1.BlazeJob({ dbPath: path.resolve(process.cwd(), dbFile) }); const tasks = jobs['db'].prepare('SELECT id, type, status, runAt, lastError, config FROM tasks ORDER BY runAt DESC').all(); console.table(tasks); } catch (err) { const errorMessage = err instanceof Error ? err.message : 'Unknown error'; console.error(`Error with ${dbFile}:`, errorMessage); } } } async function main() { const [, , cmd, ...args] = process.argv; if (cmd === 'list-all') { await listAllTasks(); return; } // Logique existante... const dbPath = path.resolve(process.cwd(), 'blazerjob.db'); const jobs = new index_1.BlazeJob({ dbPath }); switch (cmd) { case 'schedule': { // Minimal CLI: blazerjob schedule --type shell --cmd "echo hello" --runAt "2025-01-01T00:00:00Z" const opts = {}; for (let i = 0; i < args.length; i++) { if (args[i].startsWith('--')) { opts[args[i].slice(2)] = args[i + 1]; i++; } } if (!opts.type) { console.error('Missing --type'); return process.exit(1); } let config = undefined; if (opts.type === 'shell' && opts.cmd) { config = { cmd: opts.cmd }; } const runAt = opts.runAt || new Date().toISOString(); const interval = opts.interval ? Number(opts.interval) : undefined; const priority = opts.priority ? Number(opts.priority) : undefined; const retriesLeft = opts.retriesLeft ? Number(opts.retriesLeft) : undefined; const webhookUrl = opts.webhookUrl; const taskFn = async () => { }; const id = jobs.schedule(taskFn, { runAt, interval, priority, retriesLeft, type: opts.type, config, webhookUrl }); console.log(`Task scheduled with id: ${id}`); break; } case 'list': { const tasks = jobs['db'].prepare('SELECT * FROM tasks').all(); console.table(tasks); break; } case 'delete': { const id = args[0]; if (!id) { console.error('Please provide the task id to delete.'); return process.exit(1); } jobs['db'].prepare('DELETE FROM tasks WHERE id = ?').run(id); console.log(`Task ${id} deleted.`); break; } case 'help': default: console.log(`Usage: blazerjob <command> [options]\n\nCommands:\n schedule Schedule a new task\n list List tasks in blazerjob.db\n list-all List tasks from all .db files\n delete Delete a task by id\n help Show this help message\n`); } } main().catch(console.error);