2017 © Pedro Peláez
 

library php-timed-tasks

Easily manage and create multiple timed tasks with PHP

image

weidizhang/php-timed-tasks

Easily manage and create multiple timed tasks with PHP

  • Sunday, August 28, 2016
  • by ebildude123
  • Repository
  • 1 Watchers
  • 1 Stars
  • 3 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

PHP-Timed-Tasks

Created by Weidi Zhang, (*1)

Installation

composer require weidizhang/php-timed-tasks:dev-master

Usage

First, require the autoloader and use the classes., (*2)

require "vendor/autoload.php";

use weidizhang\PHPTimedTasks\Task;
use weidizhang\PHPTimedTasks\TimedTaskManager;

Create a new TimedTaskManager object, (*3)

$manager = new TimedTaskManager();

Using lambda functions

$manager->addTask(
    new Task(function() {
        echo "[" . date("h:i:s A") . "] Hello every 10 seconds!\n";
    }, 10)
);

Here, this function will run forever every 10 seconds., (*4)

Using already declared functions

function myFunc() {
    echo "[" . date("h:i:s A") . "] Hello every 10 seconds!\n";
}

$manager->addTask(
    new Task("myFunc", 10)
);

Simply pass in the function name as a string., (*5)

Setting maximum number of times to run

$manager->addTask(
    (new Task(function() {
        echo "[" . date("h:i:s A") . "] Hello every 10 seconds!\n";
    }, 10))
    ->setMaxTimes(5)
);

This is done by calling the setMaxTimes function of the Task class which accepts an integer., (*6)

In this example, this task will run 5 times total, once every 10 seconds., (*7)

Setting parameters

$manager->addTask(
    (new Task(function($a, $b) {
        echo "[" . date("h:i:s A") . "] Hello every 10 seconds for 5 times with arguments \"" . $a . "\" and \"" . $b . "\"\n";
    }, 10))
    ->setMaxTimes(5)
    ->addParameters(array(
        "test1",
        "test2"
    ))
);

This is done by calling the addParameters function of the Task class which accepts an array., (*8)

Using a random interval range

$manager->addTask(
    (new Task(function() {
        echo "[" . date("h:i:s A") . "] Hello every 5-10 seconds!\n";
    }))
    ->setRandomIntervalRange(5, 10)
);

This task will run every 5 to 10 seconds, a random interval between this range is used each time., (*9)

Running tasks

The first option is to use:, (*10)

$manager->runTasks();

All code after this will not run while there are still tasks remaining., (*11)

If you have other tasks to perform, use your own while loop instead, (*12)

while ($manager->runTasksOnce()) {
    // your additional logic
}

Removing tasks

Removing tasks is simple. The addTask function returns an integer., (*13)

$index = $manager->addTask( ... );

This can be used to remove the task., (*14)

$manager->removeTask($index);

License

Please read LICENSE.md to learn about what you can and cannot do with this source code., (*15)

The Versions

28/08 2016

dev-master

9999999-dev

Easily manage and create multiple timed tasks with PHP

  Sources   Download

CC BY-NC-ND 4.0

The Requires

  • php >=5.5.0