Task scheduler
npm install @guanghechen/scheduler
@guanghechen/scheduler
alt="Npm Version"
src="https://img.shields.io/npm/v/@guanghechen/scheduler.svg"
/>
alt="Npm Download"
src="https://img.shields.io/npm/dm/@guanghechen/scheduler.svg"
/>
alt="Npm License"
src="https://img.shields.io/npm/l/@guanghechen/scheduler.svg"
/>
alt="Module Formats: cjs, esm"
src="https://img.shields.io/badge/module_formats-cjs%2C%20esm-green.svg"
/>
alt="Node.js Version"
src="https://img.shields.io/node/v/@guanghechen/scheduler"
/>
alt="Tested with Jest"
src="https://img.shields.io/badge/tested_with-jest-9c465e.svg"
/>
alt="Code Style: prettier"
src="https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square"
/>
Task scheduler for managing and executing tasks with pipeline support.
- npm
``bash`
npm install --save @guanghechen/scheduler
- yarn
`bash`
yarn add @guanghechen/scheduler
| Name | Description |
| :-------------: | :-------------------------------------------------------: |
| Scheduler | Task scheduler with pipeline and strategy support |Pipeline
| | Processing pipeline for transforming data |
- Basic scheduler:
`typescript
import { Scheduler, Pipeline } from '@guanghechen/scheduler'
import { TaskStrategyEnum } from '@guanghechen/task'
// Create a pipeline for processing items
const pipeline = new 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:
`typescript
import { Pipeline } from '@guanghechen/scheduler'
interface DataItem {
id: number
content: string
}
const pipeline = new 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
}
})
``
- [homepage][homepage]
[homepage]:
https://github.com/guanghechen/sora/tree/@guanghechen/scheduler@7.0.0/packages/scheduler#readme