Skip to content

Commit e865803

Browse files
committed
update started code
1 parent 54ce49a commit e865803

File tree

3 files changed

+37
-26
lines changed

3 files changed

+37
-26
lines changed

README.md

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,3 @@
11
# learn-pub-sub-starter (Peril)
22

3-
This is the starter code used in Boot.dev's [Learn Pub/Sub](https://learn.boot.dev/learn-pub-sub) course. It contains:
4-
5-
* The `internal/gamelogic` package, which contains the game logic for the Peril game.
6-
* The `internal/routing` package, which contains some routing constants for the game.
7-
* Stubs of the `cmd/client` and `cmd/server` packages, which are `main` packages that run the client and server for the game.
8-
* The `rabbitmq.sh` script, which starts a RabbitMQ server in a Docker container.
9-
10-
## Running the Game
11-
12-
Make sure you have a RabbitMQ server running locally:
13-
14-
```bash
15-
./rabbit.sh
16-
```
17-
18-
Using separate terminal windows, you can run clients and servers:
19-
20-
```bash
21-
go run ./cmd/server
22-
```
23-
24-
```bash
25-
go run ./cmd/client
26-
```
27-
28-
You will be working in this repository and editing it's files throughout the course. You can peek the solution at any time in the course to make sure you're on the right track.
3+
This is the starter code used in Boot.dev's [Learn Pub/Sub](https://learn.boot.dev/learn-pub-sub) course.

internal/gamelogic/logs.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ import (
1111

1212
const logsFile = "game.log"
1313

14+
const writeToDiskSleep = 1 * time.Second
15+
1416
func WriteLog(gamelog routing.GameLog) error {
1517
log.Printf("received game log...")
18+
time.Sleep(writeToDiskSleep)
1619

1720
f, err := os.OpenFile(logsFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
1821
if err != nil {

multiserver.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/bash
2+
3+
# Check if the number of instances was provided
4+
if [ -z "$1" ]; then
5+
echo "Usage: $0 <number-of-instances>"
6+
exit 1
7+
fi
8+
9+
num_instances=$1
10+
11+
# Array to store process IDs
12+
declare -a pids
13+
14+
# Function to kill all processes when Ctrl+C is pressed
15+
cleanup() {
16+
echo "Terminating all instances of ./cmd/server..."
17+
for pid in "${pids[@]}"; do
18+
kill -SIGTERM "$pid"
19+
done
20+
exit
21+
}
22+
23+
# Setup trap for SIGINT
24+
trap 'cleanup' SIGINT
25+
26+
# Start the specified number of instances of the program in the background
27+
for (( i=0; i<num_instances; i++ )); do
28+
go run ./cmd/server &
29+
pids+=($!)
30+
done
31+
32+
# Wait for all background processes to finish
33+
wait

0 commit comments

Comments
 (0)