def post(self):
        step       = int(self.request.get('step'))
        globalstop = int(self.request.get('globalstop'))
        uniquifier = str(self.request.get('uniquifier'))
        startDesire   = int(self.request.get('start'))
        stopDesire    = int(self.request.get('stop'))

        desireFunc.doDesires(step,startDesire,stopDesire)
        # Commenting out so can just record desires
        #princeFunc.updateAlgStats(step,startAlg,stopAlg)
        if step < globalstop:
            step += 1
            taskAdd("Desires-"+str(startDesire)+"-"+str(stopDesire)+"-step-"+str(step)+"-"+uniquifier,
                    step, globalstop, uniquifier, startDesire, stopDesire, 0)
 def get(self):
     self.response.headers['Content-Type'] = 'text/plain'
     self.response.out.write('I am an algorithm!\n')
     
     import os
     inDev = os.environ.get('SERVER_SOFTWARE', '').startswith('Dev')
     
     task       = str(self.request.get('task'))
     globalstop = str(self.request.get('globalstop'))
     if self.request.get('step') != '':
         step       = int(self.request.get('step'))
     if task == 'true':
         globalstop = int(globalstop)
         uniquifier = str(self.request.get('uniquifier'))
         desireFunc.primeDesireCache(step)    # Must prime cache before starting tasks for first time.
         taskAdd("Desires-"+str(1)+"-"+str(60)+"-step-"+str(step)+"-"+uniquifier,
                 step, globalstop, uniquifier, 1, 60, 0)
     elif task == 'loop' and inDev:
         globalstop = int(globalstop)
         n = str(self.request.get('n'))
         if n == '':
             n = 1
         else:
             n = int(n)
         fillcache = str(self.request.get('fillcache'))
         if fillcache == 'true':
             # Run function to put last 400 steps of desires into memcache.
             desireFunc.primeDesireCache(n)
         if n < globalstop:
             desireFunc.doDesires(n)     # TradeCues express desires
             #commenting out so can just record desires.
             #princeFunc.updateAlgStats(n)       # Prince processes desires and updates algStats
             n += 1
             self.redirect('/algorithms/go?task=loop&n=%s&globalstop=%s'%(n,globalstop))
         else:
             self.response.out.write('Done with step %s!\n'%n)
     elif task == 'loop' and not inDev:
         self.response.out.write('I refuse to loop when not in Dev!\n')
     else:
         startAlg = int(self.request.get('start'))
         stopAlg  = int(self.request.get('stop'))
         result   = doAlgs(step,startAlg,stopAlg)
     self.response.out.write('I am done!')
def startSim(namespace, unique, globalStop, initialStop, stepRange, goNext):
    from google.appengine.api.datastore import Key
    
    JobID = meTools.buildJobID(namespace, unique, globalStop, initialStop, stepRange)

    persistStops = meSchema.WorkQueue(key_name = JobID, globalStop = globalStop, initialStop = initialStop)
    meTools.memPut_multi({persistStops.key().name() : persistStops}, priority = 1)
    
    if not globalStop >= initialStop:
        raise(BaseException('globalStop: %s is not >= lastStopStep: %s' % (globalStop, initialStop)))

    lastDesire = meSchema.desire.all(keys_only = True).filter('__key__ <', Key.from_path('desire','1000000_0000_00')).order('-__key__').get()
    if lastDesire:
        lastDesireStop = int(lastDesire.name().split('_')[0])
    else:
        lastDesireStop = 1
    desireFunc.primeDesireCache(lastDesireStop)
    for step in range(lastDesireStop, globalStop + 1):
        desireFunc.doDesires(step)

    if goNext == 'true':
        doNext(JobID, 'weeklyDesires','')