dev-master
9999999-devEasily manage and create multiple timed tasks with PHP
CC BY-NC-ND 4.0
The Requires
- php >=5.5.0
by Weidi Zhang
Wallogit.com
2017 © Pedro Peláez
Easily manage and create multiple timed tasks with PHP
Created by Weidi Zhang, (*1)
composer require weidizhang/php-timed-tasks:dev-master
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();
$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)
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)
$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)
$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)
$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)
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 is simple. The addTask function returns an integer., (*13)
$index = $manager->addTask( ... );
This can be used to remove the task., (*14)
$manager->removeTask($index);
Please read LICENSE.md to learn about what you can and cannot do with this source code., (*15)
Easily manage and create multiple timed tasks with PHP
CC BY-NC-ND 4.0