Skip to content

Commit

Permalink
Modifications required to get pip distribution working, new release
Browse files Browse the repository at this point in the history
  • Loading branch information
randompi committed Mar 30, 2016
1 parent 643bb0b commit 9a537cd
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 82 deletions.
62 changes: 62 additions & 0 deletions beepboop/bot_manager.py
Original file line number Diff line number Diff line change
@@ -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)
90 changes: 13 additions & 77 deletions beepboop/beepboop.py → beepboop/resourcer.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -62,25 +59,25 @@ 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):
self.ws_conn = 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):
Expand Down Expand Up @@ -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)


10 changes: 6 additions & 4 deletions examples/simple.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 9a537cd

Please sign in to comment.