$task->beforeRun(function ($task, $preReturn, $index, $handlers) { print_r("before run ---{$preReturn}-----{$index}<br>"); return 11; }); $task->beforeRun(function ($task, $preReturn, $index, $handlers) { print_r("before run ---{$preReturn}-----{$index}<br>"); return 22; }, false); $task->beforeRun(function ($task, $preReturn, $index, $handlers) { print_r("before run ---{$preReturn}-----{$index}<br>"); }); $task->hook('beforeDriverRun', function ($task, $preReturn, $index, $handlers) { print_r("before driver run ---{$preReturn}-----{$index}<br>"); return [1]; }); $task->hook('beforeDriverRun', function ($task, $preReturn, $index, $handlers) { print_r("before driver run ---" . implode(',', $preReturn) . "-----{$index}<br>"); return [1, 2]; }, true); $task->hook('beforeDriverRun', function ($task, $preReturn, $index, $handlers) { print_r("before driver run ---" . implode(',', $preReturn) . "-----{$index}<br>"); return [1, 2, 3]; }); $task->afterRun(function ($task, $results, $preReturn, $index, $handlers) { print_r("after run --------!<br>"); }); }); //run task: $result = Balancer::run('task1', $data); print_r('<br>'); print_r($result);
/** * Deserialize magic method. */ public function __wakeup() { if (empty($this->_status_before_enqueue_)) { return; } $status = $this->_status_before_enqueue_; self::$scheme = self::serializeOrDeserializeScheme($status['scheme']); self::$agentsConfig = $status['agentsConfig']; Balancer::destroy(self::TASK_NAME); self::bootstrap(); self::reinstallHandlers($status['handlers']); }
/** * generator a sms send task * * @return object */ public static function generatorTask() { if (!Balancer::hasTask(self::TASK)) { Balancer::task(self::TASK); } return Balancer::getTask(self::TASK); }
})->data(['this is data 2']); $task->driver('driver_3')->weight(0)->backUp()->data(['this is data 3'])->work(function ($driver, $data) { $driver->success(); print_r('run work! by ' . $driver->name . '<br>'); return ['test.driver3 working', $data]; }); $task->beforeRun(function ($task) { print_r('before run --------!<br>'); }); $task->afterRun(function ($task, $results) { print_r('after run --------!<br>'); }); }); //run task: $data['age'] = '25'; $result = Balancer::run('test1', $data); print_r('<br>resuts data:<br>'); var_dump($result); print_r('<hr>task object:<br>'); var_dump($t); class Person { protected $name; protected $age; public function __construct($name, $age) { $this->name = $name; $this->age = $age; } public function toString() {
/** * generator a sms send task * @return null */ public static function generatorTask() { if (!Balancer::getTask(self::TASK)) { Balancer::task(self::TASK, function ($task) { // create drivers self::createAgents($task); // set hooks handler foreach (self::$hookHandlers as $hook => $handler) { if (is_callable($handler)) { $task->hook($hook, $handler); } } }); } return Balancer::getTask(self::TASK); }