Rejigger example app.

This commit is contained in:
Tom Wilkie
2015-06-09 16:03:19 +00:00
parent 342d887667
commit e8eb24d65c
16 changed files with 84 additions and 102 deletions

View File

@@ -1,3 +1,4 @@
qotd/qotd
goapp/app
searchapp/searchapp
shout/shout
*.marker

View File

@@ -1,26 +1,28 @@
CC=gcc
CFLAGS=-g -lpthread
%.o: %.c
$(CC) -c -o $@ $< $(CFLAGS)
all: qotd/qotd goapp/app shout/shout
all: qotd.marker app.marker client.marker searchapp.marker shout.marker
qotd/qotd: qotd/qotd.o
gcc -o $@ $< $(CFLAGS)
goapp/app: goapp/app.go
shout/shout: shout/shout.go
%.o: %.c
$(CC) -c -o $@ $< $(CFLAGS)
shout/shout goapp/app:
searchapp/searchapp: searchapp/app.go
shout/shout: shout/shout.go
shout/shout searchapp/searchapp:
go get -tags netgo ./$(@D)
go build -ldflags "-extldflags \"-static\"" -tags netgo -o $@ ./$(@D)
clean:
rm -f qotd/*.o qotd/qotd goapp/app
qotd.marker: qotd/* qotd/qotd
app.marker: app/*
client.marker: client/*
searchapp.marker: searchapp/* searchapp/searchapp
shout.marker: shout/* shout/shout
%.marker:
docker build -t $(<D) $(<D)/
touch $@
run: all
docker-compose kill || true
docker-compose rm -f || true
docker-compose build
docker-compose up -d
clean:
rm -f qotd/*.o qotd/qotd searchapp/searchapp shout/shout *.marker

View File

@@ -3,15 +3,16 @@
To run,
```
make run
make all
./run.sh
```
# "architecture"
```
curl -> pyapp (x2) --> goapp (x2) -> elasticsearch (x3)
|
--> qotd -> internet
|
--> redis
curl -> app --> searchapp -> elasticsearch
|
--> qotd -> internet
|
--> redis
```

View File

@@ -4,4 +4,5 @@ WORKDIR /home/weave
ADD requirements.txt /home/weave/
RUN pip install -r /home/weave/requirements.txt
ADD app.py /home/weave/
EXPOSE 5000
ENTRYPOINT ["python", "/home/weave/app.py"]

View File

@@ -1,19 +1,22 @@
import os
import socket
import sys
import requests
import random
import threading
import logging
from concurrent.futures import ThreadPoolExecutor
from flask import Flask
from redis import Redis
from werkzeug.serving import WSGIRequestHandler
app = Flask(__name__)
redis = Redis(host='redis', port=6379)
pool = ThreadPoolExecutor(max_workers=10)
sessions = threading.local()
goapps = ['http://goapp1:8080/', 'http://goapp2:8080/']
searchapps = ['http://searchapp:8080/']
def do_redis():
redis.incr('hits')
@@ -31,18 +34,27 @@ def do_qotd():
def do_search():
if getattr(sessions, 'session', None) == None:
sessions.session = requests.Session()
r = sessions.session.get(random.choice(goapps))
r = sessions.session.get(random.choice(searchapps))
return r.text
def ignore_error(f):
try:
return str(f())
except:
logging.error("Error executing function", exc_info=sys.exc_info())
return "Error"
@app.route('/')
def hello():
counter_future = pool.submit(do_redis)
search_future = pool.submit(do_search)
qotd_future = pool.submit(do_qotd)
result = 'Hello World! I have been seen %s times.' % counter_future.result()
result += search_future.result()
result += qotd_future.result()
result = 'Hello World! I have been seen %s times.' % ignore_error(counter_future.result)
result += ignore_error(search_future.result)
result += ignore_error(qotd_future.result)
return result
if __name__ == "__main__":
logging.basicConfig(format='%(asctime)s %(levelname)s %(filename)s:%(lineno)d - %(message)s', level=logging.INFO)
WSGIRequestHandler.protocol_version = "HTTP/1.1"
app.run(host="0.0.0.0", port=5000, debug=True)

View File

@@ -1,23 +1,26 @@
import requests
import random
import threading
import time
import logging
import sys
pyapps = ['http://pyapp1:5000/', 'http://pyapp2:5000/']
concurrency = 5
app = 'http://app:5000/'
concurrency = 1
def do_requests():
s = requests.Session()
while True:
try:
s.get(random.choice(pyapps))
s.get(app)
logging.info("Did request")
time.sleep(1)
except:
logging.error("Error doing request", exc_info=sys.exc_info())
logging.info("Did request")
def main():
logging.info("Starting %d thread", concurrency)
logging.info("Starting %d threads", concurrency)
threads = [threading.Thread(target=do_requests) for i in range(concurrency)]
for thread in threads:
thread.start()
@@ -26,5 +29,5 @@ def main():
logging.info("Exiting")
if __name__ == "__main__":
logging.basicConfig()
logging.basicConfig(format='%(asctime)s %(levelname)s %(filename)s:%(lineno)d - %(message)s', level=logging.INFO)
main()

View File

@@ -1,66 +0,0 @@
client:
build: client
links:
- qotd
- pyapp1
- pyapp2
pyapp1:
build: pyapp
expose:
- "5000"
links:
- qotd
- redis
- goapp1
- goapp2
pyapp2:
build: pyapp
expose:
- "5000"
links:
- qotd
- redis
- goapp1
- goapp2
qotd:
build: qotd
expose:
- "4446"
redis:
image: redis
goapp1:
build: goapp
expose:
- "8080"
links:
- elasticsearch1
- elasticsearch2
- elasticsearch3
goapp2:
build: goapp
expose:
- "8080"
links:
- elasticsearch1
- elasticsearch2
- elasticsearch3
elasticsearch1:
image: elasticsearch
expose:
- "9200"
- "9300"
elasticsearch2:
image: elasticsearch
expose:
- "9200"
- "9300"
links:
- elasticsearch1
elasticsearch3:
image: elasticsearch
expose:
- "9200"
- "9300"
links:
- elasticsearch1
- elasticsearch2

View File

@@ -1,2 +0,0 @@
#!/bin/sh
exec /home/weave/app elasticsearch1 elasticsearch2 elasticsearch3

View File

@@ -2,4 +2,5 @@ FROM ubuntu
MAINTAINER Weaveworks Inc <help@weave.works>
WORKDIR /home/weave
ADD ./qotd /home/weave/
EXPOSE 4446
ENTRYPOINT ["/home/weave/qotd"]

25
experimental/example/run.sh Executable file
View File

@@ -0,0 +1,25 @@
#!/bin/bash
set -eux
REPLICAS=${REPLICAS:-1}
start_container() {
IMAGE=$1
BASENAME=${2:-$1}
HOSTNAME=$BASENAME.weave.local
for i in $(seq $REPLICAS); do
if docker inspect $BASENAME$i >/dev/null 2>&1; then
docker rm -f $BASENAME$i
fi
weave run --with-dns --name=$BASENAME$i --hostname=$HOSTNAME $IMAGE
done
}
start_container elasticsearch
start_container searchapp
start_container redis
start_container qotd
start_container app
start_container client

View File

@@ -1,5 +1,6 @@
FROM gliderlabs/alpine
FROM progrium/busybox
MAINTAINER Weaveworks Inc <help@weave.works>
WORKDIR /home/weave
ADD app entrypoint.sh /home/weave/
ADD searchapp entrypoint.sh /home/weave/
EXPOSE 8080
ENTRYPOINT ["/home/weave/entrypoint.sh"]

View File

@@ -0,0 +1,2 @@
#!/bin/sh
exec /home/weave/searchapp elasticsearch.weave.local

View File

@@ -2,4 +2,5 @@ FROM gliderlabs/alpine
MAINTAINER Weaveworks Inc <help@weave.works>
WORKDIR /home/weave
ADD shout /home/weave/
EXPOSE 8090
ENTRYPOINT ["/home/weave/shout"]

View File

@@ -19,7 +19,7 @@ type requestResponse struct {
func main() {
var (
addr = flag.String("addr", ":8080", "HTTP listen address")
addr = flag.String("addr", ":8090", "HTTP listen address")
)
flag.Parse()