Skip to content

Commit 84c8f30

Browse files
Fixed ctrl+c and SIGTERM handling (TechEmpower#3491)
Fixed ctrl+c and SIGTERM handling
1 parent f38f2f2 commit 84c8f30

File tree

6 files changed

+85
-58
lines changed

6 files changed

+85
-58
lines changed

Diff for: README.md

+30-10
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,45 @@ required.
2525

2626
$ git clone https://github.com/TechEmpower/FrameworkBenchmarks.git
2727

28-
2. Move into the vagrant-development directory.
28+
2. Change directories
2929

3030
$ cd FrameworkBenchmarks/deployment/vagrant
3131

32-
3. Turn on the VM (takes at least 20 minutes).
32+
3. Create the TFB Docker virtual network
3333

34-
$ vagrant up
34+
$ docker network create tfb
3535

36-
4. Enter the VM.
36+
4. Run a test.
3737

38-
$ vagrant ssh
38+
$ docker run -it --network=tfb -v /var/run/docker.sock:/var/run/docker.sock --mount type=bind,source=[ABS PATH TO THIS DIR],target=/FrameworkBenchmarks techempower/tfb --mode verify --test gemini
3939

40-
5. Move into the FrameworkBenchmarks directory in the vm.
40+
### Explanation of the run script
4141

42-
vagrant@TFB-all:~$ cd ~/FrameworkBenchmarks
43-
44-
6. Run a test.
42+
That run script is pretty wordy, but each and every flag is required. Unfortunately, because of the way that Docker runs processes, you **cannot** put this inside of a shell script without breaking how `ctrl+c` and `SIGTERM` work (the shell script would receive the signal, do nothing with the underlying python suite running, and exit, orphaning the toolset to continue running).
4543

46-
vagrant@TFB-all:~/FrameworkBenchmarks$ tfb --mode verify --test beego
44+
- `-it` tells docker to run this in 'interactive' mode and simulate a TTY, so that `ctrl+c` is propagated.
45+
- `--network=tfb` tells the container to join the 'tfb' Docker virtual network
46+
- `-v` specifies which Docker socket path to mount as a volume in the running container. This allows docker commands run inside this container to use the host container's docker to create/run/stop/remove containers.
47+
- `--mount` mounts the FrameworkBenchmarks source directory as a volume to share with the container so that rebuilding the toolset image is unnecessary and any changes you make on the host system are available in the running toolset container.
48+
- `techempower/tfb` is the name of toolset container to run
49+
- `--mode verify --test gemini` are the command to pass to the toolset.
50+
51+
#### A note on Linux:
52+
53+
You may not want to call step 4 from above every time. You can add an `alias` to your `~/.bash_aliases` file to shorten it since it will not change once configured:
54+
55+
`$ alias tfb="docker run -it --network=tfb -v /var/run/docker.sock:/var/run/docker.sock --mount type=bind,source=[ABS PATH TO THIS DIR],target=/FrameworkBenchmarks techempower/tfb"`
56+
57+
`$ source ~/.bash_aliases`
58+
59+
Now you can run the toolset via `tfb`:
60+
61+
`$ tfb --mode verify --test gemini`
62+
63+
#### A note on Windows:
64+
65+
- Docker expects Linux-style paths. If you cloned on your `C:\` drive, then `[ABS PATH TO THIS DIR]` would be `/c/FrameworkBenchmarks`.
66+
- [Docker for Windows](https://www.docker.com/docker-windows) understands `/var/run/docker.sock` even though that is not a valid path on Windows. [Docker Toolbox](https://docs.docker.com/toolbox/toolbox_install_windows/) **may** not - use at your own risk.
4767

4868
## Add a New Test
4969

Diff for: deployment/vagrant/bootstrap.sh

+5-21
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ if [ ! -e "~/.firstboot" ]; then
2929
sudo cat <<EOF > motd
3030
Welcome to the FrameworkBenchmarks project!
3131
32-
To get started, perhaps try this:
33-
$ cd FrameworkBenchmarks
34-
3532
You can get lots of help:
3633
$ tfb --help
3734
@@ -41,24 +38,11 @@ Welcome to the FrameworkBenchmarks project!
4138
This Vagrant environment is already setup and ready to go.
4239
EOF
4340

44-
sudo mv motd /etc/
45-
46-
sudo cat <<EOF > tfb
47-
#!/bin/bash
48-
49-
# Defaults
50-
ds=/var/run/docker.sock
51-
sd=/home/vagrant/FrameworkBenchmarks
52-
53-
# Build the tfb image
54-
docker pull techempower/tfb
55-
56-
# Create the tfb network
57-
docker network create tfb > /dev/null 2>&1
58-
# Run the suite
59-
docker run --network=tfb -v \${DOCKER_SOCKET_PATH-\$ds}:/var/run/docker.sock --mount type=bind,source=\${TFB_SOURCE_DIR-\$sd},target=/FrameworkBenchmarks techempower/tfb "\$@"
41+
cat <<EOF > /home/vagrant/.bash_aliases
42+
alias tfb="docker run -it --network=tfb -v /var/run/docker.sock:/var/run/docker.sock --mount type=bind,source=/home/vagrant/FrameworkBenchmarks,target=/FrameworkBenchmarks techempower/tfb"
6043
EOF
61-
sudo mv tfb /usr/local/bin
62-
sudo chmod a+x /usr/local/bin/tfb
44+
45+
sudo mv motd /etc/
6346
sudo chmod 777 /var/run/docker.sock
47+
docker network create tfb
6448
fi

Diff for: tfb.sh

-13
This file was deleted.

Diff for: toolset/benchmark/framework_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def start(self):
6868
result = docker_helper.build(self.benchmarker_config, [self.name],
6969
build_log_dir)
7070
if result != 0:
71-
return result
71+
return None
7272

7373
return docker_helper.run(self.benchmarker_config, test_docker_files,
7474
run_log_dir)

Diff for: toolset/run-tests.py

+15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import argparse
22
import socket
33
import sys
4+
import signal
45
from toolset.benchmark.benchmarker import Benchmarker
56
from toolset.utils.scaffolding import Scaffolding
67
from toolset.utils import cleaner
@@ -14,6 +15,9 @@
1415
from colorama import init, Fore
1516
init()
1617

18+
# Required to be globally known
19+
config = None
20+
1721

1822
class StoreSeqAction(argparse.Action):
1923
'''
@@ -42,6 +46,16 @@ def parse_seq(self, argument):
4246
return [abs(int(item)) for item in result]
4347

4448

49+
def __stop(signal, frame):
50+
log("Shutting down (may take a moment)")
51+
docker_helper.stop(config)
52+
sys.exit(0)
53+
54+
55+
signal.signal(signal.SIGTERM, __stop)
56+
signal.signal(signal.SIGINT, __stop)
57+
58+
4559
###################################################################################################
4660
# Main
4761
###################################################################################################
@@ -195,6 +209,7 @@ def main(argv=None):
195209

196210
args = parser.parse_args()
197211

212+
global config
198213
config = BenchmarkConfig(args)
199214
results = Results(config)
200215

Diff for: toolset/utils/docker_helper.py

+34-13
Original file line numberDiff line numberDiff line change
@@ -267,25 +267,46 @@ def stop(benchmarker_config=None,
267267
'''
268268
client = docker.DockerClient(
269269
base_url=benchmarker_config.server_docker_host)
270-
# Stop all our running containers
271-
for container in containers:
272-
container.stop()
273-
# 'techempower/tfb.test.gemini:0.1' -> 'techempower/tfb.test.gemini'
274-
client.images.remove(container.image.tags[0].split(':')[0], force=True)
270+
if containers is None:
271+
for container in client.containers.list():
272+
if len(
273+
container.image.tags
274+
) > 0 and 'techempower' in container.image.tags[0] and 'tfb:latest' not in container.image.tags[0]:
275+
container.stop()
276+
# 'techempower/tfb.test.gemini:0.1' -> 'techempower/tfb.test.gemini'
277+
client.images.remove(
278+
container.image.tags[0].split(':')[0], force=True)
279+
else:
280+
# Stop all our running containers
281+
for container in containers:
282+
container.stop()
283+
# 'techempower/tfb.test.gemini:0.1' -> 'techempower/tfb.test.gemini'
284+
client.images.remove(
285+
container.image.tags[0].split(':')[0], force=True)
286+
287+
database_client = docker.DockerClient(
288+
base_url=benchmarker_config.database_docker_host)
275289
# Stop the database container
276-
if database_container is not None:
290+
if database_container is None:
291+
for container in database_client.containers.list():
292+
if len(
293+
container.image.tags
294+
) > 0 and 'techempower' in container.image.tags[0] and 'tfb:latest' not in container.image.tags[0]:
295+
container.stop()
296+
# 'techempower/tfb.test.gemini:0.1' -> 'techempower/tfb.test.gemini'
297+
client.images.remove(
298+
container.image.tags[0].split(':')[0], force=True)
299+
else:
277300
database_container.stop()
301+
278302
client.images.prune()
279303
client.containers.prune()
280-
client.networks.prune()
281304
client.volumes.prune()
305+
282306
if benchmarker_config.server_docker_host != benchmarker_config.database_docker_host:
283-
db_client = docker.DockerClient(
284-
base_url=benchmarker_config.database_docker_host)
285-
db_client.images.prune()
286-
db_client.containers.prune()
287-
db_client.networks.prune()
288-
db_client.volumes.prune()
307+
database_client.images.prune()
308+
database_client.containers.prune()
309+
database_client.volumes.prune()
289310

290311

291312
def find(path, pattern):

0 commit comments

Comments
 (0)