Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8 background service #8

Open
wants to merge 39 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
ea6f6fe
Using localstack/localstack to mimic multiple AWS services locally
nathanpeck Jul 6, 2018
e1a0ca0
Another service behind an SNS topic and SQS queue
nathanpeck Jul 6, 2018
d554b03
Adding deployment for new background service
nathanpeck Jul 23, 2018
5404864
Fixing a duped name in the code builds
nathanpeck Jul 23, 2018
558af34
Fixing missing bits in the pipeline
nathanpeck Jul 23, 2018
db2fdaa
Removing an uneeded resource dependency in the message cards service …
nathanpeck Jul 24, 2018
61d2774
Adding some missing environment variables
nathanpeck Jul 24, 2018
cec5994
Trying moving all the templates into a subfolder
nathanpeck Jul 24, 2018
4e8b0ec
Fixing service name reference in message cards
nathanpeck Jul 24, 2018
b17fea6
Adding SNS topic, SQS queue, subscription, and env variable plumbing
nathanpeck Jul 24, 2018
58cac2f
Fixing references in the resources template
nathanpeck Jul 24, 2018
cf820e8
Fixing queue URL which should have been a queue ARN
nathanpeck Jul 24, 2018
30ff748
The SNS to SQS subscription also needs to be an ARN
nathanpeck Jul 24, 2018
47f38aa
Fixing reference to get the env variable value
nathanpeck Jul 24, 2018
a6069d0
Adding missing SSM IAM permission to the pipeline role
nathanpeck Jul 24, 2018
b7aaf5b
Fixing silly issue where both builds were sending images to the same ECR
nathanpeck Jul 24, 2018
270ba8f
Updating the pipeline again
nathanpeck Jul 24, 2018
42cd907
Fixing up the Datadog agent integration
nathanpeck Jul 24, 2018
9dbc593
Test out adding some spans for custom tracing
nathanpeck Jul 24, 2018
90eb40f
Trying to fix up the custom tracing
nathanpeck Jul 24, 2018
7776a6c
Fixing span tagging
nathanpeck Jul 24, 2018
28026b1
Adding analyzed spans to the tracing agent
nathanpeck Jul 24, 2018
025bb9a
Trying to fix the span details in Datadog
nathanpeck Jul 24, 2018
a26f4cb
Woops
nathanpeck Jul 24, 2018
b5e6a97
Maybe I need to make use of the scope manager
nathanpeck Jul 24, 2018
93ec6d1
Adding tracking to all the client side handlers
nathanpeck Jul 25, 2018
6f2ead6
Trying to turn on the analyzed spans
nathanpeck Jul 25, 2018
7b80948
More attempts
nathanpeck Jul 25, 2018
eb074cb
Modifying the Datadog settings some more
nathanpeck Jul 25, 2018
e578849
Modifying the environment variables and adding a debug line
nathanpeck Jul 25, 2018
093fd3b
Adding tracking to the background service as well
nathanpeck Jul 25, 2018
3cb5ee8
Adding a policy document to allow SNS to publish to SQS
nathanpeck Jul 25, 2018
2a4a507
Trying again
nathanpeck Jul 25, 2018
204aec0
Policy document needed the queue URL not the queue ARN
nathanpeck Jul 25, 2018
296b783
Removing the debug line
nathanpeck Jul 25, 2018
40bfd0a
Fixing a malformed expression in the SQS policy document
nathanpeck Jul 25, 2018
c9d0ecb
Woops, forgot the package.json dependency
nathanpeck Jul 25, 2018
8b6abee
Fixing cicular stringify exception, and enabling APM trace analytics …
nathanpeck Jul 25, 2018
cb98212
Adding a basic load test
nathanpeck Jul 26, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
.DS_Store
npm-debug.log
deploy-pipeline.sh
25 changes: 19 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
run:
docker-compose up -d

build:
docker-compose build client
up:
docker-compose up -d redis
docker-compose up -d localstack
docker-compose run bootstrap-resources
docker-compose up --no-deps -d client
docker-compose up --no-deps -d message-cards

down:
docker-compose down

test:
docker-compose build client
docker-compose build message-cards
docker-compose build test
docker-compose run --no-deps -d client
docker-compose up --no-deps -d client
docker-compose up --no-deps -d message-cards
docker-compose run --no-deps test

cards:
docker-compose build message-cards
docker-compose up --no-deps -d message-cards
docker-compose run --no-deps test

