コード例 #1
0
ファイル: daemon.py プロジェクト: asidev/aybu-manager
    def start(self):
        self.log.info("Starting daemon")

        self.client_socket = self.context.socket(zmq.REP)
        self.worker_socket = self.context.socket(zmq.PUB)
        self.worker_socket.setsockopt(zmq.LINGER, 0)
        self.client_socket.bind(self.config['zmq.daemon_addr'])
        self.worker_socket.bind('inproc://tasks')

        self.log.debug("Starting worker")
        self.worker.start()

        self.log.info("Listening on %s", self.config['zmq.daemon_addr'])

        while True:
            try:
                message = self.client_socket.recv()
                self.worker_socket.send(message)
                task = Task(uuid=message, redis_client=self.redis,
                            queued=datetime.datetime.now())
                task.status = taskstatus.QUEUED

            except Exception as e:
                self.log.exception(e)
                success = False
                response = str(e)

            else:
                success = True
                response = 'Task enqueued'

                try:
                    self.client_socket.send_json(dict(success=success,
                                                      message=response))
                except:
                    self.log.exception("Error sending reply")

        # we never get here...
        self.worker_socket.close()
        self.client_socket.close()
        self.context.term()
コード例 #2
0
ファイル: tasks.py プロジェクト: asidev/aybu-manager
def flush(context, request):
    for task in Task.all(redis_client=request.redis):
        task.remove()
    raise HTTPNoContent()
コード例 #3
0
ファイル: tasks.py プロジェクト: asidev/aybu-manager
def list(context, request):
    return {task.uuid: task.to_dict() for task in
            Task.all(redis_client=request.redis)}
コード例 #4
0
ファイル: tasks.py プロジェクト: asidev/aybu-manager
def get_task(request):
    return Task.retrieve(uuid=request.matchdict['uuid'],
                    redis_client=request.redis)
コード例 #5
0
ファイル: worker.py プロジェクト: asidev/aybu-manager
    def run(self):

        self.log.debug("Worker starting ... ")
        self.socket = self.context.socket(zmq.SUB)
        self.socket.setsockopt(zmq.SUBSCRIBE, "")  # subscribe to all
        self.socket.connect('inproc://tasks')
        log = logging.getLogger('aybu')

        while True:
            task = Task(uuid=self.socket.recv(),
                        redis_client=self.redis,
                        started=datetime.datetime.now())
            session = self.Session()
            if not hasattr(session, 'activity_log'):
                ActivityLog.attach_to(session)

            level = int(task.get('log_level', logging.DEBUG))
            handler = RedisPUBHandler(self.config, self.pub_socket,
                                      self.context, level=level)
            handler.set_task(task)
            log.addHandler(handler)
            log.setLevel(level)
            result = None

            try:
                module_name = 'aybu.manager.daemon.commands.{}'\
                        .format(task.command_module)
                module = __import__(module_name,
                                    fromlist=[task.command_name])
                function = getattr(module, task.command_name)
                log.debug('Task received: %s: %s', task, task.command_args)
                result = function(session, task, **task.command_args)
                session.commit()

            except ImportError:
                session.rollback()
                task.status = taskstatus.FAILED
                task.result = "Cannot find resource {}"\
                        .format(task.command_module)
                log.exception(task.result)

            except AttributeError:
                session.rollback()
                task.status = taskstatus.FAILED
                task.result = "Cannot find action {} on {}"\
                        .format(task.command_name, task.command_module)
                log.critical(task.result)

            except Exception:
                session.rollback()
                log.exception('Error while executing task')
                task.status = taskstatus.FAILED
                task.result = str('Error')

            else:
                task.status = taskstatus.FINISHED
                log.info("Task completed successfully")

            finally:
                task['finished'] = datetime.datetime.now()
                task.result = result or ''
                self.pub_socket.send_multipart(["{}.finished".format(task.uuid),
                                                "task endend"])
                session.close()
                log.removeHandler(handler)
                del handler

        self.socket.close()