Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nanosector/tasks
Simple task controller for long-running tasks
https://github.com/nanosector/tasks
php task tasks
Last synced: about 1 month ago
JSON representation
Simple task controller for long-running tasks
- Host: GitHub
- URL: https://github.com/nanosector/tasks
- Owner: NanoSector
- License: mit
- Created: 2017-07-10T14:51:13.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-12-01T04:10:46.000Z (12 months ago)
- Last Synced: 2024-08-18T15:39:46.180Z (3 months ago)
- Topics: php, task, tasks
- Language: PHP
- Size: 82 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Task Controller
[![Build Status](https://scrutinizer-ci.com/g/Yoshi2889/tasks/badges/build.png)](https://scrutinizer-ci.com/g/Yoshi2889/tasks/build-status/master)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/Yoshi2889/tasks/badges/quality-score.png)](https://scrutinizer-ci.com/g/Yoshi2889/tasks/?branch=master)
[![Scrutinizer Code Coverage](https://scrutinizer-ci.com/g/Yoshi2889/tasks/badges/coverage.png)](https://scrutinizer-ci.com/g/Yoshi2889/tasks/code-structure/master/code-coverage)
[![Latest Stable Version](https://poser.pugx.org/yoshi2889/tasks/v/stable)](https://packagist.org/packages/yoshi2889/tasks)
[![Latest Unstable Version](https://poser.pugx.org/yoshi2889/tasks/v/unstable)](https://packagist.org/packages/yoshi2889/tasks)
[![Total Downloads](https://poser.pugx.org/yoshi2889/tasks/downloads)](https://packagist.org/packages/yoshi2889/tasks)Simple task controller supporting multiple types of tasks.
## Installation
You can install this class via `composer`:```composer require yoshi2889/tasks```
## Usage
Create an instance of `TaskController` and add any instance of `TaskInterface` to it:```php
cancel();
```However, if we were to cancel a `CallbackTask`, it will just be put in a state where it can no longer be run
by the `TaskController` and will thus be discarded on the next run.
`TaskController` must never cancel tasks on its own, this is up to the user.
### Discarding Tasks
By default, a Task which does not return a new Task on its run() method will be discarded.
However, if a Task does pass back a new Task, the original Task itself gets discarded, but
the Task instance which is passed back will be added in its place. We can observe this with
the following snippet:```php
$callbackTask = new \Yoshi2889\Tasks\CallbackTask(function ()
{
echo 'Hello ';
return new \Yoshi2889\Tasks\CallbackTask(function ()
{
echo 'world!' . PHP_EOL;
}, 5);
}, 5);
// Output (after 10 seconds): Hello world!
```Discarded tasks will be removed from the `TaskController`.
## Implementing custom Tasks
The `TaskController` accepts any class that implements the `TaskInterface` interface. This interface contains the following methods:* `getExpiryTime(): int`: Gets the UNIX timestamp on which the task should be run and discarded afterwards. Please note that
by default, `TaskController` runs at a 1-second interval and timing might be slightly off.
* `run(): ?TaskInterface`: Runs the actual task. Return an object implementing `TaskInterface` to insert a new task, or `null`
to discard the current task.
* `cancel(): void`: Used to cancel the task, or to bring it in a state where it cannot be run. It is a good idea to have
`getExpiryTime()` always return 0 after this method is called so that the task will be discarded.## License
This code is released under the MIT License. Please see `LICENSE` to read it.