|
1 | 1 | # async.runjobs |
2 | 2 |
|
3 | | -This module runs all jobs in a job graph concurrently, respecting dependencies. |
| 3 | +Concurrent job execution with dependency support. Can run jobs from a function, jobpool, or jobgraph. |
| 4 | + |
| 5 | +::: tip TIP |
| 6 | +To use this module, you need to import it first: `import("async.runjobs")` |
| 7 | +::: |
4 | 8 |
|
5 | 9 | ## runjobs |
6 | 10 |
|
7 | | -Run all jobs in the job graph concurrently. |
| 11 | +Run jobs concurrently. |
8 | 12 |
|
9 | 13 | #### Function Prototype |
10 | 14 |
|
11 | 15 | ::: tip API |
12 | 16 | ```lua |
13 | | -runjobs(name: <string>, jobs: <jobgraph>, options: <table>) |
| 17 | +runjobs(name: <string>, jobs: <function|jobpool|jobgraph>, options?: <table>) |
14 | 18 | ``` |
15 | 19 | ::: |
16 | 20 |
|
17 | | - |
18 | | -#### Parameter Description |
| 21 | +#### Parameters |
19 | 22 |
|
20 | 23 | | Parameter | Description | |
21 | 24 | |-----------|-------------| |
22 | | -| name | Job name string | |
23 | | -| jobs | Job graph instance | |
24 | | -| options | Options table | |
| 25 | +| name | Job name for coroutine naming and grouping | |
| 26 | +| jobs | Job function, jobpool, or jobgraph instance | |
| 27 | +| options | Optional configuration table | |
| 28 | + |
| 29 | +#### Options |
| 30 | + |
| 31 | +| Option | Type | Default | Description | |
| 32 | +|--------|------|---------|-------------| |
| 33 | +| `total` | number | auto | Total job count (required when jobs is a function) | |
| 34 | +| `comax` | number | 4 | Max concurrent jobs | |
| 35 | +| `timeout` | number | 500 | Timer callback timeout in ms (must be < 60000) | |
| 36 | +| `on_timer` | function | nil | Timer callback: `function(running_jobs_indices)` | |
| 37 | +| `waiting_indicator` | boolean\|table | nil | Enable waiting indicator. See [utils.waiting_indicator](/api/scripts/extension-modules/utils/waiting-indicator) | |
| 38 | +| `progress` | boolean | nil | **Deprecated**: Use `waiting_indicator` instead | |
| 39 | +| `progress_factor` | number | 1.0 | Progress calculation factor | |
| 40 | +| `progress_refresh` | boolean | nil | Enable progress refresh for multirow progress (v3.0.5) | |
| 41 | +| `on_exit` | function | nil | Exit callback: `function(abort_errors)` | |
| 42 | +| `curdir` | string | nil | Working directory for job execution | |
| 43 | +| `isolate` | boolean | nil | Isolate coroutine environments | |
| 44 | +| `remote_only` | boolean | false | Run all jobs remotely only | |
| 45 | +| `distcc` | object | nil | Distcc client for distributed builds | |
| 46 | + |
| 47 | +#### Job Function |
| 48 | + |
| 49 | +When `jobs` is a function, it's called for each job: |
| 50 | + |
| 51 | +```lua |
| 52 | +function(job_index: <number>, total: <number>, opt: <table>) |
| 53 | +``` |
| 54 | + |
| 55 | +The `opt` table provides: |
| 56 | +- `progress`: Progress object with `current()`, `total()`, and `percent()` methods |
| 57 | + |
| 58 | +#### Examples |
| 59 | + |
| 60 | +**Function with total count:** |
| 61 | + |
| 62 | +```lua |
| 63 | +import("async.runjobs") |
| 64 | + |
| 65 | +runjobs("test", function (index, total, opt) |
| 66 | + print("job %d/%d, progress: %s", index, total, opt.progress) |
| 67 | + os.sleep(1000) |
| 68 | +end, { |
| 69 | + total = 100, |
| 70 | + comax = 6, |
| 71 | + timeout = 1000, |
| 72 | + on_timer = function (indices) |
| 73 | + print("running: %s", table.concat(indices, ",")) |
| 74 | + end |
| 75 | +}) |
| 76 | +``` |
| 77 | + |
| 78 | +**Waiting indicator:** |
| 79 | + |
| 80 | +```lua |
| 81 | +import("async.runjobs") |
| 82 | + |
| 83 | +-- Simple indicator |
| 84 | +runjobs("test", function () |
| 85 | + os.sleep(10000) |
| 86 | +end, { |
| 87 | + waiting_indicator = true |
| 88 | +}) |
25 | 89 |
|
26 | | -#### Usage |
| 90 | +-- Custom indicator |
| 91 | +runjobs("test", function () |
| 92 | + os.sleep(10000) |
| 93 | +end, { |
| 94 | + waiting_indicator = { |
| 95 | + chars = {'/', '-', '\\', '|'} |
| 96 | + } |
| 97 | +}) |
| 98 | +``` |
27 | 99 |
|
28 | | -- `comax`: Maximum number of concurrent jobs. |
29 | | -- `timeout`: Timeout for each job (ms). |
30 | | -- `timer`: Timer callback for monitoring running jobs. |
| 100 | +**Multirow progress refresh:** |
| 101 | + |
| 102 | +```lua |
| 103 | +import("async.runjobs") |
| 104 | + |
| 105 | +runjobs("test", function () |
| 106 | + os.sleep(10000) |
| 107 | +end, { |
| 108 | + waiting_indicator = true, |
| 109 | + progress_refresh = true |
| 110 | +}) |
| 111 | +``` |
| 112 | + |
| 113 | +**Using jobgraph:** |
31 | 114 |
|
32 | 115 | ```lua |
33 | 116 | import("core.base.scheduler") |
34 | 117 | import("async.jobgraph") |
35 | 118 | import("async.runjobs") |
36 | 119 |
|
37 | 120 | function jobfunc(index, total, opt) |
38 | | - print("%s: run job (%d/%d)", scheduler.co_running(), index, total) |
| 121 | + print("%s: job (%d/%d)", scheduler.co_running(), index, total) |
39 | 122 | os.sleep(1000) |
40 | | - print("%s: run job (%d/%d) end, progress: %s", scheduler.co_running(), index, total, opt.progress) |
41 | 123 | end |
42 | 124 |
|
43 | 125 | local jobs = jobgraph.new() |
|
53 | 135 | runjobs("test", jobs, { |
54 | 136 | comax = 4, |
55 | 137 | timeout = 1000, |
56 | | - timer = function (running_jobs_indices) |
57 | | - print("timeout, running: %s", table.concat(running_jobs_indices, ",")) |
| 138 | + on_timer = function (indices) |
| 139 | + print("running: %s", table.concat(indices, ",")) |
58 | 140 | end |
59 | 141 | }) |
60 | 142 | ``` |
61 | 143 |
|
62 | | -See also: [async.jobgraph](/api/scripts/extension-modules/async/jobgraph) |
| 144 | +**Using jobpool:** |
| 145 | + |
| 146 | +```lua |
| 147 | +import("async.jobpool") |
| 148 | +import("async.runjobs") |
| 149 | + |
| 150 | +local jobs = jobpool.new() |
| 151 | +local root = jobs:addjob("job/root", function (index, total, opt) |
| 152 | + print(index, total, opt.progress) |
| 153 | +end) |
| 154 | +for i = 1, 3 do |
| 155 | + jobs:addjob("job/" .. i, function (index, total, opt) |
| 156 | + print(index, total, opt.progress) |
| 157 | + end, {rootjob = root}) |
| 158 | +end |
| 159 | + |
| 160 | +runjobs("test", jobs, { |
| 161 | + comax = 6, |
| 162 | + timeout = 1000 |
| 163 | +}) |
| 164 | +``` |
| 165 | + |
| 166 | +**Distributed build:** |
| 167 | + |
| 168 | +```lua |
| 169 | +import("async.runjobs") |
| 170 | + |
| 171 | +local distcc_client = distcc_build_client.singleton() |
| 172 | +runjobs("test", jobs, { |
| 173 | + comax = 6, |
| 174 | + distcc = distcc_client |
| 175 | +}) |
| 176 | +``` |
| 177 | + |
| 178 | +**Exit callback:** |
| 179 | + |
| 180 | +```lua |
| 181 | +import("async.runjobs") |
| 182 | + |
| 183 | +runjobs("test", function (index, total, opt) |
| 184 | + -- job logic |
| 185 | +end, { |
| 186 | + total = 100, |
| 187 | + on_exit = function (abort_errors) |
| 188 | + if abort_errors then |
| 189 | + print("aborted:", abort_errors) |
| 190 | + end |
| 191 | + end |
| 192 | +}) |
| 193 | +``` |
| 194 | + |
| 195 | +**Working directory:** |
| 196 | + |
| 197 | +```lua |
| 198 | +import("async.runjobs") |
| 199 | + |
| 200 | +runjobs("test", function (index, total, opt) |
| 201 | + print(os.getcwd()) -- prints /tmp/build |
| 202 | +end, { |
| 203 | + total = 10, |
| 204 | + curdir = "/tmp/build" |
| 205 | +}) |
| 206 | +``` |
| 207 | + |
| 208 | +See also: [async.jobgraph](/api/scripts/extension-modules/async/jobgraph), [async.jobpool](/api/scripts/extension-modules/async/jobpool) |
0 commit comments