def __init__(self, maxconnection=10): # copie de l'état lors de l'initialisation d'une nouvelle instance self.__dict__ = self.__shared_state if not(self.initialized): #N MajorDomoClient #print "INIT ConnectionPool" zb = ZooBorgFactory(Configuration.getclient()['zookeeper']['mockmode'], Configuration.getclient()['zookeeper']['ip'], Configuration.getclient()['zookeeper']['port']) zooconf = zb.getConf(ZooConst.CLIENT) connection_str = zooconf['broker']['connectionstr'] uniq = Uniq() for _ in range(1, maxconnection): conn = MajorDomoClient(connection_str, False, self) self.__availableconnection.append(conn) #register connexion zb.register(ZooConst.CLIENT, uniq.getid(uniq.CLIENT), self._zconfchanged) self.initialized = True
def __init__(self, verbose=False): """Initialize broker state.""" self.verbose = verbose self.services = {} self.workers = {} self.waiting = [] self.heartbeat_at = time.time() + 1e-3 * self.HEARTBEAT_INTERVAL self.ctx = zmq.Context() self.socket = self.ctx.socket(zmq.ROUTER) self.socket.linger = 0 self.poller = zmq.Poller() self.poller.register(self.socket, zmq.POLLIN) logging.basicConfig(format="%(asctime)s %(message)s", datefmt="%Y-%m-%d %H:%M:%S", level=logging.INFO) uniq = Uniq() self._uniqid = uniq.getid(uniq.BROKER) zb = ZooBorgFactory(Configuration.getbroker()['zookeeper']['mockmode'], Configuration.getbroker()['zookeeper']['ip'], Configuration.getbroker()['zookeeper']['port']) addconf = zb.getConf(ZooConst.BROKER) con_str = addconf['bindstr'] zb.register(ZooConst.BROKER, self._uniqid, self.zoo_conf_changed) self.bind(con_str)
def __init__(self, verbose=False): # TODO: add uniq id self.verbose = verbose uniq = Uniq() self.uniqid = uniq.getid(uniq.WORKER) # zookeeper connection zb = ZooBorgFactory(Configuration.getworker()['zookeeper']['mockmode'], Configuration.getworker()['zookeeper']['ip'], Configuration.getworker()['zookeeper']['port']) addconf = zb.getConf(ZooConst.WORKER) con_str = addconf['broker']['connectionstr'] zb.register(ZooConst.WORKER, self.uniqid, self.zoo_conf_changed) self.worker = MajorDomoWorker(con_str, "echo", self.verbose) # init mongodb pool self.mp = MongoPool(Configuration.getworker()['mongo']['host'], Configuration.getworker()['mongo']['port'], Configuration.getworker()['mongo']['db'], Configuration.getworker()['mongo']['maxcon'])
def FoodDAOFactory(id=None, name_fr=None, pro=None, lip=None, glu=None, cal=None, qty=None, unit=None): factory = {} factory['REAL'] = FoodDAOMongo factory['MOCK'] = FoodDAOMock workerdaomockmode = Configuration.getworker()['dbdaoimpl'] return factory[workerdaomockmode](id, name_fr, pro, lip, glu, cal, qty, unit)
list.append(FoodDAOMock('3', 'random food 3' + pattern)) list.append(FoodDAOMock('4', 'random food 4' + pattern)) return list def save(self): pass if __name__ == '__main__': parser = argparse.ArgumentParser(description='Test objects') parser.add_argument('-c', '--conf', help='conf to load', type=str) args = parser.parse_args() # init conf conf = Configuration(args.conf) # init mongo pool mp = MongoPool(Configuration.getworker()['mongo']['host'], Configuration.getworker()['mongo']['port'], Configuration.getworker()['mongo']['db']) impl = FoodDAOFactory('youpi', 'label') print impl print type(impl) print FoodDAOFactory().get('myid') fdao = FoodDAOFactory() try: res = fdao.searchFoodByPattern('an') except: print 'we got an error' finally: print res
def SearchFoodServiceFactory(request): factory = {} factory['REAL'] = SearchFoodService factory['MOCK'] = SearchFoodServiceMock clientmockmode = Configuration.getClientMockMode() return factory[clientmockmode](request)
# encoding: utf-8 import argparse import traceback import codecs import re from distark.majordaemon.worker.db.mongopool import MongoPool from distarkcli.utils.MyConfiguration import Configuration # TODO: pass the configuration by command line MAX_CONN = 1 host = Configuration.getworker()["mongo"]["host"] port = Configuration.getworker()["mongo"]["port"] db = Configuration.getworker()["mongo"]["db"] mp = MongoPool(host, port, db, MAX_CONN) refloat = re.compile("^\d+(\.\d+)*$") def is_valid_qte(line): res = False qte = [u"100 g", u"1 part", u"1 pot", u"1 unité", u"1 tranche", u"1 portion", u"100 ml", u"1 canette", u"c.c."] if line in qte: res = True return res def store_data(datadic): # store in db
zb.zk.set("/distark/broker/conf/bindstr", b"tcp://*:5555") zb.zk.ensure_path("/distark/broker/conf/conf_reload_trigger") if __name__ == "__main__": ############################################## # ARGUMENTS PARSING ############################################## parser = argparse.ArgumentParser(description="Send requests") parser.add_argument("do", help="test initclientconf initworkerconf initbrokerconf initall", type=str) parser.set_defaults(do="test") args = parser.parse_args() print "Program Launched with args:" + str(args) print "Do:" + str(args.do) zb = None ip = Configuration.getclient()["zookeeper"]["ip"] port = Configuration.getclient()["zookeeper"]["port"] zb = ZooBorg(ip, port) try: if args.do == "initclientconf": _initclientconf(zb) elif args.do == "initworkerconf": _initworkerconf(zb) elif args.do == "initbrokerconf": _initbrokerconf(zb) elif args.do == "initall": _initworkerconf(zb) _initclientconf(zb) _initbrokerconf(zb) else: print "do nothing !!!"
def AnotherServiceFactory(request): factory = {} factory["REAL"] = AnotherService factory["MOCK"] = AnotherServiceMock clientmockmode = Configuration.getClientMockMode() return factory[clientmockmode](request)