Exemplo n.º 1
0
def request_game(game_name, game_id, pubsub_url, timeout=5):
    emcee_pubsub = pubsub_connect(pubsub_url, 'emcee', marshal=dumps)
    game_pubsub = pubsub_connect(pubsub_url, 'games-' + game_id)
    game_state_queue = game_pubsub.subscribe('game_state')

    game_info = {'name': game_name, 'id': game_id}
    emcee_pubsub.publish('game.wanted', game_info)

    return game_pubsub.get_message_block(game_state_queue, timeout) is not None
Exemplo n.º 2
0
def request_game(game_name, game_id, pubsub_url, timeout=5):
    emcee_pubsub = pubsub_connect(pubsub_url, 'emcee', marshal=dumps)
    game_pubsub = pubsub_connect(pubsub_url, 'games-' + game_id)
    game_state_queue = game_pubsub.subscribe('game_state')

    game_info = {'name': game_name, 'id': game_id}
    emcee_pubsub.publish('game.wanted', game_info)

    return game_pubsub.get_message_block(game_state_queue, timeout) is not None
Exemplo n.º 3
0
 def __init__(self, game_id):
     self.game_id = game_id
     pubsub_url = getenv('ALEXANDRA_PUBSUB')
     self.pubsub = pubsub_connect(pubsub_url, 'games-' + game_id,
                                  marshal=dumps, unmarshal=loads)
     self.config = None
     while self.config is None:
         self.config = self.pubsub.get_current_message('game.json')
Exemplo n.º 4
0
 def __init__(self, game_id):
     self.game_id = game_id
     pubsub_url = getenv('ALEXANDRA_PUBSUB')
     self.pubsub = pubsub_connect(pubsub_url,
                                  'games-' + game_id,
                                  marshal=dumps,
                                  unmarshal=loads)
     self.config = None
     while self.config is None:
         self.config = self.pubsub.get_current_message('game.json')
Exemplo n.º 5
0
def start_game(game_info, pubsub_url):
    name, id = game_info['name'], game_info['id']
    sources = ['/games/%s' % (name,),
               '/services/emcee/run_components',
               '/libraries']
    pubsub = pubsub_connect(pubsub_url, 'process', marshal=dumps)

    install_message = {'name': 'run_components.%s' % (id,),
                       'sources': sources,
                       'options': [id, name]}
    pubsub.publish('install', install_message)
Exemplo n.º 6
0
def start_game(game_info, pubsub_url):
    name, id = game_info['name'], game_info['id']
    sources = [
        '/games/%s' % (name, ), '/services/emcee/run_components', '/libraries'
    ]
    pubsub = pubsub_connect(pubsub_url, 'process', marshal=dumps)

    install_message = {
        'name': 'run_components.%s' % (id, ),
        'sources': sources,
        'options': [id, name]
    }
    pubsub.publish('install', install_message)
Exemplo n.º 7
0
def queue_handler(verb, headers, content, context, queue):
    pubsub_url = getenv('ALEXANDRA_PUBSUB')
    pubsub = pubsub_connect(pubsub_url, context)
    if verb == 'GET':
        range_header = headers.get('Range', 'head')
        timeout = float(headers.get('Patience', 0))
        message = get_message_range(pubsub, queue, timeout, range_header)
        if message is None:
            message = ''
        return {'code': 200, 'content': message}
    elif verb == 'DELETE':
        pubsub.unsubscribe(queue)
        return {'code': 200, 'content': ''}
    else:
        return wrong_verb(expected='DELETE or GET', got=verb)
Exemplo n.º 8
0
def queue_handler(verb, headers, content, context, queue):
    pubsub_url = getenv('ALEXANDRA_PUBSUB')
    pubsub = pubsub_connect(pubsub_url, context)
    if verb == 'GET':
        range_header = headers.get('Range', 'head')
        timeout = float(headers.get('Patience', 0))
        message = get_message_range(pubsub, queue, timeout, range_header)
        if message is None:
            message = ''
        return {'code': 200, 'content': message}
    elif verb == 'DELETE':
        pubsub.unsubscribe(queue)
        return {'code': 200, 'content': ''}
    else:
        return wrong_verb(expected='DELETE or GET', got=verb)
Exemplo n.º 9
0
def topic_handler(verb, headers, content, context, topic):
    pubsub_url = getenv('ALEXANDRA_PUBSUB')
    pubsub = pubsub_connect(pubsub_url, context)
    if verb == 'POST':
        pubsub.publish(topic, content)
        return {'code': 200, 'content': ''}
    elif verb == 'GET':
        range_header = headers.get('Range', 'queue')
        if range_header == 'queue':
            queue = pubsub.subscribe(topic)
            return {'code': 200, 'content': queue}
        elif range_header == 'current':
            message = pubsub.get_current_message(topic)
            if message is None:
                message = ''
            return {'code': 200, 'content': message}
    else:
        return wrong_verb(expected='GET or POST', got=verb)
