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

Drain connections for python3-http #68

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
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
12 changes: 12 additions & 0 deletions template/python3-http/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@
from flask import Flask, request, jsonify
from waitress import serve
import os
import sys

import signal

from function import handler

app = Flask(__name__)

def SignalHandler(SignalNumber, Frame):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recording my thoughts from the community call here.

I don't think this is required in any of the templates. The watchdog is capable of implementing the required graceful shutdown logic without any changes or even knowledge in the specific template.

My definition of graceful shutdown is that

  1. the orchestration sends the signal to stop the service/function.
  2. the function should now reject any new requests
  3. the function should start a timer and allow any already running requests to finish
  4. the function stops when (a) all requests are completed before the timer or (b) the timer is reached and the function forcefully stops

Due to the design of the watchdog, it is capable of implementing all of this logic because it handles all of the requests and the signals. Once we are satisfied with the graceful shutdown implementation in of-watchdog, then any template that uses the watchdog should be considered as having a graceful shutdown.

timeout = os.getenv("write_timeout")

sys.stderr.write('Function got SIGTERM, draining for up to: {}\n'.format(timeout))
sys.stderr.flush()
alexellis marked this conversation as resolved.
Show resolved Hide resolved

class Event:
def __init__(self):
self.body = request.get_data()
Expand Down Expand Up @@ -66,4 +75,7 @@ def call_handler(path):
return resp

if __name__ == '__main__':

signal.signal(signal.SIGTERM, SignalHandler)

serve(app, host='0.0.0.0', port=5000)