Download the PHP package yoshi2889/tasks without Composer
On this page you can find all versions of the php package yoshi2889/tasks. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download yoshi2889/tasks
More information about yoshi2889/tasks
Files in yoshi2889/tasks
Package tasks
Short Description Simple task controller supporting multiple types of tasks.
License MIT
Informations about the package tasks
Task Controller
Simple task controller supporting multiple types of tasks.
Installation
You can install this class via composer
:
Usage
Create an instance of TaskController
and add any instance of TaskInterface
to it:
Repeatable Tasks
A RepeatableTask
instance is a Task which runs its child task on an interval. Before a RepeatableTask
starts
running the child task, it first checks if its expiry time has passed. If it has not, it will not run the task.
For example, to create a new RepeatableTask
that runs the previous CallbackTask
every 5 seconds:
This task would only start running after the 10 seconds defined before in the CallableTask
have passed.
Cancelling Tasks
Tasks can be cancelled prematurely. How a cancel is handled depends on the task type. For instance, if we cancel a RepeatableTask, it will internally cancel its child task and stop repeating, after which it will be discarded:
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:
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 implementingTaskInterface
to insert a new task, ornull
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 havegetExpiryTime()
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.