Exemplo n.º 10
0
def topic_handler(verb, headers, content, context, topic):
    pubsub_url = getenv('ALEXANDRA_PUBSUB')
    pubsub = pubsub_connect(pubsub_url, context)
    if verb == 'POST':
        pubsub.publish(topic, content)
        return {'code': 200, 'content': ''}
    elif verb == 'GET':
        range_header = headers.get('Range', 'queue')
        if range_header == 'queue':
            queue = pubsub.subscribe(topic)
            return {'code': 200, 'content': queue}
        elif range_header == 'current':
            message = pubsub.get_current_message(topic)
            if message is None:
                message = ''
            return {'code': 200, 'content': message}
    else:
        return wrong_verb(expected='GET or POST', got=verb)
Exemplo n.º 11
0
#! /usr/bin/env python

from pubsub import connect as pubsub_connect
from sys import argv

def locate(message, pubsub, docstore_url):
    pubsub.publish('location', docstore_url)

docstore_url, pubsub_url = argv[1:3]
pubsub = pubsub_connect(pubsub_url, 'docstore')
pubsub.consume_topic('locate', lambda m: locate(m, pubsub, docstore_url))
Exemplo n.º 12
0
#! /usr/bin/env python

from json import loads, dumps
from os import getenv
from pubsub import connect as pubsub_connect
from sys import argv


def start_game(game_info, pubsub_url):
    name, id = game_info['name'], game_info['id']
    sources = [
        '/games/%s' % (name, ), '/services/emcee/run_components', '/libraries'
    ]
    pubsub = pubsub_connect(pubsub_url, 'process', marshal=dumps)

    install_message = {
        'name': 'run_components.%s' % (id, ),
        'sources': sources,
        'options': [id, name]
    }
    pubsub.publish('install', install_message)


pubsub_url = getenv('ALEXANDRA_PUBSUB')
pubsub = pubsub_connect(pubsub_url, 'emcee', unmarshal=loads)
pubsub.consume_topic('game.wanted', lambda m: start_game(m, pubsub_url))
Exemplo n.º 13
0
from json import load, loads, dumps
from publish_dir import publish_dir
from pubsub import connect as pubsub_connect
from sys import exit
from time import sleep

services_dir = abspath(pathjoin('..', 'services'))
libraries_dir = abspath(pathjoin('..', 'libraries'))

with open(pathjoin(services_dir, 'services.json'), 'r') as config_file:
    config = load(config_file)
docstore_host = str(config['docstore_host'])
docstore_port = str(config['docstore_port'])
docstore_url = 'http://%s:%s' % (docstore_host, docstore_port)
pubsub_url = str(config['pubsub_url'])
pubsub = pubsub_connect(pubsub_url, 'process', marshal=dumps)

docstore_dir = abspath(pathjoin(services_dir, 'docstore_server_http'))
install_dir('docstore_server_http', [docstore_dir, libraries_dir],
            [docstore_host, docstore_port], docstore_url, 'services')

docstore = docstore_connect(docstore_url)
if docstore.wait_until_up() is False:
    print 'Could not start docstore'
    exit(1)

publisher_dir = abspath(pathjoin(services_dir, 'publisher'))
install_dir('publisher', [publisher_dir, libraries_dir], [docstore_url],
            docstore_url, 'services')
sleep(1)
Exemplo n.º 14
0
from platform import node
from pubsub import connect as pubsub_connect
from signal import SIGKILL
from sys import argv

def is_local(server_name):
    return server_name == node()

def get_local_pids(group_path, docstore):
    proc_paths = loads(docstore.get(group_path + '/processes/index.json'))
    proc_data = sum(map(lambda p: loads(docstore.get(p)), proc_paths), [])
    return [pid for (server, pid) in proc_data if is_local(server)]

def move_self_to_end(pids):
    self_pid = getpid()
    if self_pid in pids:
        pids.remove(self_pid)
        pids.append(self_pid)

def kill_group(group_path, docstore):
    local_pids = get_local_pids(group_path, docstore)
    move_self_to_end(local_pids)
    for pid in local_pids:
        kill(pid, SIGKILL)

docstore_url = argv[1]
docstore = docstore_connect(docstore_url)
pubsub_url = getenv('ALEXANDRA_PUBSUB')
pubsub = pubsub_connect(pubsub_url, 'process', unmarshal=loads)
pubsub.consume_topic('kill', lambda m: kill_group(m, docstore))
Exemplo n.º 15
0
from sys import argv


def heart_monitor(game_id, pubsub):
    heartbeat_queue = pubsub.subscribe('heartbeat')
    message = ''
    while message is not None:
        message = pubsub.get_message_block(queue=heartbeat_queue,
                                           timeout=HEARTBEAT_TIMEOUT)


game_id, game_name = argv[1:3]
with open('game.json', 'r') as game_file:
    game_data = load(game_file)
pubsub_url = getenv('ALEXANDRA_PUBSUB')
game_pubsub = pubsub_connect(pubsub_url, 'games-' + game_id, marshal=dumps)
game_pubsub.publish('game.json', game_data)

