def save_worker_state(self, worker, lifetime = None): ''' Saves the worker state in memcache in order to retrieve it later, in subsequent tasks dedicated to run this worker. @param worker: Worker object whose state is to be saved @param lifetime: How long the state shall be kept in memcache. ''' if not lifetime and lifetime < config.MEMCACHE_DATA_LIFETIME: lifetime = config.MEMCACHE_DATA_LIFETIME # dump worker state into dictionary state = {} for attr, value in worker._get_state_dict().iteritems(): try: state[attr] = data.save_value(value) except data.DataError, e: logging.error("[gae-workers] Error while saving %s: %s", attr, e)
def post_message(self, msg): """ Posts a message to worker of given ID. Worker will receive it the when calling Worker.GET_MESSAGES routine. @param msg: Message to pass to worker. This can be any object, although simple Python types are recommended. @return: Whether the message was posted (this doesn't mean it was processed!) """ worker_id = getattr(self, "_id", None) if not worker_id: raise InvalidWorkerState("Worker object has not been initialized") mc_key = _WORKER_MESSAGES_MEMCACHE_KEY % {"id": worker_id} msg_queue = memcache.get(mc_key, namespace=config.MEMCACHE_NAMESPACE) # @UndefinedVariable msg_queue = msg_queue or [] msg_queue.append(data.save_value(msg)) if not memcache.set(mc_key, messages, namespace=config.MEMCACHE_NAMESPACE): # @UndefinedVariable logging.error("[gae-workers] Could not post message -- possible overflow of message queue") return False return True