From 9a537cdedbb3e3dec20e8524ae017d981df68807 Mon Sep 17 00:00:00 2001 From: Randall Barnhart Date: Wed, 30 Mar 2016 16:27:18 -0600 Subject: [PATCH] Modifications required to get pip distribution working, new release --- beepboop/bot_manager.py | 62 ++++++++++++++++++ beepboop/{beepboop.py => resourcer.py} | 90 ++++---------------------- examples/simple.py | 10 +-- setup.py | 2 +- 4 files changed, 82 insertions(+), 82 deletions(-) create mode 100644 beepboop/bot_manager.py rename beepboop/{beepboop.py => resourcer.py} (51%) diff --git a/beepboop/bot_manager.py b/beepboop/bot_manager.py new file mode 100644 index 0000000..ff302ed --- /dev/null +++ b/beepboop/bot_manager.py @@ -0,0 +1,62 @@ +import threading +import logging + +logger = logging.getLogger(__name__) + +class BotManager(object): + def __init__(self, spawn_bot): + self.spawn_bot = spawn_bot + self.resources = {} + + def add_bot_resource(self, res): + logging.debug("Adding bot resource: {}".format(res)) + resID = res['resourceID'] + runnableBot = BotRunner(self.spawn_bot(), res) + self.resources[resID] = runnableBot + runnableBot.start() + + def update_bot_resource(self, res): + logging.debug("Updating bot res: {}".format(res)) + if res['resourceID'] in self.resources: + self.resources[res['resourceID']].stop() + runnableBot = BotRunner(self.spawn_bot(), res) + self.resources[res['resourceID']] = runnableBot + runnableBot.start() + else: + logging.error("Failed to find resourceID: {} in resources to update.".format(res['resourceID'])) + + def get_bot_resource(self, resID): + logging.debug("Getting bot resource for resID: {}".format(resID)) + return self.resources[resID] + + def remove_bot_resource(self, resID): + logging.debug("Removing bot resource for resID: {}".format(resID)) + if resID in self.resources: + self.resources[resID].stop() + del self.resources[resID] + else: + logging.error("Failed to find resID: {} in resources to remove.".format(resID)) + + +class BotRunner(threading.Thread): + def __init__(self, bot, resource): + self._stopevent = threading.Event() + self._sleepperiod = 1.0 # 1 second + threading.Thread.__init__(self) + self.setDaemon(True) # thread will stop if main process is killed + self.bot = bot + self.resource = resource + + def run(self): + logging.debug("Starting Bot: {} for Resource: {}".format(self.bot, self.resource)) + self.bot.start(self.resource) + while not self._stopevent.isSet(): + logging.debug("Waiting for Bot thread interrupt on ResourceID: {}".format(self.resource['resourceID'])) + self._stopevent.wait(self._sleepperiod) + logging.debug("Stopped Bot: {} for Resource: {}".format(self.bot, self.resource)) + + def stop(self, timeout=None): + logging.debug("Stopping Bot: {} for Resource: {}".format(self.bot, self.resource)) + self.bot.stop(self.resource) + self._stopevent.set() + threading.Thread.join(self, timeout) diff --git a/beepboop/beepboop.py b/beepboop/resourcer.py similarity index 51% rename from beepboop/beepboop.py rename to beepboop/resourcer.py index 0f3ba5f..8104fd2 100644 --- a/beepboop/beepboop.py +++ b/beepboop/resourcer.py @@ -1,19 +1,16 @@ # -*- coding: utf8 -*- -from __future__ import print_function import os import json import time -import threading - +import websocket +import random import logging log_level = os.getenv("LOG_LEVEL", "INFO") logging.basicConfig(format='%(asctime)s - %(levelname)s: %(message)s', level=log_level) logger = logging.getLogger(__name__) -import websocket -import random # Use binary exponential backoff to desynchronize client requests. # As described by: https://cloud.google.com/storage/docs/exponential-backoff @@ -23,11 +20,11 @@ def expBackoffSleep(n, max_backoff_time): time.sleep(time_to_sleep) -class BeepBoop(object): +class Resourcer(object): def __init__(self, bot_manager=None, token=None, pod_id=None, resourcer=None): self.token = self._getprop(token, "BEEPBOOP_TOKEN") self.pod_id = self._getprop(pod_id, "BEEPBOOP_ID") - self.resourcer = self._getprop(resourcer, "BEEPBOOP_RESOURCER") + self.resourcer_url = self._getprop(resourcer, "BEEPBOOP_RESOURCER") self.ws_conn = None self.ws_app = None @@ -37,12 +34,12 @@ def __init__(self, bot_manager=None, token=None, pod_id=None, resourcer=None): self.bot_manager = bot_manager def start(self): - logging.info('Connecting to Beep Boop Resourcer server: ' + self.resourcer) + logging.info('Connecting to Beep Boop Resourcer server: ' + self.resourcer_url) - ws_app = websocket.WebSocketApp(self.resourcer, - on_message = self.on_message, - on_error = self.on_error, - on_close = self.on_close) + ws_app = websocket.WebSocketApp(self.resourcer_url, + on_message = self.on_message, + on_error = self.on_error, + on_close = self.on_close) ws_app.on_open = self.on_open self.ws_app = ws_app @@ -62,17 +59,17 @@ def _connect(self): def on_message(self, ws, message): msg = json.loads(message) - if self.handler_funcs['on_message']: + if self.handler_funcs is not None and 'on_message' in self.handler_funcs: self.handler_funcs['on_message'](ws, msg) if self.bot_manager is not None: self._handle_message(ws, msg) def on_error(self, ws, error): - if self.handler_funcs['on_error']: + if self.handler_funcs is not None and 'on_error' in self.handler_funcs: self.handler_funcs['on_error'](ws, error) def on_close(self, ws): - if self.handler_funcs['on_close']: + if self.handler_funcs is not None and 'on_close' in self.handler_funcs: self.handler_funcs['on_close'](ws) def on_open(self, ws): @@ -80,7 +77,7 @@ def on_open(self, ws): self._authorize() # reset to 0 since we've reopened a connection self.iter = 0 - if self.handler_funcs['on_open']: + if self.handler_funcs is not None and 'on_open' in self.handler_funcs: self.handler_funcs['on_open'](ws) def _handle_message(self, ws, msg): @@ -108,64 +105,3 @@ def _getprop(self, param, env_var): exit() return v - - -class BotManager(object): - def __init__(self, spawn_bot): - self.spawn_bot = spawn_bot - self.resources = {} - - def add_bot_resource(self, res): - logging.debug("Adding bot resource: {}".format(res)) - resID = res['resourceID'] - runnableBot = BotRunner(self.spawn_bot(), res) - self.resources[resID] = runnableBot - runnableBot.start() - - def update_bot_resource(self, res): - logging.debug("Updating bot res: {}".format(res)) - if res['resourceID'] in self.resources: - self.resources[res['resourceID']].stop() - runnableBot = BotRunner(self.spawn_bot(), res) - self.resources[res['resourceID']] = runnableBot - runnableBot.start() - else: - logging.error("Failed to find resourceID: {} in resources to update.".format(res['resourceID'])) - - def get_bot_resource(self, resID): - logging.debug("Getting bot resource for resID: {}".format(resID)) - return self.resources[resID] - - def remove_bot_resource(self, resID): - logging.debug("Removing bot resource for resID: {}".format(resID)) - if resID in self.resources: - self.resources[resID].stop() - del self.resources[resID] - else: - logging.error("Failed to find resID: {} in resources to remove.".format(resID)) - - -class BotRunner(threading.Thread): - def __init__(self, bot, resource): - self._stopevent = threading.Event() - self._sleepperiod = 1.0 # 1 second - threading.Thread.__init__(self) - self.setDaemon(True) # thread will stop if main process is killed - self.bot = bot - self.resource = resource - - def run(self): - logging.debug("Starting Bot: {} for Resource: {}".format(self.bot, self.resource)) - self.bot.start(self.resource) - while not self._stopevent.isSet(): - logging.debug("Waiting for Bot thread interrupt on ResourceID: {}".format(self.resource['resourceID'])) - self._stopevent.wait(self._sleepperiod) - logging.debug("Stopped Bot: {} for Resource: {}".format(self.bot, self.resource)) - - def stop(self, timeout=None): - logging.debug("Stopping Bot: {} for Resource: {}".format(self.bot, self.resource)) - self.bot.stop(self.resource) - self._stopevent.set() - threading.Thread.join(self, timeout) - - diff --git a/examples/simple.py b/examples/simple.py index 91648f5..08bf250 100644 --- a/examples/simple.py +++ b/examples/simple.py @@ -1,9 +1,11 @@ #from __future__ import print_function -import sys import os +import sys +sys.path.insert(0, os.path.abspath('..')) import pprint -import beepboop +from beepboop import resourcer +from beepboop import bot_manager def spawn_bot(): return SampleBot() @@ -76,8 +78,8 @@ def on_open(ws): # optional to use our bot manager to spawn instances of your bot in daemon threads; # bot developer can choose instead to listen to the websockect messages above and # write their own bot per resource manager or integrate with a 3rd party library that does - botManager = beepboop.BotManager(spawn_bot) + botManager = bot_manager.BotManager(spawn_bot) - bp = beepboop.BeepBoop(botManager) + bp = resourcer.Resourcer(botManager) bp.handlers(handler_funcs) bp.start() diff --git a/setup.py b/setup.py index f8454d2..a07d697 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='beepboop', - version='0.1.0', + version='0.1.1', packages=['beepboop'], description='Beep Boop client and Bot Manager', author='Beep Boop HQ',