process_pubsub = pubsub_connect(pubsub_url, 'process', marshal=dumps)
for (comp_name, copies) in game_data['components'].items():
    sources = [
        '/games/%s/components/%s' % (game_name, comp_name), '/libraries'
    ]
    install_message = {
        'name': comp_name,
        'sources': sources,
        'options': [game_id],
        'group': game_id,
        'copies': copies
    }
    process_pubsub.publish('install', install_message)
Exemplo n.º 16
0
def is_local(server_name):
    return server_name == node()


def get_local_pids(group_path, docstore):
    proc_paths = loads(docstore.get(group_path + '/processes/index.json'))
    proc_data = sum(map(lambda p: loads(docstore.get(p)), proc_paths), [])
    return [pid for (server, pid) in proc_data if is_local(server)]


def move_self_to_end(pids):
    self_pid = getpid()
    if self_pid in pids:
        pids.remove(self_pid)
        pids.append(self_pid)


def kill_group(group_path, docstore):
    local_pids = get_local_pids(group_path, docstore)
    move_self_to_end(local_pids)
    for pid in local_pids:
        kill(pid, SIGKILL)


docstore_url = argv[1]
docstore = docstore_connect(docstore_url)
pubsub_url = getenv('ALEXANDRA_PUBSUB')
pubsub = pubsub_connect(pubsub_url, 'process', unmarshal=loads)
pubsub.consume_topic('kill', lambda m: kill_group(m, docstore))
Exemplo n.º 17
0
from json import load, loads, dumps
from publish_dir import publish_dir
from pubsub import connect as pubsub_connect
from sys import exit
from time import sleep     

services_dir = abspath(pathjoin('..', 'services'))
libraries_dir = abspath(pathjoin('..', 'libraries'))

with open(pathjoin(services_dir, 'services.json'), 'r') as config_file:
    config = load(config_file)
docstore_host = str(config['docstore_host'])
docstore_port = str(config['docstore_port'])
docstore_url = 'http://%s:%s' % (docstore_host, docstore_port)
pubsub_url = str(config['pubsub_url'])
pubsub = pubsub_connect(pubsub_url, 'process', marshal=dumps)

docstore_dir = abspath(pathjoin(services_dir, 'docstore_server_http'))
install_dir('docstore_server_http', [docstore_dir, libraries_dir],
            [docstore_host, docstore_port], docstore_url, 'services')

docstore = docstore_connect(docstore_url)
if docstore.wait_until_up() is False:
    print 'Could not start docstore'
    exit(1)

publisher_dir = abspath(pathjoin(services_dir, 'publisher'))
install_dir('publisher', [publisher_dir, libraries_dir],
            [docstore_url], docstore_url, 'services')
sleep(1)    
Exemplo n.º 18
0
from os import getenv
from pubsub import connect as pubsub_connect
from sys import argv

def heart_monitor(game_id, pubsub):
    heartbeat_queue = pubsub.subscribe('heartbeat')
    message = ''
    while message is not None:
        message = pubsub.get_message_block(queue=heartbeat_queue,
                                           timeout=HEARTBEAT_TIMEOUT)

game_id, game_name = argv[1:3]
with open('game.json', 'r') as game_file:
    game_data = load(game_file)
pubsub_url = getenv('ALEXANDRA_PUBSUB')
game_pubsub = pubsub_connect(pubsub_url, 'games-' + game_id, marshal=dumps)
game_pubsub.publish('game.json', game_data)

process_pubsub = pubsub_connect(pubsub_url, 'process', marshal=dumps)
for (comp_name, copies) in game_data['components'].items():
    sources = ['/games/%s/components/%s' % (game_name, comp_name), '/libraries']
    install_message = {'name': comp_name,
                       'sources': sources,
                       'options': [game_id],
                       'group': game_id,
                       'copies': copies}
    process_pubsub.publish('install', install_message)

game_pubsub = pubsub_connect(pubsub_url, 'games-' + game_id)
game_pubsub.publish('game_state', 'running')
print 'Now running game %s' % (game_id,)
Exemplo n.º 19
0
#! /usr/bin/env python

from json import loads, dumps
from os import getenv
from pubsub import connect as pubsub_connect
from sys import argv

def start_game(game_info, pubsub_url):
    name, id = game_info['name'], game_info['id']
    sources = ['/games/%s' % (name,),
               '/services/emcee/run_components',
               '/libraries']
    pubsub = pubsub_connect(pubsub_url, 'process', marshal=dumps)

    install_message = {'name': 'run_components.%s' % (id,),
                       'sources': sources,
                       'options': [id, name]}
    pubsub.publish('install', install_message)

pubsub_url = getenv('ALEXANDRA_PUBSUB')
pubsub = pubsub_connect(pubsub_url, 'emcee', unmarshal=loads)
pubsub.consume_topic('game.wanted', lambda m: start_game(m, pubsub_url))