Esempio n. 1
0
    def __load_config(self):
        """
        This function is used only if servicename is not given, and
        daemon is not started by kasaya daemon.
        """
        from kasaya.conf import load_worker_settings, set_value
        try:
            config = load_worker_settings("service.conf")
        except IOError:
            import sys
            LOG.critical(
                "File 'service.conf' not found, unable to start service.")
            sys.exit(1)

        # system settings overwriting
        for k, v in config['config'].items():
            set_value(k, v)

        # worker environment
        for k, v in config['env'].items():
            os.environ[k.upper()] = v

        # service name
        svcc = config['service']
        svname = svcc['name']
        LOG.info("Service config loaded. Service name: %s" % svname)

        # set flag to load tasks automatically
        self.__auto_load_tasks_module = svcc['module']

        return svname
Esempio n. 2
0
    def register_task(self, name, func, timeout, anonymous, permissions,
                      close_dj_conn):
        # task name
        if name is None:
            name = func.__name__
        if name in self.db:
            if func == self.db[name]['func']:
                return
            c = "Task %s is already registered" % name
            LOG.critical(c)
            raise Exception(c)

        # timeout
        if not timeout is None:
            if type(timeout) != int:
                raise Exception("Timeout must be integer value")
            if timeout < 0:
                raise Exception("Timeout cannot be negative value")

        # extra params for task
        doc = func.__doc__
        if not doc is None:
            doc = doc.strip()
        taskdata = {
            'func': func,
            'doc': doc,  # docstring
            'timeout': timeout,  # timeout in seconds
            'anon': anonymous,  # can task be executed without authorisation
            'perms': permissions,  # permissions required to call task
            'close_djconn': close_dj_conn,  # close django connection on exit
            'res_succ': 0,  # successful calls
            'res_err': 0,  # error finishing calls
            'res_tout': 0,  # timed out calls
        }

        self.db[name] = taskdata
        LOG.debug("Registered task %s" % name)