Exemplo n.º 1
0
def controller():
    """
    Connect all the components together and press the big red button.
    """
    multilogging()
    config = DASAnalyticsConfig()
    config.configure()
    logconf = DASAnalyticsLogging(config)

    scheduler = TaskScheduler(config, cherrypy.engine)
    scheduler.subscribe()
    results = ResultManager(config)
    web = AnalyticsWeb(config, scheduler, results)
    logconf.logger.info("Analytics starting")
    logconf.add_handler(results)
    scheduler.add_callback(results.receive_task_result)

    if config.get_tasks():
        logconf.logger.info("Adding %d tasks", len(config.get_tasks()))
        for task in config.get_tasks():
            if config.no_start_offset:
                scheduler.add_task(task, offset=0)
            else:
                scheduler.add_task(task, offset=random.random() * task.interval)

    cherrypy.config["engine.autoreload_on"] = False
    cherrypy.config["server.socket_port"] = config.web_port

    cherrypy.tree.mount(web, config.web_base)

    pid = cherrypy.process.plugins.PIDFile(cherrypy.engine, config.pid)
    pid.subscribe()

    logconf.logger.info("Starting cherrypy")
    cherrypy.engine.start()
    cherrypy.engine.block()
    logconf.logger.info("Stopping cherrypy")
Exemplo n.º 2
0
Arquivo: task.py Projeto: ktf/DAS
    def __call__(self):
        "Callable that the worker process will run."

        self.logger = multilogging().getLogger("DASAnalytics.RunnableTask")
        msg = 'Starting task=%s:%s, class=%s' \
                % (self.name, self.index, self.classname)
        self.logger.info(msg)
        start_time = time.time()

        if not self.classname in TASK_CLASSES:
            msg = 'Task "%s:%s" unknown class "%s", aborting.' \
                % (self.name, self.index, self.classname)
            self.logger.error(msg)
            return {'success': False, 'error': "unknown class",
                    'start_time':start_time, 'finish_time':start_time,
                    'name':self.name, 'index':self.index, 'parent':self.parent,
                    'master_id':self.master_id, 'classname': self.classname}

        klass = TASK_CLASSES[self.classname]

        childlogger = multilogging().getLogger("DASAnalytics.Task",
                                               task_name=self.name,
                                               task_class=self.classname,
                                               task_index=self.index,
                                               task_master=self.master_id,
                                               task_parent=self.parent)

        try:
            das = das_singleton(multitask=None)
        except Exception as exp:
            msg = 'ERROR: task=%s:%s failed to instantiate, aborting' \
                         % (self.name, self.index)
            print msg
            print_exc(exp)
            return {'success': False, 'error': exp,
                    'start_time':start_time, 'finish_time':start_time,
                    'name':self.name, 'index':self.index, 'parent':self.parent,
                    'master_id':self.master_id, 'classname': self.classname}

        #the DAS instance will now be global if this option is set,
        #and otherwise uniquely created by das_factory
        self.kwargs.update({'logger':childlogger,
                            'name':self.name, #task title
                            'index':self.index, # #runs of this task
                            'interval':self.interval, #desired frequency
                            'DAS':das})

        try:
            instance = klass(**self.kwargs)
        except Exception as exp:
            msg = 'ERROR: task=%s:%s failed to instantiate, aborting' \
                         % (self.name, self.index)
            print msg
            print_exc(exp)
            return {'success': False, 'error': exp,
                    'start_time':start_time, 'finish_time':start_time,
                    'name':self.name, 'index':self.index, 'parent':self.parent,
                    'master_id':self.master_id, 'classname': self.classname}
        try:
            result = instance()
        except Exception as exp:
            finish_time = time.time() #we might have run for some time by now
            msg = 'ERROR: task=%s:%s failed during run, aborting' \
                         % (self.name, self.index)
            self.logger.error(msg)
            print_exc(exp)
            return {'success': False, 'error': exp,
                    'start_time':start_time, 'finish_time':finish_time,
                    'name':self.name, 'index':self.index, 'parent':self.parent,
                    'master_id':self.master_id, 'classname': self.classname}

        finish_time = time.time()
        if isinstance(result, dict):
            result.update({'success':True,
                           'start_time':start_time, 'finish_time':finish_time,
                           'name':self.name, 'index':self.index, 'parent':self.parent,
                           'master_id':self.master_id, 'classname': self.classname})
            return result
        else:
            return {'success':True, 'result':result,
                    'start_time':start_time, 'finish_time':finish_time,
                    'name':self.name, 'index':self.index, 'parent':self.parent,
                    'master_id':self.master_id, 'classname': self.classname}