-
Notifications
You must be signed in to change notification settings - Fork 15
Tasks
Jason M edited this page Jun 7, 2017
·
5 revisions
Tasks are simple callbacks that perform some sort of job in a forked child process. When you run a task your code is not blocked and will continue running while the task runs in the background. One example might be a task that sends an email out to 1 or more recipients.
Tasks do not return anything to the parent Daemon process.
Tasks can be one of the following:
- A Closure:
function(){...}
- A callable: [$myObject, 'methodName']
- An object instance that implements
TaskInterface
:new MyTask()
- A FQCN string:
'Lifo\Daemon\Task\SimpleTask'
. The class will be instantiated within the forked process. This is an important distinction. Since the class is instantiated in the child the parent process doesn't use up any extra memory.
...
public function execute() {
$this->task(function() {
sleep(3);
$this->log("Hey look!! I'm logging from inside a task");
});
}
...
...
public function execute() {
$this->task([$this, 'doTask']));
}
public function doTask() {
sleep(3);
$this->log("Hey look!! I'm logging from inside a callable");
}
...
...
public function execute() {
$this->task(new MyTask());
// or
$this->task('MyTask');
}
...
class MyTask() implements Lifo\Daemon\Task\TaskInterface {
public function run() {
sleep(3);
$this->log("Hey look!! I'm logging from inside a class");
}
public function getGroup() {}
public function setup() {}
public function teardown() {}
}