Skip to content

Commit

Permalink
Ensure messages get picked up in the right network.
Browse files Browse the repository at this point in the history
  • Loading branch information
jk0 committed May 25, 2016
1 parent 76d47b6 commit ad24119
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 21 deletions.
29 changes: 27 additions & 2 deletions pyhole/core/api.py
Expand Up @@ -20,11 +20,13 @@
import time
import uuid

import utils
import version
from pyhole.core import queue
from pyhole.core import utils
from pyhole.core import version


APP = flask.Flask("pyhole")
QUEUE = queue.MessageQueue()


@APP.route("/", methods=["GET"])
Expand All @@ -33,6 +35,29 @@ def index():
return version.version_string(), 200


# BEGIN MESSAGE API #
@APP.route("/messages/send", methods=["POST"])
def send_message():
"""Send a message."""
request = flask.request.get_json()

try:
item = (
request["network"],
request["source"],
request["target"],
request["message"]
)
except KeyError:
flask.abort(422)

# NOTE(jk0): Disable until auth is implemented.
# QUEUE.put(item)

return "", 200
# END MESSAGE API #


# BEGIN PASTE API #
PASTE_TEMPLATE = """{% set lines = paste.split('\n') %}<html>
<head>
Expand Down
2 changes: 1 addition & 1 deletion pyhole/core/irc/client.py
Expand Up @@ -147,7 +147,7 @@ def on_welcome(self, connection, _event):
else:
connection.join(channel[0])

q = queue.FIFOQueue()
q = queue.MessageQueue()
q.watch(self)

def on_disconnect(self, _connection, _event):
Expand Down
9 changes: 3 additions & 6 deletions pyhole/core/logger.py
Expand Up @@ -32,14 +32,12 @@


class PyholeFileHandler(logging.handlers.TimedRotatingFileHandler):
# CamelCase because superclass is like that
def doRollover(self):
result = super(PyholeFileHandler, self).doRollover()
self.archive_old_logs()
return result

def archive_old_logs(self):
# Compress uncompressed logs
matcher = "*.log.*[!b][!z][!2]"
files = glob.glob(os.path.join(LOG_DIR, matcher))
for file_path in files:
Expand All @@ -51,11 +49,10 @@ def archive_old_logs(self):
compressed_file_path = os.path.join(archive_dir,
compressed_filename)

with open(file_path, 'rb') as input:
with bz2.BZ2File(compressed_file_path,
'wb',
with open(file_path, "rb") as fp:
with bz2.BZ2File(compressed_file_path, "wb",
compresslevel=9) as output:
shutil.copyfileobj(input, output)
shutil.copyfileobj(fp, output)

os.remove(file_path)

Expand Down
22 changes: 15 additions & 7 deletions pyhole/core/queue.py
Expand Up @@ -22,23 +22,31 @@
QUEUE = multiprocessing.Queue()


class FIFOQueue(object):
class MessageQueue(object):
"""Global message queue."""

def __init__(self):
self.queue = QUEUE

def put(self, item):
"""Place an item in the queue."""
"""Place an item into the queue."""
self.queue.put_nowait(item)

@utils.spawn
def watch(self, session):
"""Watch the queue for incoming messages."""
while True:
network, source, target, message = self.queue.get()
item = self.queue.get()

# NOTE(jk0): Right now there is no way to guarantee that the
# message will get delivered to the right network.
_msg = "New message from %s: %s" % (source, message)
session.reply(target, _msg)
actual_network = session.log.name
intended_network = item[0]

# NOTE(jk0): If the intended network does not match the actual
# network, place the item back into the queue and until it gets
# picked up by the intended network.
if actual_network != intended_network:
self.queue.put(item)
continue

_msg = "New message from %s: %s" % (item[1], item[3])
session.reply(item[2], _msg)
2 changes: 1 addition & 1 deletion pyhole/core/slack/client.py
Expand Up @@ -63,7 +63,7 @@ def start(self):
self.client = slackclient.SlackClient(self.api_token)
self.client.rtm_connect()

q = queue.FIFOQueue()
q = queue.MessageQueue()
q.watch(self)

count = 0
Expand Down
2 changes: 1 addition & 1 deletion pyhole/core/slack/message.py
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Slack Message Class"""
"""Slack Message"""


class Message(object):
Expand Down
4 changes: 1 addition & 3 deletions pyhole/core/utils.py
Expand Up @@ -116,7 +116,6 @@ def get_home_directory():
"""Return the home directory."""
home_dir = os.getenv("HOME") + "/.pyhole/"
make_directory(home_dir)

return home_dir


Expand All @@ -125,12 +124,11 @@ def get_directory(new_dir):
home_dir = get_home_directory()
new_dir = os.path.join(home_dir, new_dir, "")
make_directory(new_dir)

return new_dir


def make_directory(directory):
"""Make the specified direectory, if it doesn't exist already."""
"""Make a direectory if it doesn't exist."""
if not os.path.exists(directory):
os.makedirs(directory)

Expand Down

0 comments on commit ad24119

Please sign in to comment.