clean:
docker system prune -a -f
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ RUN apk -v --update add \
groff \
less \
mailcap \
curl \
&& \
pip install --upgrade awscli==1.14.5 s3cmd==2.0.1 python-magic && \
apk -v --purge del py-pip && \
rm /var/cache/apk/*
VOLUME /root/.aws
VOLUME /project
WORKDIR /project
ADD tables.sh .
ENTRYPOINT ["sh", "tables.sh"]
ADD wait-for-it.sh .
ADD bootstrap.sh .
ENTRYPOINT ["sh", "bootstrap.sh"]
119 changes: 119 additions & 0 deletions deps/bootstrap-resources/bootstrap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# Make sure that the localstack resources are up first.
./wait-for-it.sh $SNS_ENDPOINT
./wait-for-it.sh $SQS_ENDPOINT
./wait-for-it.sh $DYNAMODB_ENDPOINT

aws dynamodb list-tables \
--region us-east-1 \
--endpoint-url $DYNAMODB_ENDPOINT \
--cli-connect-timeout 1 \
--cli-read-timeout 1

ENV_NAME="test";

# Local Users table
TABLE_NAME="Users";
RESULT=$(aws dynamodb describe-table \
--region us-east-1 \
--endpoint-url $DYNAMODB_ENDPOINT \
--table-name ${ENV_NAME}_${TABLE_NAME})
if [ $? -eq 0 ]; then
echo "Delete existing table ${TABLE_NAME}";
aws dynamodb delete-table \
--region us-east-1 \
--endpoint-url $DYNAMODB_ENDPOINT \
--table-name ${ENV_NAME}_${TABLE_NAME}
fi
echo "Create table ${TABLE_NAME}";
aws dynamodb create-table \
--region us-east-1 \
--endpoint-url $DYNAMODB_ENDPOINT \
--table-name ${ENV_NAME}_${TABLE_NAME} \
--key-schema AttributeName=username,KeyType=HASH \
--attribute-definitions AttributeName=username,AttributeType=S \
--provisioned-throughput ReadCapacityUnits=10,WriteCapacityUnits=10

# Local Messages table
TABLE_NAME="Messages";
RESULT=$(aws dynamodb describe-table \
--region us-east-1 \
--endpoint-url $DYNAMODB_ENDPOINT \
--table-name ${ENV_NAME}_${TABLE_NAME})
if [ $? -eq 0 ]; then
echo "Delete existing table ${TABLE_NAME}";
aws dynamodb delete-table \
--region us-east-1 \
--endpoint-url $DYNAMODB_ENDPOINT \
--table-name ${ENV_NAME}_${TABLE_NAME}
fi
echo "Create table ${TABLE_NAME}";
aws dynamodb create-table \
--region us-east-1 \
--endpoint-url $DYNAMODB_ENDPOINT \
--table-name ${ENV_NAME}_${TABLE_NAME} \
--key-schema AttributeName=room,KeyType=HASH AttributeName=message,KeyType=RANGE \
--attribute-definitions AttributeName=room,AttributeType=S AttributeName=message,AttributeType=S \
--provisioned-throughput ReadCapacityUnits=10,WriteCapacityUnits=10

# Local SQS queue
QUEUE_NAME="MessagesToCardify";
RESULT=$(aws sqs get-queue-url \
--region us-east-1 \
--endpoint-url $SQS_ENDPOINT \
--queue-name ${ENV_NAME}_${QUEUE_NAME})
if [ $? -eq 0 ]; then
echo "Delete queue ${QUEUE_NAME}";
aws sqs delete-queue \
--region us-east-1 \
--endpoint-url $SQS_ENDPOINT \
--queue-url $SQS_ENDPOINT/queue/${ENV_NAME}_${QUEUE_NAME}
fi
echo "Create queue ${QUEUE_NAME}";
aws sqs delete-queue \
--region us-east-1 \
--endpoint-url $SQS_ENDPOINT \
--queue-url $SQS_ENDPOINT/queue/${ENV_NAME}_${QUEUE_NAME}
aws sqs create-queue \
--region us-east-1 \
--endpoint-url $SQS_ENDPOINT \
--attributes VisibilityTimeout=60 \
--queue-name ${ENV_NAME}_${QUEUE_NAME}

# Local SNS topic
TOPIC_NAME="MessageSent"
TOPIC_ARN="arn:aws:sns:us-east-1:123456789012:${ENV_NAME}_${TOPIC_NAME}"
echo "Create topic ${TOPIC_NAME}";
SUBSCRIPTION_ARN=$(aws sns list-subscriptions-by-topic \
--region us-east-1 \
--endpoint-url $SNS_ENDPOINT \
--topic-arn $TOPIC_ARN \
--query Subscriptions[0].SubscriptionArn \
--output text)
aws sns unsubscribe \
--region us-east-1 \
--endpoint-url $SNS_ENDPOINT \
--subscription-arn $SUBSCRIPTION_ARN
aws sns delete-topic \
--region us-east-1 \
--endpoint-url $SNS_ENDPOINT \
--topic-arn $TOPIC_ARN
aws sns create-topic \
--region us-east-1 \
--endpoint-url $SNS_ENDPOINT \
--name ${ENV_NAME}_${TOPIC_NAME}

# Subscription between the SQS queue and the SNS topic
echo "Create subscription between topic ${TOPIC_NAME} and queue ${QUEUE_NAME}"
aws sns subscribe \
--region us-east-1 \
--endpoint-url $SNS_ENDPOINT \
--topic-arn $TOPIC_ARN \
--protocol sqs \
--notification-endpoint $SQS_ENDPOINT/queue/${ENV_NAME}_${QUEUE_NAME}
aws sns list-subscriptions-by-topic \
--region us-east-1 \
--endpoint-url $SNS_ENDPOINT \
--topic-arn $TOPIC_ARN



32 changes: 32 additions & 0 deletions deps/bootstrap-resources/wait-for-it.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/sh
echo "Waiting for $1 (This may take a few moments)";

clean=${1//http:\/\//}
hostport=${clean//:/ }

set -f; IFS=' '
set -- $hostport
second=$2; fourth=$4
set +f; unset IFS

HOST=$1
PORT=$2

while ! nc -w 1 -z $HOST $PORT; do sleep 1 && echo -n .; done;

#while true
#do
# echo "pre poll";
# STATUS=$(curl -s -o /dev/null -w '%{http_code}' $1)
# echo $STATUS
# if [ $STATUS -eq 404 ]; then
# echo "ready"
# break
# else
# echo -n "."
# fi
# sleep 1
#done


#--output /dev/null --silent --head
39 changes: 0 additions & 39 deletions deps/dynamodb-tables/tables.sh

This file was deleted.

9 changes: 9 additions & 0 deletions deps/sqs-local/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM java:8

ADD https://s3-eu-west-1.amazonaws.com/softwaremill-public/elasticmq-server-0.13.8.jar /
COPY custom.conf /
ENTRYPOINT ["/usr/bin/java", "-Dconfig.file=custom.conf", "-jar", "/elasticmq-server-0.13.8.jar"]

EXPOSE 9324

CMD ["-help"]
8 changes: 8 additions & 0 deletions deps/sqs-local/custom.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
include classpath("application.conf")

node-address {
protocol = http
host = "*"
port = 9324
context-path = ""
}
54 changes: 42 additions & 12 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,80 @@ services:
networks:
- chat
ports:
- 6379:6379
- 6379

# Launch a local version of DynamoDB
dynamodb-local:
# Launch localstack (https://github.com/localstack/localstack)
# This container mimics AWS services locally for offline testing
# and development purposes.
localstack:
image: localstack/localstack
networks:
- chat
build: ./deps/dynamodb-local
ports:
- 8000:8000
# DynamoDB Local
- 4569
# SNS local
- 4575
# SQS local
- 4576

# Ephemeral container used for creating the tables in DynamoDB
dynamodb-tables:
# Ephemeral container used for creating the tables in
# the local DynamoDB tables, SNS topics, and SQS queues
bootstrap-resources:
depends_on:
- dynamodb-local
- localstack
networks:
- chat
build: ./deps/dynamodb-tables
build: ./deps/bootstrap-resources
environment:
DYNAMODB_ENDPOINT: http://dynamodb-local:8000
DYNAMODB_ENDPOINT: http://localstack:4569
SQS_ENDPOINT: http://localstack:4576
SNS_ENDPOINT: http://localstack:4575
AWS_ACCESS_KEY_ID: test
AWS_SECRET_ACCESS_KEY: test

# The actual client application
client:
depends_on:
- redis
- dynamodb-tables
- bootstrap-resources
networks:
- chat
build: ./services/client
environment:
LOCAL: "true"
ENV_NAME: test
REDIS_ENDPOINT: redis
DYNAMODB_ENDPOINT: http://dynamodb-local:8000
DYNAMODB_ENDPOINT: http://localstack:4569
SQS_ENDPOINT: http://localstack:4576
SNS_ENDPOINT: http://localstack:4575
MESSAGE_SENT_SNS_ARN: arn:aws:sns:us-east-1:123456789012:test_MessageSent
AWS_ACCESS_KEY_ID: test
AWS_SECRET_ACCESS_KEY: test
ports:
- 3000:3000

# Background worker that cardifies messages
message-cards:
depends_on:
- redis
- bootstrap-resources
networks:
- chat
build: ./services/message-cards
environment:
LOCAL: "true"
ENV_NAME: test
REDIS_ENDPOINT: redis
QUEUE_URL: http://localstack:4576/queue/test_MessagesToCardify
AWS_ACCESS_KEY_ID: test
AWS_SECRET_ACCESS_KEY: test

# The test suite
test:
depends_on:
- client
- message-cards
networks:
- chat
build: ./services/test-suite
Expand Down
9 changes: 8 additions & 1 deletion services/client/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
// Entrypoint
var server = require('./server');

// Initialize the Datadog APM tracer.
const tracer = require('dd-trace');
tracer.init();

var config = require('./lib/config');
config.tracer = tracer;

var server = require('./server');

server.listen(config.PORT, function() {
console.log('Server listening at port %d', config.PORT);
Expand Down
3 changes: 3 additions & 0 deletions services/client/lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ module.exports = {

REDIS_ENDPOINT: process.env.REDIS_ENDPOINT,
DYNAMODB_ENDPOINT: new AWS.Endpoint(process.env.DYNAMODB_ENDPOINT),
SNS_ENDPOINT: new AWS.Endpoint(process.env.SNS_ENDPOINT),

MESSAGE_SENT_SNS_ARN: process.env.MESSAGE_SENT_SNS_ARN,

// Controls how often clients ping back and forth
HEARTBEAT_TIMEOUT: 8000,
Expand Down
Loading