Esempio n. 1
0
    def message_deliver(self, channel, basic_deliver, properties, body):
        data = json.loads(body)

        uuid = uuid4()

        publisher = RPCReplyPublisher(self.rabbitmq, properties.reply_to, properties.correlation_id)
        publisher.publish({"id": str(uuid)})
        publisher.close()

        channel.basic_ack(basic_deliver.delivery_tag)
Esempio n. 2
0
    def message_deliver(self, channel, basic_deliver, properties, body):

        data = json.loads(body)

        with self.database.session() as session:
            worker = session.query(Worker).filter(Worker.target == data['target']).\
                filter(Worker.framework == data['framework']).filter(Worker.deleted == False).first()

            if worker is not None:
                worker.tags = data['tags']
            else:
                worker = Worker(target=data['target'], framework=data['framework'], datacenter=data['datacenter'],
                                tags=data['tags'])
                session.add(worker)
            session.commit()

            publisher = RPCReplyPublisher(self.rabbitmq, properties.reply_to, properties.correlation_id)
            publisher.publish({"id": str(worker.id)})
            publisher.close()

        channel.basic_ack(basic_deliver.delivery_tag)
Esempio n. 3
0
    def message_deliver(self, channel, basic_deliver, properties, body):
        data = json.loads(body)

        publisher = RPCReplyPublisher(self.rabbitmq, properties.reply_to, properties.correlation_id)

        token = data['token']

        if self.assignment.validate_token(token) is False:
            publisher.publish({"code": 403, "error": "Invalid API Token"})
            publisher.close()
            channel.basic_ack(basic_deliver.delivery_tag)
            return

        if 'permission' in data:
            if not self.assignment.has_permission_token(token, data['permission']):
                publisher.publish({"code": 403, "error": "Invalid user permissions"})
                publisher.close()
                channel.basic_ack(basic_deliver.delivery_tag)
                return

        username = self.assignment.get_username_from_token(token)
        user_id = self.assignment.get_assignment_id(username)

        publisher.publish({"code": 200, "user": {'id': user_id, 'name': username}})
        publisher.close()

        channel.basic_ack(basic_deliver.delivery_tag)
Esempio n. 4
0
    def message_deliver(self, channel, basic_deliver, properties, body):
        data = json.loads(body)

        workers = []

        with self.database.session() as session:
            worker_objects = session.query(Worker).filter(Worker.framework == data['framework']).\
                filter(Worker.datacenter == data['datacenter']).filter(Worker.deleted == False)

            for worker in worker_objects:
                data = {'id': worker.id,
                        'target': worker.target,
                        'framework': worker.framework,
                        'tags': worker.tags}

                workers.append(data)

        publisher = RPCReplyPublisher(self.rabbitmq, properties.reply_to, properties.correlation_id)
        publisher.publish({'workers': workers})
        publisher.close()

        channel.basic_ack(basic_deliver.delivery_tag)
Esempio n. 5
0
    def message_deliver(self, channel, basic_deliver, properties, body):
        data = json.loads(body)

        self.logger.info("Received task launch "+str(data['task_id']))

        with self.database.session() as session:
            task = session.query(Task).filter(Task.id == data['task_id']).first()

            publisher = RPCReplyPublisher(self.rabbitmq, properties.reply_to, properties.correlation_id)

            if task is None:
                publisher.publish({"error": "Unknown task_id "+data['task_id'], "code": 404})
                publisher.close()
                channel.basic_ack(basic_deliver.delivery_tag)
                return

            if task.status != TaskStatus.PENDING and task.status != TaskStatus.LOST:
                self.logger.warning("Task cannot launch")
                publisher.publish({"error": "Task "+data['task_id']+" does not have PENDING or LOST status", "code": 400})
                publisher.close()
                channel.basic_ack(basic_deliver.delivery_tag)
                return

            worker = session.query(Worker).filter(Worker.id == data['worker_id']).first()

            if worker is None:
                publisher.publish({"error": "Invalid worker id supplied", "code": 400})
                publisher.close()
                channel.basic_ack(basic_deliver.delivery_tag)
                return

            publisher_worker = RPCPublisher(self.rabbitmq, "worker-"+worker.target, "alive-"+worker.framework)
            corr_id = publisher_worker.publish({})

            if corr_id is None:
                publisher.publish({"error": "Worker Alive RPC corr_id is None", "code": 400})
                publisher.close()
                channel.basic_ack(basic_deliver.delivery_tag)
                return

            if publisher_worker.get_data(corr_id) is None:
                publisher.publish({"error": "Worker did not reply it is dead", "code": 400})
                publisher.close()
                channel.basic_ack(basic_deliver.delivery_tag)
                return

            task.status = TaskStatus.STARTING
            session.add(task)
            session.commit()
            session.refresh(task)

            data = {'id': task.id, 'name': task.name, 'actions': []}

            for action in task.actions:
                action_data = {'processor': action.processor, 'arguments': {}}

                if action.arguments is not None:
                    action_data['arguments'] = action.arguments

                data['actions'].append(action_data)

            WorkerRunTask(self.rabbitmq, worker).run(data)

            publisher.publish({"status": task.status.value, "code": 200})
            publisher.close()
            channel.basic_ack(basic_deliver.delivery_tag)