2017 © Pedro Peláez
 

library tasks

Simple task controller supporting multiple types of tasks.

image

yoshi2889/tasks

Simple task controller supporting multiple types of tasks.

  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 3 % Grown

The README.md

Task Controller

Build Status Scrutinizer Code Quality Scrutinizer Code Coverage Latest Stable Version Latest Unstable Version Total Downloads, (*1)

Simple task controller supporting multiple types of tasks., (*2)

Installation

You can install this class via composer:, (*3)

composer require yoshi2889/tasks, (*4)

Usage

Create an instance of TaskController and add any instance of TaskInterface to it:, (*5)

<?php

$loop = React\EventLoop\Factory::create();
$taskController = new \Yoshi2889\Tasks\TaskController($loop);

// A simple callback task, run only once, which will trigger in 10 seconds:
$callbackTask = new \Yoshi2889\Tasks\CallbackTask(function ()
{
    echo 'Hello world!' . PHP_EOL;
}, 10);
// Output (after 10 seconds): Hello world!

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., (*6)

For example, to create a new RepeatableTask that runs the previous CallbackTask every 5 seconds:, (*7)

$repeatableTask = new \Yoshi2889\Tasks\RepeatableTask($callbackTask, 5);

This task would only start running after the 10 seconds defined before in the CallableTask have passed., (*8)

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:, (*9)

$repeatableTask->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., (*10)

TaskController must never cancel tasks on its own, this is up to the user., (*11)

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:, (*12)

$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., (*13)

Implementing custom Tasks

The TaskController accepts any class that implements the TaskInterface interface. This interface contains the following methods:, (*14)

  • 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., (*15)

The Versions

15/09 2017

dev-master

9999999-dev

Simple task controller supporting multiple types of tasks.

  Sources   Download

MIT

The Requires

 

The Development Requires

29/07 2017

v0.1.1

0.1.1.0

Simple task controller supporting multiple types of tasks.

  Sources   Download

MIT

The Requires

 

The Development Requires

10/07 2017

v0.1

0.1.0.0

Simple task controller supporting multiple types of tasks.

  Sources   Download

MIT

The Requires

 

The Development Requires