Skip to content

Commit b99f65b

Browse files
committed
readme
1 parent 14f7021 commit b99f65b

File tree

1 file changed

+56
-37
lines changed

1 file changed

+56
-37
lines changed

README.md

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
# Intro
22

3+
[![Latest Stable Version](https://img.shields.io/packagist/v/toplan/task-balancer.svg)](https://packagist.org/packages/toplan/task-balancer)
4+
[![Total Downloads](https://img.shields.io/packagist/dt/toplan/task-balancer.svg)](https://packagist.org/packages/toplan/task-balancer)
5+
36
Lightweight and powerful task load balancing.
47

58
> like the `nginx` load balancing :smile:
69
710
# Features
811

912
- Support multiple drives for every task.
10-
- Automatically choose a driver to execute task by drivers` weight value.
13+
- Automatically choose a driver to execute task by drivers' weight value.
1114
- Support multiple backup drivers.
1215
- Task lifecycle and hooks system.
1316

@@ -24,30 +27,30 @@ composer require toplan/task-balancer:~0.5
2427
Balancer::task('task1', function($task){
2528
//define a driver for current task like this:
2629
$task->driver('driver_1 100 backup', function ($driver, $data) {
27-
//do something here
28-
...
29-
//set whether run success/failure at last
30-
if ($success) {
31-
$driver->success();
32-
} else {
33-
$driver->failure();
34-
}
35-
//return some data you need
36-
return 'some data you need';
37-
});
30+
//do something here
31+
...
32+
//set whether run success/failure at last
33+
if ($success) {
34+
$driver->success();
35+
} else {
36+
$driver->failure();
37+
}
38+
//return some data you need
39+
return 'some data you need';
40+
});
3841

3942
//or like this:
4043
$task->driver('driver_2', 90, function ($driver, $data) {
41-
//...same as above..
42-
})->data(['this is data 2']);
44+
//...same as above..
45+
})->data(['this is data 2']);
4346

4447
//or like this:
4548
$task->driver('driver_3')
46-
->weight(0)->backUp()
47-
->data(['this is data 3'])
48-
->work(function ($driver, $data) {
49-
//...same as above..
50-
});
49+
->weight(0)->backUp()
50+
->data(['this is data 3'])
51+
->work(function ($driver, $data) {
52+
//...same as above..
53+
});
5154
});
5255

5356
//run the task
@@ -79,9 +82,9 @@ The `$result` structure:
7982

8083
# API
8184

82-
## 1. Create & Run
85+
## Balancer
8386

84-
### Balancer::task($name[, $data], Closure $ready);
87+
### Balancer::task($name[, $data][, Closure $ready]);
8588

8689
Create a task instance, and return it.
8790
The closure `$ready` immediately called with argument `$task`.
@@ -102,15 +105,17 @@ The keys of `$options`:
102105
- `data`
103106
- `driver`
104107

105-
### $task->name($name)
108+
## Task
109+
110+
### name($name)
106111

107112
set the name of task.
108113

109-
### $task->data($data)
114+
### data($data)
110115

111116
Set the data of task.
112117

113-
### $task->driver($config[, $weight][, 'backup'], Closure $work)
118+
### driver($config[, $weight][, 'backup'], Closure $work)
114119

115120
Create a driver for the task. The closure `$work` will been called with arguments `$driver` and `$data`.
116121

@@ -122,62 +127,76 @@ $task->driver('driverName 80 backup', function($driver, $data){
122127
});
123128
```
124129

125-
### $driver->weight($weight)
130+
### hasDriver($name)
131+
132+
Whether has the specified driver.
133+
134+
### getDriver($name)
135+
136+
Get driver by name.
137+
138+
### removeDriver($name)
139+
140+
Remove driver from drivers' pool by name.
141+
142+
## Driver
143+
144+
### weight($weight)
126145

127146
Set the weight value of driver.
128147

129-
### $driver->backup($is)
148+
### backup($is)
130149

131150
Set whether backup driver.
132151

133-
> Expected `$is` to be boolean.
152+
> Expected `$is` to be boolean, default `true`.
134153
135-
### $driver->data($data);
154+
### data($data)
136155

137156
Set the data of driver.
138157

139158
> `$data` will store in driver instance.
140159
141-
### $driver->work(Closure $work);
160+
### work(Closure $work);
142161

143162
Set the job content of driver.
144163

145164
> `$data` equals to `$driver->getData()`
146165
147-
### $driver->reset($config[, $weight][, 'backup'], Closure $work)
166+
### reset($config[, $weight][, 'backup'], Closure $work)
148167

149168
Reset driver's weight value, job content and reset whether backup.
150169

151-
### $driver->destroy()
170+
### destroy()
152171

153172
Remove the driver from task which belongs to.
154173

155-
### $driver->failure()
174+
### failure()
156175

157176
Set the driver running failure.
158177

159-
### $driver->success()
178+
### success()
160179

161180
Set the driver run successfully.
162181

163-
### $driver->getDriverData()
182+
### getDriverData()
164183

165184
Get the data which store in driver instance.
166185

167-
### $driver->getTaskData()
186+
### getTaskData()
168187

169188
Get the data which store in task instance.
170189

171190

172-
## 2. Lifecycle & Hooks
191+
## Lifecycle & Hooks
173192

174193
> Support multiple handlers for every hooks!
175194
176195
### Hooks
177196

178197
| Hook name | handler arguments | influence of the last handler's return value |
179198
| --------- | :----------------: | :-----: |
180-
| beforeCreateDriver | $task, $props, $index, &$handlers, $prevReturn | if an array will be merged to original props |
199+
| beforeCreateDriver | $task, $props, $index, &$handlers, $prevReturn | if an array will been merged into original props |
181200
| afterCreateDriver | $task, $driver, $index, &$handlers, $prevReturn | - |
182201
| beforeRun | $task, $index, &$handlers, $prevReturn | if `false` will stop run task and return `false` |
183202
| beforeDriverRun | $task, $driver, $index, &$handlers, $prevReturn | if `false` will stop to use current driver and try to use next backup driver |

0 commit comments

Comments
 (0)