def enqueue_task(token, locale, eta=None, task=None, store=None, queue="default"): """Take a task and add it to the Task Queue, I've seperated this to allow for recursive retries on failures.""" # First save the data for the task in the datastore, this is because tasks # can only be so large and I was hitting the limit occasionally. if not store: try: store = TaskData(data=json.dumps(task)) store.put() except ApplicationError: enqueue_task(token, locale, eta=eta, task=task, store=None, queue=queue) # Figure out the eta for the task if not eta: eta = grab_eta() # Figure out the key for the store if type(store).__name__ == "str": store_key = store else: store_key = str(store.key()) # Now check if the task contains a custom locale locale = task.get("locale", locale) # Next figure out the correct queue if task: queue = task.get("queue", queue) # Now enqueue the task, give the store's key so we can access all the # data when handling it # (We vary the URL based on type only to get more useful information from # Google's graphs and statistics.) try: taskqueue.add( url="/worker/" + str(task.get("type")), queue_name=task.get("queue", "default"), params={"store_key": store_key, "token": token, "locale": locale}, eta=eta, ) # Finally catch TransientError (temporary failure to enqueue task) and # handle by giving it another shot recursively. except taskqueue.TransientError: enqueue_task(token, locale, eta=eta, task=task, store=store, queue=queue)
def handle_task(store_key, token, locale): """Take a task, given by the task queue worker and deal with it. This requires we look up the task's details in the data store, dispatch the correct handler and finaly enqueue and future tasks generated in the process.""" # First we need to grab the task's data from the store store = TaskData.get(db.Key(store_key)) # Nothing in store by that ID, we have to give up :( if not store: logging.info("Store_key " + store_key + " not found, can't handle task.") return # Delete the store, we want to keep things nice and tidy try: task = json.loads(store.data) store.delete() # If we get an error just recurse round again # (I've already checked the store existed, hopefully that's enough..) except ApplicationError, err: return handle_task(store_key, token, locale)
def create_task_data(user_id): print(user_id) content = request.json task_data = TaskData() task_data.userID = str(content['userID']) task_data.date = str(content['date']) task_data.startTime = str(content['startTime']) task_data.taskSession = str(content['taskSession']) task_data.taskSessionTry = str(content['taskSessionTry']) task_data.trialNum = str(content['trialNum']) task_data.trialTime = str(content['trialTime']) task_data.blockNum = str(content['blockNum']) task_data.trialinBlockNum = str(content['trialinBlockNum']) task_data.devaluedBlock = str(content['devaluedBlock']) task_data.fixTime = str(content['fixTime']) task_data.attenIndexIndiv = str(content['attenIndexIndiv']) task_data.attenCheckKey = str(content['attenCheckKey']) task_data.attenCheckTime = str(content['attenCheckTime']) task_data.stimIndexCondIndiv = str(content['stimIndexCondIndiv']) task_data.stimTime = str(content['stimTime']) task_data.fbProbTrack = str(content['fbProbTrack']) task_data.randProb = str(content['randProb']) task_data.responseKey = str(content['responseKey']) task_data.reactionTime = str(content['reactionTime']) task_data.playFbSound = str(content['playFbSound']) task_data.fbTime = str(content['fbTime']) task_data.volume = str(content['volume']) task_data.volumeNotLog = str(content['volumeNotLog']) BaseObject.check_and_save(task_data) result = dict({"success": "yes"}) return jsonify(result)