Exemplo n.º 1
0
 def __init__(self,
              inst,
              tag,
              storage=None,
              keyspace=None,
              fqn="mg.core.applications.Application"):
     """
     inst - Instance object
     tag - Application tag
     """
     Loggable.__init__(self, fqn)
     if storage is None:
         if tag == "int" or tag == "main":
             storage = 1
         else:
             storage = 0
     self.storage = storage
     self.inst = inst
     self.tag = tag
     self.keyspace = keyspace
     self.hooks = Hooks(self)
     self.config = Config(self)
     self.modules = Modules(self)
     self.config_lock = Lock()
     self.hook_lock = Lock()
     self.dynamic = False
     self.protocol = "http"
Exemplo n.º 2
0
 def __init__(self, inst, tag, storage=None, keyspace=None, fqn="mg.core.applications.Application"):
     """
     inst - Instance object
     tag - Application tag
     """
     Loggable.__init__(self, fqn)
     if storage is None:
         if tag == "int" or tag == "main":
             storage = 1
         else:
             storage = 0
     self.storage = storage
     self.inst = inst
     self.tag = tag
     self.keyspace = keyspace
     self.hooks = Hooks(self)
     self.config = Config(self)
     self.modules = Modules(self)
     self.config_lock = Lock()
     self.hook_lock = Lock()
     self.dynamic = False
     self.protocol = "http"
Exemplo n.º 3
0
class Application(Loggable):
    """
    Application is anything that can process unified /group/hook/args
    HTTP requests, call hooks, keep it's own database with configuration,
    data and hooks
    """
    def __init__(self,
                 inst,
                 tag,
                 storage=None,
                 keyspace=None,
                 fqn="mg.core.applications.Application"):
        """
        inst - Instance object
        tag - Application tag
        """
        Loggable.__init__(self, fqn)
        if storage is None:
            if tag == "int" or tag == "main":
                storage = 1
            else:
                storage = 0
        self.storage = storage
        self.inst = inst
        self.tag = tag
        self.keyspace = keyspace
        self.hooks = Hooks(self)
        self.config = Config(self)
        self.modules = Modules(self)
        self.config_lock = Lock()
        self.hook_lock = Lock()
        self.dynamic = False
        self.protocol = "http"

    @property
    def db(self):
        try:
            return self._db
        except AttributeError:
            pass
        if self.storage == 2:
            self._db = self.inst.dbpool.dbget(self.keyspace, self.mc,
                                              self.storage, self.tag)
        else:
            self._db = self.inst.dbpool.dbget(self.tag, self.mc, self.storage)
        return self._db

    @property
    def mc(self):
        try:
            return self._mc
        except AttributeError:
            pass
        self._mc = Memcached(self.inst.mcpool, prefix="%s-" % self.tag)
        return self._mc

    @property
    def sql_read(self):
        try:
            return self._sql_read
        except AttributeError:
            pass
        self._sql_read = self.inst.sql_read.dbget(self)
        return self._sql_read

    @property
    def sql_write(self):
        try:
            return self._sql_write
        except AttributeError:
            pass
        self._sql_write = self.inst.sql_write.dbget(self)
        return self._sql_write

    def obj(self, cls, uuid=None, data=None, silent=False):
        "Create CassandraObject instance"
        return cls(self.db, uuid=uuid, data=data, silent=silent)

    def objlist(self, cls, uuids=None, **kwargs):
        "Create CassandraObjectList instance"
        return cls(self.db, uuids=uuids, **kwargs)

    def lock(self, keys, patience=20, delay=0.1, ttl=30, reason=None):
        return MemcachedLock(self.mc,
                             keys,
                             patience,
                             delay,
                             ttl,
                             value_prefix=str(self.inst.instid) + "-",
                             reason=reason)

    def nowmonth(self):
        return datetime.datetime.utcnow().strftime("%Y-%m")

    def nowdate(self):
        return datetime.datetime.utcnow().strftime("%Y-%m-%d")

    def now(self, add=0):
        return (datetime.datetime.utcnow() +
                datetime.timedelta(seconds=add)).strftime("%Y-%m-%d %H:%M:%S")

    def now_local(self, add=0):
        now = self.hooks.call("l10n.now_local", add)
        if not now:
            return self.now(add)
        return now.strftime("%Y-%m-%d %H:%M:%S")

    def yesterday_interval(self):
        now = datetime.datetime.utcnow()
        yesterday = (now +
                     datetime.timedelta(seconds=-86400)).strftime("%Y-%m-%d")
        today = now.strftime("%Y-%m-%d")
        return '%s 00:00:00' % yesterday, '%s 00:00:00' % today

    def store_config_hooks(self, notify=True):
        self.config.store()
        self.modules.load_all()
        self.hooks.store()
        if notify:
            self.hooks.call("cluster.appconfig_changed")

    def config_updater(self):
        return ApplicationConfigUpdater(self)

    def clconf(self, key, default=None):
        return self.inst.dbconfig.get(key, default)

    @property
    def main_host(self):
        return self.inst.conf("metagam", "domain", "main")

    def load(self, *args, **kwargs):
        "Syntactic sugar for modules.load(...)"
        return self.modules.load(*args, **kwargs)

    def call(self, *args, **kwargs):
        "Syntactic sugar for hooks.call(...)"
        return self.hooks.call(*args, **kwargs)
