UNPKG

express-post-task-scheduler

Version:

A lightweight npm package to create and manage scheduled tasks using Express middleware. Configure tasks via POST requests and execute them at specified times seamlessly.

112 lines (111 loc) 3.91 kB
import { IRecordFilter, ITaskFilter } from "../@types/task"; import { Prisma, PrismaClient } from "@prisma/client"; /** * CRUD tasks in the sqlite db file */ export declare class DB { private static instance; /** * Create or get exist instance of this class */ static init(): DB; /** * Instance of PrismaClient */ conn: PrismaClient | undefined; /** * Restore unfinished tasks from database if the program was terminated */ loadUnfinshedTasks(): Promise<void>; /** * Write a new task info into the database * @param taskName For example: Count * @param taskDescription For example: Execute 3 arrow functions to print Count + index on the terminal * @param executeTime The returned formatted datetime string from scheduleTask, for example: "2024-12-30 17:00:00" */ createTask(taskName: string, taskDescription: string, executeTime: string, data: string): Promise<{ id: number; taskName: string; taskDescription: string; data: string; executeTime: Date; startTime: Date | null; finishTime: Date | null; }>; /** * Mark a executing task as started * @param taskId The created task's id of method createTask */ startTask(taskId: number): Promise<void>; /** * Mark a executed task as finished * @param taskId The created task's id of method createTask */ finishTask(taskId: number): Promise<void>; /** * Executing a task item successfully * @param taskId The primary key of a task * @param itemId The id of each item in the request body's data field * @param itemValue The stringified value of the item */ markItemSuccessful(taskId: number, itemId: string, itemValue: string): Promise<void>; /** * Executing a task item failed * @param taskId The primary key of a task * @param itemId The id of each item in the request body's data field * @param itemValue The stringified value of the item */ markItemFailed(taskId: number, itemId: string, itemValue: string, errorMessage: string): Promise<void>; /** * To filter the task from database * @param filter The object consist of conditions, the example below shows the tasks finished during 2025-01-02 * @example {"finish": {"after": "2025-01-02", "before": "2025-01-03"}} */ getTasks(filter: ITaskFilter): Promise<{ id: number; taskName: string; taskDescription: string; executeTime: Date; startTime: Date | null; finishTime: Date | null; }[]>; /** * To delete the task and all related records * @param taskId Primary key of a task record */ deleteTaskById(taskId: number): Promise<Prisma.BatchPayload | undefined>; /** * To reschedule waiting state task * @param taskId Primary key of a task record * @param executeTime The new executeTime for reschedule task * @returns */ rescheduleTask(taskId: number, executeTime: Date): Promise<{ isSuccessful: boolean; updatedTask: { id: number; taskName: string; taskDescription: string; executeTime: Date; startTime: Date | null; finishTime: Date | null; }; }>; /** * To filter the records from database * @param filter The object consist of conditions, the example below shows the records finished during 2025-01-02 * @example {"finish": {"after": "2025-01-02", "before": "2025-01-03"}} */ getRecords(filter: IRecordFilter): Promise<{ error: string | null; id: number; finishTime: Date; itemId: string; itemValue: string; task: { id: number; taskName: string; taskDescription: string; }; }[]>; }