0

I'm running a python transformation pipeline by pulling messages from GCP pub/sub, transforming them and publishing them back to pub/sub.

I need to setup an http health check. A healthy server suppose to return code 200. If no results produced for 30 seconds, then server suppose t return code 500.

How do I run an http server in parallel with my pipeline?

Since python functions are executed synchronously, my code doesn't behave the way I want.

from http.server import HTTPServer, BaseHTTPRequestHandler
import time;

lastItemFinishedAt = time.time()
def start():
  while True:
    # pull message from pub/sub
    # if got a message, then transform it and do this:
    lastItemFinishedAt = time.time()
    # if no messages found, then break the loop
  print(' No more messages in the queue')

start()

class Serv(BaseHTTPRequestHandler):

  def do_GET(self):
    if time.time() - lastItemFinishedAt > 30:
      self.send_response(500)
    else:
      self.send_response(200)

httpd = HTTPServer(('localhost',8080),Serv)
httpd.serve_forever()
stkvtflw
  • 12,092
  • 26
  • 78
  • 155

0 Answers0