171 lines
5.8 KiB
Markdown
171 lines
5.8 KiB
Markdown
|
---
|
||
|
title: "@push.rocks/taskbuffer"
|
||
|
---
|
||
|
# @push.rocks/taskbuffer
|
||
|
flexible task management. TypeScript ready!
|
||
|
|
||
|
## Install
|
||
|
To install `@push.rocks/taskbuffer`, use the following npm command:
|
||
|
|
||
|
```bash
|
||
|
npm install @push.rocks/taskbuffer --save
|
||
|
```
|
||
|
|
||
|
This will add `@push.rocks/taskbuffer` to your project's dependencies, allowing you to use it within your TypeScript project.
|
||
|
|
||
|
## Usage
|
||
|
`@push.rocks/taskbuffer` offers a versatile way to manage tasks in your application. Whether you need simple asynchronous task execution, buffered tasks to limit concurrent executions, or more complex scenarios involving task chaining and parallel execution, `@push.rocks/taskbuffer` has got you covered. Below, you'll find detailed examples showcasing the module's capabilities and how to use them in TypeScript.
|
||
|
|
||
|
### Basic Task Execution
|
||
|
To create and run a simple task:
|
||
|
|
||
|
```typescript
|
||
|
import { Task } from '@push.rocks/taskbuffer';
|
||
|
|
||
|
const simpleTask = new Task({
|
||
|
name: 'SimpleTask',
|
||
|
taskFunction: async () => {
|
||
|
console.log('Executing a simple task');
|
||
|
// Perform your task here (e.g., fetch data from an API)
|
||
|
}
|
||
|
});
|
||
|
|
||
|
// Trigger the task
|
||
|
simpleTask.trigger();
|
||
|
```
|
||
|
|
||
|
This basic example creates a task that prints a message to the console when executed.
|
||
|
|
||
|
### Buffered Task Execution
|
||
|
Buffered tasks allow you to throttle task executions, for example, to avoid overwhelming a server with simultaneous requests:
|
||
|
|
||
|
```typescript
|
||
|
import { Task } from '@push.rocks/taskbuffer';
|
||
|
|
||
|
const bufferedTask = new Task({
|
||
|
name: 'BufferedTask',
|
||
|
taskFunction: async () => {
|
||
|
// Task implementation
|
||
|
console.log('Buffered task execution');
|
||
|
},
|
||
|
buffered: true,
|
||
|
bufferMax: 3 // Maximum number of concurrent executions
|
||
|
});
|
||
|
|
||
|
// Trigger the task multiple times
|
||
|
bufferedTask.trigger();
|
||
|
bufferedTask.trigger();
|
||
|
bufferedTask.trigger();
|
||
|
bufferedTask.trigger();
|
||
|
```
|
||
|
|
||
|
In this scenario, only three instances of `bufferedTask` will run concurrently, thanks to the buffer settings.
|
||
|
|
||
|
### Task Chains
|
||
|
Task chains are a powerful feature that allows you to execute tasks in a specified order:
|
||
|
|
||
|
```typescript
|
||
|
import { Task, Taskchain } from '@push.rocks/taskbuffer';
|
||
|
|
||
|
const firstTask = new Task({
|
||
|
name: 'FirstTask',
|
||
|
taskFunction: async () => {
|
||
|
console.log('First task execution');
|
||
|
// First task logic
|
||
|
}
|
||
|
});
|
||
|
|
||
|
const secondTask = new Task({
|
||
|
name: 'SecondTask',
|
||
|
taskFunction: async () => {
|
||
|
console.log('Second task execution');
|
||
|
// Second task logic
|
||
|
}
|
||
|
});
|
||
|
|
||
|
const taskChain = new Taskchain({
|
||
|
name: 'MyTaskChain',
|
||
|
taskArray: [firstTask, secondTask] // Tasks will be executed in the order they are provided
|
||
|
});
|
||
|
|
||
|
// Trigger the task chain
|
||
|
taskChain.trigger();
|
||
|
```
|
||
|
|
||
|
### Parallel Task Execution
|
||
|
To run tasks in parallel, use `Taskparallel`:
|
||
|
|
||
|
```typescript
|
||
|
import { Task, Taskparallel } from '@push.rocks/taskbuffer';
|
||
|
|
||
|
const taskOne = new Task({
|
||
|
name: 'TaskOne',
|
||
|
taskFunction: async () => {
|
||
|
// Task one logic
|
||
|
console.log('Task one execution');
|
||
|
}
|
||
|
});
|
||
|
|
||
|
const taskTwo = new Task({
|
||
|
name: 'TaskTwo',
|
||
|
taskFunction: async () => {
|
||
|
// Task two logic
|
||
|
console.log('Task two execution');
|
||
|
}
|
||
|
});
|
||
|
|
||
|
const parallelTask = new Taskparallel({
|
||
|
taskArray: [taskOne, taskTwo] // These tasks will be executed in parallel
|
||
|
});
|
||
|
|
||
|
// Trigger the parallel tasks
|
||
|
parallelTask.trigger();
|
||
|
```
|
||
|
|
||
|
### Task Scheduling with Task Manager
|
||
|
`@push.rocks/taskbuffer` also provides a `TaskManager` to schedule tasks using cron expressions:
|
||
|
|
||
|
```typescript
|
||
|
import { Task, TaskManager } from '@push.rocks/taskbuffer';
|
||
|
|
||
|
const scheduledTask = new Task({
|
||
|
name: 'ScheduledTask',
|
||
|
taskFunction: async () => {
|
||
|
console.log('Scheduled task execution');
|
||
|
// Task logic here
|
||
|
}
|
||
|
});
|
||
|
|
||
|
const taskManager = new TaskManager();
|
||
|
taskManager.addAndScheduleTask(scheduledTask, '0 * * * * *'); // Execute task at the start of every minute
|
||
|
|
||
|
// Start the task manager to enable the scheduled execution
|
||
|
taskManager.start();
|
||
|
```
|
||
|
|
||
|
### Advanced Use Cases
|
||
|
The flexibility of `@push.rocks/taskbuffer` allows for more sophisticated task management scenarios. You can combine the features mentioned above to fit your specific needs, such as chaining parallel tasks, dynamically adding tasks to a manager or chain based on conditions, and much more.
|
||
|
|
||
|
`@push.rocks/taskbuffer` leverages TypeScript's advanced typing features to provide you with clear and predictable APIs, making it easier to build and maintain complex task management logic within your application.
|
||
|
|
||
|
For further information and advanced configurations, please refer to the [official documentation](https://gitlab.com/pushrocks/taskbuffer).
|
||
|
|
||
|
## License and Legal Information
|
||
|
|
||
|
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
|
||
|
|
||
|
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
|
||
|
|
||
|
### Trademarks
|
||
|
|
||
|
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
|
||
|
|
||
|
### Company Information
|
||
|
|
||
|
Task Venture Capital GmbH
|
||
|
Registered at District court Bremen HRB 35230 HB, Germany
|
||
|
|
||
|
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
|
||
|
|
||
|
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
|