{"id":13669179,"url":"https://github.com/toplan/task-balancer","last_synced_at":"2025-05-05T15:17:46.329Z","repository":{"id":57071823,"uuid":"45529937","full_name":"toplan/task-balancer","owner":"toplan","description":":game_die:Task load balancing for php (like the nginx load balancing)","archived":false,"fork":false,"pushed_at":"2017-06-15T15:24:35.000Z","size":58,"stargazers_count":46,"open_issues_count":0,"forks_count":12,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-05T15:17:32.691Z","etag":null,"topics":["balancing","loadbalancing","task-runner"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/toplan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-11-04T09:54:02.000Z","updated_at":"2023-03-23T12:25:40.000Z","dependencies_parsed_at":"2022-08-24T14:54:32.160Z","dependency_job_id":null,"html_url":"https://github.com/toplan/task-balancer","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toplan%2Ftask-balancer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toplan%2Ftask-balancer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toplan%2Ftask-balancer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toplan%2Ftask-balancer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/toplan","download_url":"https://codeload.github.com/toplan/task-balancer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252522172,"owners_count":21761685,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["balancing","loadbalancing","task-runner"],"created_at":"2024-08-02T08:01:05.240Z","updated_at":"2025-05-05T15:17:46.268Z","avatar_url":"https://github.com/toplan.png","language":"PHP","funding_links":[],"categories":["PHP"],"sub_categories":[],"readme":"# Intro\n\n[![Latest Stable Version](https://img.shields.io/packagist/v/toplan/task-balancer.svg)](https://packagist.org/packages/toplan/task-balancer)\n[![Total Downloads](https://img.shields.io/packagist/dt/toplan/task-balancer.svg)](https://packagist.org/packages/toplan/task-balancer)\n\nLightweight and powerful task load balancing.\n\n\u003e like the `nginx` load balancing :smile:\n\n# Features\n\n- Support multiple drives for every task.\n- Automatically choose a driver to execute task by drivers' weight value.\n- Support multiple backup drivers.\n- Task lifecycle and hooks system.\n\n# Install\n\n```php\ncomposer require toplan/task-balancer:~0.5\n```\n\n# Usage\n\n```php\n//define a task\nBalancer::task('task1', function($task){\n    //define a driver for current task like this:\n    $task-\u003edriver('driver_1 100 backup', function ($driver, $data) {\n        //do something here\n        ...\n        //set whether run success/failure at last\n        if ($success) {\n            $driver-\u003esuccess();\n        } else {\n            $driver-\u003efailure();\n        }\n        //return some data you need\n        return 'some data you need';\n    });\n\n    //or like this:\n    $task-\u003edriver('driver_2', 90, function ($driver, $data) {\n        //...same as above..\n    })-\u003edata(['this is data 2']);\n\n    //or like this:\n    $task-\u003edriver('driver_3')\n    -\u003eweight(0)-\u003ebackUp()\n    -\u003edata(['this is data 3'])\n    -\u003ework(function ($driver, $data) {\n        //...same as above..\n    });\n});\n\n//run the task\n$result = Balancer::run('task1');\n```\n\nThe `$result` structure:\n```php\n[\n    'success' =\u003e true,\n    'time' =\u003e [\n        'started_at' =\u003e timestamp,\n        'finished_at' =\u003e timestamp\n    ],\n    'logs' =\u003e [\n        '0' =\u003e [\n            'driver' =\u003e 'driver_1',\n            'success' =\u003e false,\n            'time' =\u003e [\n                'started_at' =\u003e timestamp,\n                'finished_at' =\u003e timestamp\n            ],\n            'result' =\u003e 'some data you need'\n        ],\n        ...\n    ]\n]\n```\n\n# API\n\n## Balancer\n\n### Balancer::task($name[, $data][, Closure $ready]);\n\nCreate a task instance, and return it.\nThe closure `$ready` immediately called with argument `$task`.\n\n```php\nBalancer::task('taskName', $data, function($task){\n    //task's ready work, such as create drivers.\n});\n```\n\n\u003e `$data` will store in the task instance.\n\n### Balancer::run($name[, array $options])\n\nRun the task by name, and return the result data.\n\nThe keys of `$options`:\n- `data`\n- `driver`\n\n## Task\n\n### name($name)\n\nset the name of task.\n\n### data($data)\n\nSet the data of task.\n\n### driver($config[, $weight][, 'backup'], Closure $work)\n\nCreate a driver for the task. The closure `$work` will been called with arguments `$driver` and `$data`.\n\n\u003e Expected `$weight` to be a integer, default `1`.\n\n```php\n$task-\u003edriver('driverName 80 backup', function($driver, $data){\n    //driver's job content.\n});\n```\n\n### hasDriver($name)\n\nWhether has the specified driver.\n\n### getDriver($name)\n\nGet driver by name.\n\n### removeDriver($name)\n\nRemove driver from drivers' pool by name.\n\n## Driver\n\n### weight($weight)\n\nSet the weight value of driver.\n\n### backup($is)\n\nSet whether backup driver.\n\n\u003e Expected `$is` to be boolean, default `true`.\n\n### data($data)\n\nSet the data of driver.\n\n\u003e `$data` will store in driver instance.\n\n### work(Closure $work);\n\nSet the job content of driver.\n\n\u003e `$data` equals to `$driver-\u003egetData()`\n\n### reset($config[, $weight][, 'backup'], Closure $work)\n\nReset driver's weight value, job content and reset whether backup.\n\n### destroy()\n\nRemove the driver from task which belongs to.\n\n### failure()\n\nSet the driver running failure.\n\n### success()\n\nSet the driver run successfully.\n\n### getDriverData()\n\nGet the data which store in driver instance.\n\n### getTaskData()\n\nGet the data which store in task instance.\n\n\n## Lifecycle \u0026 Hooks\n\n\u003e Support multiple handlers for every hooks!\n\n### Hooks\n\n| Hook name | handler arguments | influence of the last handler's return value |\n| --------- | :----------------: | :-----: |\n| beforeCreateDriver | $task, $props, $index, \u0026$handlers, $prevReturn | if an array will been merged into original props |\n| afterCreateDriver | $task, $driver, $index, \u0026$handlers, $prevReturn | - |\n| beforeRun | $task, $index, \u0026$handlers, $prevReturn | if `false` will stop run task and return `false` |\n| beforeDriverRun | $task, $driver, $index, \u0026$handlers, $prevReturn | if `false` will stop to use current driver and try to use next backup driver |\n| afterDriverRun | $task, $driverResult, $index, \u0026$handlers, $prevReturn | - |\n| afterRun | $task, $taskResult, $index, \u0026$handlers, $prevReturn | if not boolean will override result value |\n\n### Usage\n\n* $task-\u003ehook($hookName, $handler, $override)\n\n* $task-\u003ebeforeCreateDriver($handler, $override)\n\n* $task-\u003eafterCreateDriver($handler, $override)\n\n* $task-\u003ebeforeRun($handler, $override)\n\n* $task-\u003ebeforeDriverRun($handler, $override)\n\n* $task-\u003eafterDriverRun($handler, $override)\n\n* $task-\u003eafterRun($handler, $override)\n\n\u003e `$override` default `false`.\n\n```php\n//example\n$task-\u003ebeforeRun(function($task, $index, $handlers, $prevReturn){\n    //what is $prevReturn?\n    echo $prevReturn == null; //true\n    //what is $index?\n    echo $index == 0; //true\n    //what is $handlers?\n    echo count($handlers); //2\n    //do something..\n    return 'beforeRun_1';\n}, false);\n\n$task-\u003ebeforeRun(function($task, $index, $handlers, $prevReturn){\n    //what is $prevReturn?\n    echo $prevReturn == 'beforeRun_1'; //true\n    //what is $index?\n    echo $index == 1; //true\n    //what is $handlers?\n    echo count($handlers); //2\n    //do other something..\n}, false);\n```\n\n# Dependents\n\n- [phpsms](https://github.com/toplan/phpsms)\n\n# License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftoplan%2Ftask-balancer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftoplan%2Ftask-balancer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftoplan%2Ftask-balancer/lists"}