def test_worker_unable_to_connect(): socket_path = "tests/enp2" kwargs = dict(app="testapp", endpoint=socket_path, uuid="randomuuid", disown_timeout=1, heartbeat_timeout=2) w = Worker(**kwargs) w.run()
from cocaine.decorators import http from cocaine.worker import Worker __author__ = 'Evgeny Safronov <*****@*****.**>' @http def generate(request, response): request = yield request.read() try: message = request.request['message'] out = StringIO.StringIO() img = qrcode.make(message) img.save(out, 'png') response.write_head(200, [('Content-type', 'image/png')]) response.write(out.getvalue()) except KeyError: response.write_head(400, [('Content-type', 'text/plain')]) response.write('Query field "message" is required') except Exception as err: response.write_head(400, [('Content-type', 'text/plain')]) response.write(str(err)) finally: response.close() w = Worker() w.run({ 'generate': generate })
#!/usr/bin/env python from cocaine.worker import Worker from cocaine.decorators.wsgi import wsgi from app import app W = Worker() W.run({"http": wsgi(app)})
# author: I. Korolev from __future__ import unicode_literals from __future__ import print_function from __future__ import absolute_import from cocaine.worker import Worker from time import sleep import msgpack from cocaine.logging import Logger log = Logger() def sleepy_echo(request, response): """Sleep for requested number of seconds""" msg = yield request.read() t = msgpack.loads(msg) log.debug("Sleeping for {0}...".format(t)) sleep(t) log.debug("Awaken!") response.write("Awaken!") response.close() W = Worker() W.run({ "sleepy_echo": sleepy_echo })
def test_worker_missing_args(): Worker()
def drop(request, response): raw = yield request.read() tablename = msgpack.unpackb(raw) try: drop_query = "DROP TABLE IF EXISTS %s" % tablename log.debug(drop_query) mysqldg.perfomCustomQuery(drop_query) except Exception as err: response.error(-100, str(err)) else: response.write("ok") response.close() def query(request, response): raw = yield request.read() tablename, querystr = msgpack.unpackb(raw) try: log.debug("QUERY STRING: %s " % querystr) res = cPickle.dumps(mysqldg.perfomCustomQuery(querystr)) except Exception as err: response.error(-99, str(err)) else: response.write(res) response.close() if __name__ == "__main__": W = Worker() W.run({"put": put, "drop": drop, "query": query})
#!/usr/bin/env python from cocaine.worker import Worker from cocaine.decorators.wsgi import wsgi from app import app if __name__ == '__main__': W = Worker() W.run({"http": wsgi(app)})
# Send data to various senders for name, item in aggcfg.senders.iteritems(): try: sender_type = item.get("type") if sender_type is None: logger.error("unable to detect sender type: %s", name) continue logger.info("Send to %s", sender_type) s = Service(sender_type) res = yield s.enqueue( "send", msgpack.packb({ "Config": item, "Data": result, "Id": task.Id })) logger.info("res for %s is %s", sender_type, res) except Exception as err: logger.error("unable to send to %s %s", name, err) logger.info("Result %s", result) response.write("Done") response.close() if __name__ == "__main__": W = Worker() W.run({"handleTask": aggreagate})
#!/usr/bin/env python import msgpack from cocaine.decorators import http from cocaine.worker import Worker from cocaine.logging import Logger @http def main(request, response): response.write_head(200, [("Content-Type", "plain/text")]) response.write("Hello, world!") response.close() W = Worker() W.run({ 'doIt': main, })
#!/usr/bin/env python from cocaine.worker import Worker from cocaine.services import Service storage = Service("storage") def write_dummy(request, response): req = yield request.read() yield storage.write("dummy-namespace", "dummy-key", req, ["dummy-tag"]) dummy = yield storage.read("dummy-namespace", "dummy-key") response.write(dummy) response.close() W = Worker() W.run({"write_dummy": write_dummy})
import qrcode from cocaine.decorators import http from cocaine.worker import Worker __author__ = 'Evgeny Safronov <*****@*****.**>' @http def generate(request, response): request = yield request.read() try: message = request.request['message'] out = StringIO.StringIO() img = qrcode.make(message) img.save(out, 'png') response.write_head(200, [('Content-type', 'image/png')]) response.write(out.getvalue()) except KeyError: response.write_head(400, [('Content-type', 'text/plain')]) response.write('Query field "message" is required') except Exception as err: response.write_head(400, [('Content-type', 'text/plain')]) response.write(str(err)) finally: response.close() w = Worker() w.run({'generate': generate})
def main(): w = Worker() w.run({"ping": echo, "inc": inc, "getfile": http_getfile})
#!/usr/bin/env python import msgpack from cocaine.worker import Worker from cocaine.logging import Logger __author__ = 'Evgeny Safronov <*****@*****.**>' log = Logger() def echo(request, response): message = yield request.read() log.debug('Message received: \'{0}\'. Sending it back ...'.format(message)) response.write(msgpack.dumps(message)) response.close() W = Worker() W.run({ 'doIt': echo, })
def main(): w = Worker() w.on("ping", echo) w.run()
#!/usr/bin/env python import msgpack from cocaine.worker import Worker from cocaine.logging import Logger __author__ = 'EvgenySafronov <*****@*****.**>' log = Logger() def echo(request, response): message = yield request.read() log.debug('Message received: \'{0}\'. Sending it back ...'.format(message)) response.write(msgpack.dumps(message)) response.close() W = Worker() W.run({'doIt': echo})
#!/usr/bin/env python import logging import os from cocaine.decorators.wsgi import django from cocaine.logging import LoggerHandler from cocaine.worker import Worker __author__ = 'Evgeny Safronov <*****@*****.**>' log = logging.getLogger(__name__) cocaineHandler = LoggerHandler() log.addHandler(cocaineHandler) PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__)) worker = Worker() worker.run({ 'work': django(**{ 'root': os.path.join(PROJECT_ROOT, 'enterprise'), 'settings': 'enterprise.settings', 'async': True, 'log': log }) })
# by the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # Cocaine is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # from cocaine.worker import Worker from cocaine.logging import Logger __author__ = 'EvgenySafronov <*****@*****.**>' log = Logger() def echo(request, response): message = yield request.read() log.debug('Message received: \'{0}\'. Sending it back ...'.format(message)) response.write(message) response.close() W = Worker() W.run({ 'ping': echo, })
meta_session = elliptics.Session(meta_node) meta_session.set_timeout(meta_wait_timeout) meta_session.add_groups(list(config["metadata"]["groups"])) logger.info("trace %d" % (i.next())) n.meta_session = meta_session mrsc_options = config['metadata'].get('options', {}) meta_db = None if config['metadata'].get('url'): meta_db = MongoReplicaSetClient(config['metadata']['url'], **mrsc_options) logger.info("trace %d" % (i.next())) logger.info("before creating worker") W = Worker(disown_timeout=config.get('disown_timeout', 2)) logger.info("after creating worker") b = balancer.Balancer(n, meta_db) def register_handle(h): @wraps(h) def wrapper(request, response): start_ts = time() req_uid = uuid.uuid4().hex try: data = yield request.read() data = msgpack.unpackb(data) logger.info(':{req_uid}: Running handler for event {0}, ' 'data={1}'.format(h.__name__,
def main(): worker = Worker() urlfetcher = UrlFetcher(io_loop=worker.loop) worker.on('get', urlfetcher.on_get_request) worker.on('post', urlfetcher.on_post_request) worker.run()
if len(tagging) > 0: html += """ <h2>%s ---> %s</h2> """ % (str(message.split()), str(tagging)) html += """ <form name="submitform" method="get" onSubmit="window.location.replace('/?message=' + document.submitform['sentence'])"> Sentence: <input type="text" name="message"><br> <input type="submit" value="Tag!"> </form> """ html += """ Please, send your suggestions to: [email protected] </body> </html> """ # message = "i love birds" # response.write(str(message) + " " + str(tagging)) response.write(html) response.close() W = Worker() W.run({ 'posTag': main, }) # test()
def load_cfg(cls): with open(cls.CONFIG_PATH, 'r') as f: cls.config = yaml.load(f) return cls.config def send(request, response): raw = yield request.read() task = msgpack.unpackb(raw) log.info("%s" % str(task)) ID = task.get("Id", "MissingID") hosts = JConfig.get_config() juggler_config = task['Config'] juggler_config.update(hosts) juggler_config['id'] = ID jc = Juggler(**juggler_config) try: jc.Do(task["Data"]) except Exception as err: log.error("%s %s" % (ID, str(err))) finally: response.write("ok") log.info("%s Done" % ID) response.close() if __name__ == "__main__": W = Worker() W.run({"send": send})
message = request.request['message'] size = int(request.request.get('size', 10)) if size < 10: data = generate_qr(message, size) else: key = '{0}size={1}'.format(message, size) try: data = yield storage.read('qr-codes', key) except ServiceError: data = generate_qr(message, size) yield storage.write('qr-codes', key, data) response.write_head(200, [('Content-type', 'image/png')]) response.write(data) except KeyError: response.write_head(400, [('Content-type', 'text/plain')]) response.write('Query field "message" is required') except Exception as err: response.write_head(400, [('Content-type', 'text/plain')]) response.write(str(err)) finally: response.close() w = Worker() w.run({ 'generate': generate, 'generate-http': generate_http })
meta_session = elliptics.Session(meta_node) meta_session.set_timeout(meta_wait_timeout) meta_session.add_groups(list(config["metadata"]["groups"])) logger.info("trace %d" % (i.next())) n.meta_session = meta_session mrsc_options = config['metadata'].get('options', {}) meta_db = None if config['metadata'].get('url'): meta_db = MongoReplicaSetClient(config['metadata']['url'], **mrsc_options) logger.info("trace %d" % (i.next())) logger.info("before creating worker") W = Worker(disown_timeout=config.get('disown_timeout', 2)) logger.info("after creating worker") b = balancer.Balancer(n, meta_db) def register_handle(h): @wraps(h) def wrapper(request, response): start_ts = time() req_uid = uuid.uuid4().hex try: data = yield request.read() data = msgpack.unpackb(data) logger.info(
def main(): w = Worker() w.run({"http": echo})
from cocaine.decorators import http import uuid log = Logger(endpoints=(("2a02:6b8:0:1a16:556::200", 10053), )) @http def http_echo(request, response): req_id = uuid.uuid4().hex log.info("request %s: start" % req_id) req = yield request.read() log.info("request %s: got body of %s bytes" % (req_id, len(req.body))) response.write_head(200, {}) log.info("request %s: responding with original body" % req_id) response.write(req.body) log.info("request %s: done" % req_id) if __name__ == '__main__': W = Worker() W.run({"http": http_echo})
def test_worker_wrong_timeouts(): Worker(heartbeat_timeout=1, disown_timeout=2)
#!/usr/bin/env python # For YaSubbotnik at 15.06.2013 from cocaine.worker import Worker W = Worker() # Dispatcher object def event_handler(request, response): req = yield request.read() # Read incoming data if "Hello!" in req: response.write("Hello, world!") # Send data chunk else: response.write("Please, say 'Hello' to me!") response.close() W.run({"hello": event_handler}) # Run event loop - ready to work!
def main(): log.info("entire main()") w = Worker() log.info("do main().Worker()") w.run({"http": echo})
log.setup_logger() logger = logging.getLogger('mm.init') logger.warn( 'mm_inventory_logging is not set up properly in ' 'cocaine.conf, fallback to default logging service' ) from config import config # TODO: rename inv module to 'inventory' when switched to using inventory worker import inv as inventory import helpers def init_inventory_worker(worker): helpers.register_handle_wne(worker, inventory.Inventory.get_dc_by_host) DEFAULT_DISOWN_TIMEOUT = 2 if __name__ == '__main__': logger.info("before creating inventory worker") worker = Worker(disown_timeout=config.get('disown_timeout', DEFAULT_DISOWN_TIMEOUT)) logger.info("after creating inventory worker") init_inventory_worker(worker) logger.info("Starting inventory worker") worker.run() logger.info("Inventory worker stopped")
#!/usr/bin/env python import asyncio from cocaine.worker import Worker from cocaine.services import Service w = Worker(app="app", uuid="a", endpoint="enp", heartbeat_timeout=2, disown_timeout=1) node = Service("node", version=0) @asyncio.coroutine def echo(request, response): yield asyncio.sleep(1) inp = yield request.read(timeout=1) print inp fut = yield node.list() result = yield fut.get() print result response.write(result) response.close() w.on("echo", echo) w.run()
def read(request, response): try: channel = yield storage.read(NAMESPACE, KEY) data = yield channel.rx.get() response.write(data) except Exception as err: response.error(-100, repr(err)) finally: response.close() @http def http_read(request, response): yield request.read() try: channel = yield storage.read(NAMESPACE, KEY) res = yield channel.rx.get() except Exception as err: response.write_head(502, {}) response.write(str(err)) else: response.write_head(200, {}) response.write(res) if __name__ == "__main__": W = Worker() W.run({"write": write, "read": read, "http": http_read})
#!/usr/bin/env python import msgpack from cocaine.worker import Worker from cocaine.logging import Logger __author__ = 'EvgenySafronov <*****@*****.**>' log = Logger() def echo(request, response): message = yield request.read() log.debug('Message received: \'{0}\'. Sending it back ...'.format(message)) response.write(msgpack.dumps(message)) response.close() W = Worker() W.run({ 'ping': echo, })
L.info(error) future = urlfetcher_service.get("www.ya.ru",{}, True) future(on_url) L.info("INITIALIZE FUNCTION") future = urlfetcher_service.get() future(on_url, errorback) @http def http(request, response): stat = yield request.read() L.info("HTTP") L.info(stat) response.write_head(200, [('Content-type', 'text/plain')]) response.write("OK") response.close() @fs def fs(request, response): stat = yield request.read() L.info("FS") L.info(stat) response.write("OK") response.close() W = Worker() #W.on("hash", example) #W.on("nodejs", nodejs) #W.on("fs", fs) W.run({"hash" : example, "fs" : fs})
def main(): w = Worker() w.run({"http": unicorn_tank})
def read(request, response): try: channel = yield storage.read(NAMESPACE, KEY) data = yield channel.rx.get() response.write(data) except Exception as err: response.error(-100, repr(err)) finally: response.close() @http def http_read(request, response): yield request.read() try: channel = yield storage.read(NAMESPACE, KEY) res = yield channel.rx.get() except Exception as err: response.write_head(502, {}) response.write(str(err)) else: response.write_head(200, {}) response.write(res) if __name__ == '__main__': W = Worker() W.run({"write": write, "read": read, "http": http_read})
#!/usr/bin/env python from cocaine.worker import Worker from cocaine.decorators import wsgi from app import app print "A" w = Worker() print "B" w.run({"http": wsgi(app)})
log.info("Put data into %s" % tablename) try: m = MySqlDG(**config) m.putData(data, tablename) except Exception as err: response.error(-100, str(err)) else: response.write(tablename) response.close() def drop(request, response): raw = yield request.read() config, tablename = msgpack.unpackb(raw) try: m = MySqlDG(**config) drop_query = "DROP TABLE IF EXISTS %s" % tablename log.info(drop_query) m.perfomCustomQuery(drop_query) except Exception as err: response.error(-100, str(err)) else: response.write("ok") response.close() if __name__ == "__main__": W = Worker() W.run({"put": put, "drop": drop})
#!/usr/bin/python # NB: this import have to be executed as early as possible # to guarantee proper initialization of ioloops in # each process of cocaine pool import flowmastermind from cocaine.decorators.wsgi import wsgi from cocaine.worker import Worker Worker().run({'http': wsgi(flowmastermind.app)})
except Exception as err: logger.error("unable to aggreagate all: %s %s", name, err) logger.info("name %s ALL %s %d" % (name, res, len(all_data))) result[name][metahost] = res # Send data to various senders for name, item in aggcfg.senders.iteritems(): try: sender_type = item.get("type") if sender_type is None: logger.error("unable to detect sender type: %s", name) continue logger.info("Send to %s", sender_type) s = Service(sender_type) res = yield s.enqueue("send", msgpack.packb({"Config": item, "Data": result, "Id": task.Id})) logger.info("res for %s is %s", sender_type, res) except Exception as err: logger.error("unable to send to %s %s", name, err) logger.info("Result %s", result) response.write("Done") response.close() if __name__ == "__main__": W = Worker() W.run({"handleTask": aggreagate})
#!/usr/bin/env python import logging import os from cocaine.decorators.wsgi import django from cocaine.logging import LoggerHandler from cocaine.worker import Worker __author__ = 'Evgeny Safronov <*****@*****.**>' PROJECT_NAME = 'enterprise' log = logging.getLogger(__name__) cocaineHandler = LoggerHandler() log.addHandler(cocaineHandler) PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__)) worker = Worker() worker.run({ 'work': django(**{ 'root': os.path.join(PROJECT_ROOT, PROJECT_NAME), 'settings': '{0}.settings'.format(PROJECT_NAME), 'async': True, 'log': log }) })
#!/usr/bin/env python from __future__ import print_function, unicode_literals from cocaine.worker import Worker from cocaine.decorators import wsgi from app import app print("A") w = Worker() print("B") w.run({"http": wsgi(app)})
#!/usr/bin/env python # -*- coding: utf-8 -*- # # Copyright (c) 2012+ Tyurin Anton [email protected] # # This file is part of Combaine. # # Combaine is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # Combaine is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # from cocaine.worker import Worker from combaine.cloud.parsingapp import parse if __name__ == "__main__": W = Worker(disown_timeout=300) W.run({"parse": parse})
#!/usr/bin/env python import logging import os from cocaine.decorators.wsgi import django from cocaine.logging import LoggerHandler from cocaine.worker import Worker __author__ = 'Evgeny Safronov <*****@*****.**>' PROJECT_NAME = 'enterprise' log = logging.getLogger(__name__) cocaineHandler = LoggerHandler() log.addHandler(cocaineHandler) PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__)) worker = Worker() worker.run({ 'work': django( **{ 'root': os.path.join(PROJECT_ROOT, PROJECT_NAME), 'settings': '{0}.settings'.format(PROJECT_NAME), 'async': True, 'log': log }) })
# users "user-exists": user_exists, "user-signup": user_signup, "user-signin": user_signin, "user-remove": user_remove, "user-list": user_list, "user-upload": user_upload, "user-app-list": user_apps_list, "user-buildlog-list": user_buildlog_list, "user-buildlog-read": user_buildlog_read, # app "app-info": app_info, "app-deploy": app_deploy, "app-start": app_start, "app-stop": app_stop, } API = {"Version": 1, "Methods": binds.keys()} def api(request, response): yield request.read() response.write(API) response.close() if __name__ == '__main__': W = Worker() W.on("API", api) W.run(binds)
def write(request, response): data = yield request.read() try: channel = yield storage.write(NAMESPACE, KEY, data, []) yield channel.rx.get() response.write("Ok") except Exception as err: response.error(-100, repr(err)) finally: response.close() def read(request, response): try: channel = yield storage.read(NAMESPACE, KEY) data = yield channel.rx.get() response.write(data) except Exception as err: response.error(-100, repr(err)) finally: response.close() if __name__ == '__main__': W = Worker() W.run({"write": write, "read": read, "http": wsgi(app)})
#!/usr/bin/env python import msgpack from cocaine.worker import Worker from cocaine.logging import Logger __author__ = 'EvgenySafronov <*****@*****.**>' log = Logger() def chunker(request, response): chunks = yield request.read() try: chunks = int(msgpack.loads(chunks)) except ValueError: chunks = int(chunks) for num in xrange(chunks): response.write(msgpack.dumps('{0:-<1024}'.format(num))) response.write(msgpack.dumps('Done')) response.close() W = Worker() W.run({'chunkMe': chunker})
response.error(-100, "There's no class named %s" % klass_name) logger.error("class %s is absent", klass_name) except Exception as err: logger.error("%s", err) response.error(100, repr(err)) else: logger.info("Result of group aggreagtion %s", str(result)) response.write(result) response.close() def _aggregate_host(klass_name, payload, config, task): available = plugin_import() klass = available[klass_name] handler = klass(config) prevtime, currtime = task["prevtime"], task["currtime"] return handler.aggregate_host(payload, prevtime, currtime) def _aggregate_group(klass_name, payload, config): available = plugin_import() klass = available[klass_name] handler = klass(config) return handler.aggregate_group(payload) if __name__ == '__main__': W = Worker() W.run({"aggregate_host": aggregate_host, "aggregate_group": aggregate_group})
#!/usr/bin/env python import msgpack from cocaine.decorators import http from cocaine.worker import Worker from cocaine.logging import Logger __author__ = 'EvgenySafronov <*****@*****.**>' log = Logger() def echo(request, response): message = yield request.read() log.debug('Message received: \'{0}\'. Sending it back ...'.format(message)) response.write(msgpack.dumps(message)) response.close() W = Worker() W.run({ 'doIt': echo, })
#!/usr/bin/env python from cocaine.worker import Worker from cocaine.decorators.wsgi import wsgi from cocaine.services import Service from app import app W = Worker() def raw(request, response): inc = yield request.read() goapp = Service("gococaine") storage = Service("storage") # Send data to the go application response.write("Send data to the go application") key = yield goapp.enqueue("process", str(inc)) response.write("Data was processed and saved to the storage, key: %s" % key) # Read data from storage res = yield storage.read("namespace", key) response.write(res) response.close() def read(request, response): key = yield request.read() storage = Service("storage") data = yield storage.read("namespace", key) response.write(data) response.close()
def aggregate_group(request, response): raw = yield request.read() tid, cfg, data = msgpack.unpackb(raw) logger = get_logger_adapter(tid) logger.debug("Unpack raw data successfully") raw_data = map(msgpack.unpackb, data) ret = merge(raw_data) logger.debug("Data has been merged %s", ret) qts = map( int, map(lambda x: float(ret["count"]) * x / 100, cfg.get("values", [75, 90, 93, 94, 95, 96, 97, 98, 99]))) try: ret = quants(qts, ret['data']) except Exception as err: logger.error(str(err)) response.error(100, repr(err)) else: logger.info("Result of group aggreagtion %s", ret) response.write(ret) response.close() if __name__ == '__main__': W = Worker() W.run({ "aggregate_host": aggregate_host, "aggregate_group": aggregate_group })
storage = Service("storage") def write(request, response): data = yield request.read() try: channel = yield storage.write(NAMESPACE, KEY, data, []) yield channel.rx.get() response.write("Ok") except Exception as err: response.error(-100, repr(err)) finally: response.close() def read(request, response): try: channel = yield storage.read(NAMESPACE, KEY) data = yield channel.rx.get() response.write(data) except Exception as err: response.error(-100, repr(err)) finally: response.close() if __name__ == '__main__': W = Worker() W.run({"write": write, "read": read, "http": wsgi(app)})