-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlisten
executable file
·67 lines (51 loc) · 1.79 KB
/
listen
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
#!/usr/bin/env bash
set -o errexit
source ./localutils.sh
# Listen to given topic:
# - create temporary queue
# - subscribe queue to the given topic
# - start the webhook endpoint
# - create a webhook subscription to the queue
# - monitor the output of the webhook endpoint
# On interrupt, clean up:
# - delete webhook subscription
# - delete queue (which will delete the queue subscription)
# - (leave the webhook endpoint running)
# Usage:
# listen <topic> <webhook app name>
# Functions ----------------------------------------------------------------------
cleanup() {
echo
log Cleaning up - deleting webhook subscription "$webhook_subscription"
./messaging delete_webhook_subscription "$webhook_subscription"
log Cleaning up - deleting queue "$queue"
./management delete_queue "$queue"
exit 0
}
# MAIN ---------------------------------------------------------------------------
if [ "$#" -ne 2 ]; then
echo "Usage: listen <topic> <webhook app name>"
exit 1
fi
topic=$1
if [[ -z "$topic" ]]; then
echo Specify topic
exit 1
fi
# Names for temporary queue and webhook subscription,
# plus details of webhook app in the cloud.
epoch=$(date "+%s")
queue="queue-$epoch"
webhook_app=$2
webhook_url="https://$(cf app "$webhook_app" | awk '/^routes:/ { print $2 }')"
webhook_subscription="whs-$epoch"
log Creating queue "$queue"
./management create_update_queue "$queue"
log Subscribing queue "$queue" to topic "$topic"
./management create_update_queue_subscription "$queue" "$topic"
log Attaching webhook subscription to app "$webhook_app" for queue "$queue"
./messaging create_webhook_subscription "$webhook_subscription" "$queue" "$webhook_url"
log Monitoring messages received at the webhook app "$webhook_app" ...
trap cleanup SIGINT
cf start "$webhook_app"
cf logs "$webhook_app" | grep '☞'