Exemplo n.º 4
0
class Application(Loggable):
    """
    Application is anything that can process unified /group/hook/args
    HTTP requests, call hooks, keep it's own database with configuration,
    data and hooks
    """
    def __init__(self, inst, tag, storage=None, keyspace=None, fqn="mg.core.applications.Application"):
        """
        inst - Instance object
        tag - Application tag
        """
        Loggable.__init__(self, fqn)
        if storage is None:
            if tag == "int" or tag == "main":
                storage = 1
            else:
                storage = 0
        self.storage = storage
        self.inst = inst
        self.tag = tag
        self.keyspace = keyspace
        self.hooks = Hooks(self)
        self.config = Config(self)
        self.modules = Modules(self)
        self.config_lock = Lock()
        self.hook_lock = Lock()
        self.dynamic = False
        self.protocol = "http"

    @property
    def db(self):
        try:
            return self._db
        except AttributeError:
            pass
        if self.storage == 2:
            self._db = self.inst.dbpool.dbget(self.keyspace, self.mc, self.storage, self.tag)
        else:
            self._db = self.inst.dbpool.dbget(self.tag, self.mc, self.storage)
        return self._db

    @property
    def mc(self):
        try:
            return self._mc
        except AttributeError:
            pass
        self._mc = Memcached(self.inst.mcpool, prefix="%s-" % self.tag)
        return self._mc

    @property
    def sql_read(self):
        try:
            return self._sql_read
        except AttributeError:
            pass
        self._sql_read = self.inst.sql_read.dbget(self)
        return self._sql_read

    @property
    def sql_write(self):
        try:
            return self._sql_write
        except AttributeError:
            pass
        self._sql_write = self.inst.sql_write.dbget(self)
        return self._sql_write

    def obj(self, cls, uuid=None, data=None, silent=False):
        "Create CassandraObject instance"
        return cls(self.db, uuid=uuid, data=data, silent=silent)

    def objlist(self, cls, uuids=None, **kwargs):
        "Create CassandraObjectList instance"
        return cls(self.db, uuids=uuids, **kwargs)

    def lock(self, keys, patience=20, delay=0.1, ttl=30, reason=None):
        return MemcachedLock(self.mc, keys, patience, delay, ttl, value_prefix=str(self.inst.instid) + "-", reason=reason)

    def nowmonth(self):
        return datetime.datetime.utcnow().strftime("%Y-%m")

    def nowdate(self):
        return datetime.datetime.utcnow().strftime("%Y-%m-%d")

    def now(self, add=0):
        return (datetime.datetime.utcnow() + datetime.timedelta(seconds=add)).strftime("%Y-%m-%d %H:%M:%S")

    def now_local(self, add=0):
        now = self.hooks.call("l10n.now_local", add)
        if not now:
            return self.now(add)
        return now.strftime("%Y-%m-%d %H:%M:%S")

    def yesterday_interval(self):
        now = datetime.datetime.utcnow()
        yesterday = (now + datetime.timedelta(seconds=-86400)).strftime("%Y-%m-%d")
        today = now.strftime("%Y-%m-%d")
        return '%s 00:00:00' % yesterday, '%s 00:00:00' % today

    def store_config_hooks(self, notify=True):
        self.config.store()
        self.modules.load_all()
        self.hooks.store()
        if notify:
            self.hooks.call("cluster.appconfig_changed")

    def config_updater(self):
        return ApplicationConfigUpdater(self)

    def clconf(self, key, default=None):
        return self.inst.dbconfig.get(key, default)

    @property
    def main_host(self):
        return self.inst.conf("metagam", "domain", "main")

    def load(self, *args, **kwargs):
        "Syntactic sugar for modules.load(...)"
        return self.modules.load(*args, **kwargs)

    def call(self, *args, **kwargs):
        "Syntactic sugar for hooks.call(...)"
        return self.hooks.call(*args, **kwargs)