Skip to content

Commit

Permalink
WebhookServer: Lock serve_forever() and shutdown()
Browse files Browse the repository at this point in the history
  • Loading branch information
jh0ker committed Nov 24, 2015
1 parent e1f3f34 commit c2853fa
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions telegram/utils/webhookhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from telegram import Update, NullHandler
from future.utils import bytes_to_native_str as n
from threading import Lock
import json
try:
import BaseHTTPServer
Expand All @@ -22,24 +23,29 @@ def __init__(self, server_address, RequestHandlerClass, update_queue,
self.update_queue = update_queue
self.webhook_path = webhook_path
self.is_running = False
self.server_lock = Lock()
self.shutdown_lock = Lock()

def serve_forever(self, poll_interval=0.5):
self.is_running = True
self.logger.info("Webhook Server started.")
super(WebhookServer, self).serve_forever(poll_interval)
self.logger.info("Webhook Server stopped.")
with self.server_lock:
self.is_running = True
self.logger.info("Webhook Server started.")
super(WebhookServer, self).serve_forever(poll_interval)
self.logger.info("Webhook Server stopped.")

def shutdown(self):
if not self.is_running:
return
else:
super(WebhookServer, self).shutdown()
with self.shutdown_lock:
if not self.is_running:
return
else:
super(WebhookServer, self).shutdown()
self.is_running = False


# WebhookHandler, process webhook calls
# Based on: https://github.com/eternnoir/pyTelegramBotAPI/blob/master/
# examples/webhook_examples/webhook_cpython_echo_bot.py
class WebhookHandler(BaseHTTPServer.BaseHTTPRequestHandler, object):
class WebhookHandler(BaseHTTPServer.BaseHTTPRequestHandler, object):
server_version = "WebhookHandler/1.0"

def __init__(self, request, client_address, server):
Expand Down

0 comments on commit c2853fa

Please sign in to comment.