Balancer::task('task1', $data, function ($task) { $task->driver('driver1 10 backup', function ($driver, $data) { $driver->failed(); print_r('run work! by ' . $driver->name . '<br>'); }); $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>"); }); });
/** * Get or generate a balancing task instance for send SMS/voice verify. * * @return Task */ public static function getTask() { if (!Balancer::hasTask(self::TASK_NAME)) { Balancer::task(self::TASK_NAME); } return Balancer::getTask(self::TASK_NAME); }
/** * 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); }
require '../src/TaskBalancer/TaskBalancerException.php'; use Toplan\TaskBalance\Balancer; //define task: $data = ['name' => 'top lan', 'age' => '20']; $t = Balancer::task('test1', $data, function ($task) { $task->driver('driver_1 100', 'backup', function ($driver, $data) { $person = new Person($data['name'], $data['age']); $driver->failed(); print_r('run work! by ' . $driver->name . '<br>'); return ['test.driver1 working', $person->toString()]; }); $task->driver('driver_2', 90, function ($driver, $data) { $driver->failed(); print_r('run work! by ' . $driver->name . '<br>'); return ['test.driver2 working', $data]; })->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>');
/** * 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); }