SFin
SFin

Reputation: 159

How to check if Cloud Pub/Sub emulator is up and running?

I have GC functions which I develop and test locally by using Cloud Pub/Sub emulator.

I want to be able to check from within Go code if Cloud Pub/Sub emulator is up and running. If not, I would like to inform a developer that he/she should start emulator before he/she execute code locally.

When the emulator starts I noticed a line

INFO: Server started, listening on 8085

Maybe I can check if port is available or similar.

Upvotes: 1

Views: 2790

Answers (2)

lucastamoios
lucastamoios

Reputation: 592

You can just request the pubsub endpoint and see if the result is Ok, like this

#!/usr/bin/env sh

# Execute the curl command and store the result.
# $1 is the port pubsub is running, passed as argument
result=$(curl -s "localhost:$1")

# Check if the result is "Ok"
if [ "$result" == "Ok" ]; then
    echo "Success: The output is 'Ok'"
    exit 0
else
    echo "Failure: The output is not 'Ok'"
    exit 1
fi

One of the things that I realized is that using gcloud is not so helpful on the emulator, so using the REST API is the way to go.

Upvotes: 0

Miguel
Miguel

Reputation: 996

I guess you have used this command:

gcloud beta emulators pubsub start

And you got the following output:

[pubsub] This is the Google Pub/Sub fake.
[pubsub] Implementation may be incomplete or differ from the real system.
[pubsub] 
[pubsub] INFO: IAM integration is disabled. IAM policy methods and ACL checks are not supported
[pubsub] 
[pubsub] INFO: Applied Java 7 long hostname workaround.
[pubsub] 
[pubsub] INFO: Server started, listening on 8085

If you take a look at the second INFO message you'll notice that the process name will be JAVA. Now you can run this command:

sudo lsof -i -P -n

Getting all the listening ports and applications, the output should be something like this:

COMMAND PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

XXXX
XXXX
java    XXX  XXX    XX   IPv4  XXX      0t0    TCP 127.0.0.1:8085 (LISTEN)

Alternatively you can modify the previous command to show only what is happening on the desired port:

sudo lsof -i -P -n | grep 8085

Upvotes: 1

Related Questions