forked from themattrix/bash-concurrent
-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo.sh
executable file
·147 lines (121 loc) · 4.3 KB
/
demo.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env bash
set -e -o pipefail
# shellcheck source=concurrent.lib.sh
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/concurrent.lib.sh"
success() {
local args=(
- "Creating VM" create_vm 3.0
- "Creating ramdisk" my_sleep 0.1
- "Enabling swap" my_sleep 0.1
- "Populating VM with world data" restore_data 5.0
- "Spigot: Pulling docker image for build" my_sleep 0.5
- "Spigot: Building JAR" my_sleep 6.0
- "Pulling remaining docker images" my_sleep 2.0
- "Launching services" my_sleep 0.2
--require "Creating VM"
--before "Creating ramdisk"
--before "Enabling swap"
--require "Creating ramdisk"
--before "Populating VM with world data"
--before "Spigot: Pulling docker image for build"
--require "Spigot: Pulling docker image for build"
--before "Spigot: Building JAR"
--before "Pulling remaining docker images"
--require "Populating VM with world data"
--require "Spigot: Building JAR"
--require "Pulling remaining docker images"
--before "Launching services"
)
concurrent "${args[@]}"
}
failure() {
local args=(
- "Creating VM" create_vm 3.0
- "Creating ramdisk" my_sleep 0.1
- "Enabling swap" my_sleep 0.1
- "Populating VM with world data" restore_data 0.0 64
- "Spigot: Pulling docker image for build" my_sleep 0.5 128
- "Spigot: Building JAR" my_sleep 6.0
- "Pulling remaining docker images" my_sleep 2.0
- "Launching services" my_sleep 0.2
--require "Creating VM"
--before "Creating ramdisk"
--before "Enabling swap"
--require "Creating ramdisk"
--before "Populating VM with world data"
--before "Spigot: Pulling docker image for build"
--require "Spigot: Pulling docker image for build"
--before "Spigot: Building JAR"
--before "Pulling remaining docker images"
--require "Populating VM with world data"
--require "Spigot: Building JAR"
--require "Pulling remaining docker images"
--before "Launching services"
)
concurrent "${args[@]}"
}
nesting_success() {
local args=(
- "Task A1" my_sleep 2.0
- "Task A2" concurrent
-- "Task B1" concurrent
--- "Task C1" my_sleep 1.0
--- "Task C2" my_sleep 2.0
-- "Task B2" my_sleep 3.0
- "Task A3" my_sleep 4.0
)
concurrent "${args[@]}"
}
nesting_failure() {
local args=(
- "Task A1" my_sleep 2.0
- "Task A2" concurrent
-- "Task B1" concurrent
--- "Task C1" my_sleep 1.0
--- "Task C2" my_sleep 2.0 1
-- "Task B2" my_sleep 3.0
- "Task A3" my_sleep 4.0
)
concurrent "${args[@]}"
}
many() {
local args=()
local i
for (( i = 0; i < 300; i++ )); do
args+=(- "Task ${i}" my_sleep $(( RANDOM % 5 + 1 )))
done
concurrent "${args[@]}"
}
create_vm() {
local provider=digitalocean
echo "(on ${provider})" >&3
my_sleep "${@}"
}
restore_data() {
local data_source=dropbox
echo "(with ${data_source})" >&3
my_sleep "${@}"
}
my_sleep() {
local seconds=${1}
local code=${2:-0}
echo "Yay! Sleeping for ${seconds} second(s)!"
sleep "${seconds}"
if [ "${code}" -ne 0 ]; then
echo "Oh no! Terrible failure!" 1>&2
fi
return "${code}"
}
main() {
if [[ -n "${1}" ]]; then
"${1}"
else
echo
echo "[SUCCESS EXAMPLE]"
success
echo
echo "[FAILURE EXAMPLE]"
failure
fi
}
main "${@}"