@guanghechen/scheduler
v7.1.2
Published
Task scheduler
Readme
Task scheduler for managing and executing tasks with pipeline support.
Install
npm
npm install --save @guanghechen/scheduleryarn
yarn add @guanghechen/scheduler
Usage
| Name | Description |
| :-------------: | :-------------------------------------------------------: |
| Scheduler | Task scheduler with pipeline and strategy support |
| Pipeline | Processing pipeline for transforming data |
Example
Basic scheduler:
import { Scheduler, Pipeline } from '@guanghechen/scheduler' import { TaskStrategyEnum } from '@guanghechen/task' // Create a pipeline for processing items const pipeline = new Pipeline<string, string>('my-pipeline') // Add material cookers to the pipeline pipeline.use({ name: 'uppercase-cooker', cook: async (data, embryo, api, next) => { const processed = data.toUpperCase() return next(processed) } }) // Create scheduler const scheduler = new Scheduler({ name: 'my-scheduler', pipeline, strategy: TaskStrategyEnum.ABORT_ON_ERROR }) // Schedule items for processing await scheduler.schedule('hello') await scheduler.schedule('world') // Start processing await scheduler.start()Custom pipeline with multiple stages:
import { Pipeline } from '@guanghechen/scheduler' interface DataItem { id: number content: string } const pipeline = new Pipeline<DataItem, string>('processing-pipeline') pipeline.use({ name: 'validator', cook: async (data, embryo, api, next) => { // Stage 1: Validate if (!data.content) { throw new Error('Content is required') } return next(data) } }) pipeline.use({ name: 'transformer', cook: async (data, embryo, api, next) => { // Stage 2: Transform const transformed = `[${data.id}] ${data.content.trim()}` return next(transformed) } }) pipeline.use({ name: 'logger', cook: async (data, embryo, api, next) => { // Stage 3: Output console.log('Processed:', data) return data } })
