def handle_task(message): data = json.loads(message.get('data', '{}')) action = data.get('action') if action == ACTION_RESULT: try: worker = Worker(data['worker_id'], pin=data['pin']) except WorkerNotFound: result = errors.get(errors.WORKER_NOT_FOUND) else: worker.end_task(data['result'])
def get(self): """List all workers""" workers = [] for worker in Worker.objects(): workers.append({ 'name': worker.name, 'pin': worker.pin, 'is_busy': worker.is_busy() }) self.finish({ 'workers': workers })
def handle_worker(message): data = json.loads(message.get('data', '{}')) if data.get('direction') != __name__: return action = data.get('action') result = {} if action == ACTION_REGISTER: try: worker = Worker(name=data['name'], pin=data['pin']) except WorkerExists: result = errors.get(errors.WORKER_EXISTS) else: worker.release() workers.append(worker) result = { 'action': action, 'name': data['name'], 'worker_id': worker._id, 'direction': 'workers' } print '{} joined'.format(data['name']) elif action == ACTION_UNREGISTER: try: worker = Worker(data['worker_id'], pin=data['pin']) except WorkerNotFound: result = errors.get(errors.WORKER_NOT_FOUND) else: worker.unregister() result = { 'action': action, 'worker_id': worker._id, 'direction': 'workers' } for w in filter(lambda x: x.name == worker.name, workers): workers.remove(w) print '{} left'.format(worker.name) if result: config.get('storage').publish(Worker.CHANNEL, json.dumps(result))